Unable to pass inputs to stdin of Python subprocess

34 Views Asked by At

Unable to pass inputs to stdin of subprocess. Terminal gets struck while waiting for input. How can I handle stdin properly?

def run_command(command: str, out_callback, in_callback):

    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True)

    while process.poll() is None:
        if process.stdout:
            # On Output
            for line in process.stdout:
                out_callback(line.strip())

        if process.stderr:
            # On Error
            for line in process.stderr:
                out_callback(line.strip())

        if process.stdin:
            # On Input !!! This doesnt works !!!
            input = in_callback()
            process.stdin.write(input.encode())
            process.stdin.flush()

        # On Termination
        out_callback(process.returncode)
0

There are 0 best solutions below