process.stdout.readline() hangs. How to use it properly?

后端 未结 3 1849
自闭症患者
自闭症患者 2020-12-21 02:59

I want to repeatedly send requests to process standard input and receive responses from standard output without calling subprocess mul

3条回答
  •  温柔的废话
    2020-12-21 03:07

    There are a couple of minor tweaks you can make to get this working. First is to disable buffered output in the child using the -u option. Second is to send a newline character along with the user-inputted message to the child process, so that the raw_input call in the child completes.

    main.py

    import subprocess
    
    # We use the -u option to tell Python to use unbuffered output
    p = subprocess.Popen(['python','-u', 'subproc.py'],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE)
    
    while True:
        s = raw_input('Enter message:')
        p.stdin.write(s + "\n")  # Include '\n'
        p.stdin.flush()
        response = p.stdout.readline()
        if response != '': 
            print "Process response:", response
        else:
            break
    

    You should also wrap the child process in an infinite loop, or things will break after the first message is sent:

    subproc.py:

    while True:
        s = raw_input()
        print 'Input=',s
    

    Output:

    dan@dantop:~$ ./main.py 
    Enter message:asdf
    Process response: Input= asdf
    
    Enter message:asdf
    Process response: Input= asdf
    
    Enter message:blah blah
    Process response: Input= blah blah
    
    Enter message:ok
    Process response: Input= ok
    

提交回复
热议问题