python pipe only stdin,out once, how to do twice or more time

前端 未结 2 1344
花落未央
花落未央 2021-01-28 10:51

success python pipe stdin, out only one time this source

main.py

import subprocess from subprocess import PIPE, STDOUT

player_pipe = subprocess.Popen([\         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 11:33

    the output of call.py is buffered. so you have to flush() it to send to main.py.

    #!/usr/bin/python2
    import sys
    
    getMsg = raw_input()
    print getMsg
    sys.stdout.flush()
    
    getMsg2 = raw_input()
    print getMsg2
    sys.stdout.flush()
    

    Note that you need shebang #!/usr/bin/python2 at least when your OS is Linux (I don't know why OP's code works without shebang. Maybe some Windows magic ?).

    Also you can use -u option not to buffer the output of python.

    player_pipe = subprocess.Popen(["/usr/bin/python2","-u","./call.py"], stdin=PIPE,
         stdout=PIPE, stderr=STDOUT, shell=False)
    

提交回复
热议问题