Python PIPE to popen stdin

前端 未结 3 1986
南方客
南方客 2021-01-14 03:57

I am attempting something very similar to real time subprocess.Popen via stdout and PIPE

I, however, want to send input to the running process as well.

If I

3条回答
  •  星月不相逢
    2021-01-14 04:25

    The select module in the standard library is made for these kind of situation:

    process = subprocess.Popen(cmd,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    while True:
       reads,writes,excs = select.select([process.stdout, process.stderr], [process.stdin], [], 1)
       for r in reads:
           out = r.read(1)
           display.insert(INSERT, out)
           sys.stdout.write(out)
           sys.stdout.flush()
       for w in writes:
           w.write('a')
    

    You can pass a list of file objects or file descriptors to select() which will return those files that are have data ready for read/write or until an optional timeout.

    The select module works on both Windows and Unix-like systems (Linux, Macs, etc).

提交回复
热议问题