Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个stdin写入的python popen.communicate()_Python_Stream_Stdout_Pipe_Stdin - Fatal编程技术网

具有多个stdin写入的python popen.communicate()

具有多个stdin写入的python popen.communicate(),python,stream,stdout,pipe,stdin,Python,Stream,Stdout,Pipe,Stdin,我试图从python触发一个外部进程,然后通过Popen.stdin.communicate()将信息发送到该进程。我正在谈论的进程向stdout写入几行,然后通过stdin(一个数字)提示用户输入,然后再写入几行,并通过stdin提示第二次输入。我想用一个python脚本来代替手动输入,该脚本将为我实现自动化。到目前为止,我有以下代码很好地提交了第一轮用户输入: working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], std

我试图从python触发一个外部进程,然后通过Popen.stdin.communicate()将信息发送到该进程。我正在谈论的进程向stdout写入几行,然后通过stdin(一个数字)提示用户输入,然后再写入几行,并通过stdin提示第二次输入。我想用一个python脚本来代替手动输入,该脚本将为我实现自动化。到目前为止,我有以下代码很好地提交了第一轮用户输入:

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
working_file.communicate(input=b'3')[0]
子进程按预期运行,然后提示输入第二个输入,不幸的是,communicate()似乎不允许您多次写入stdin。接下来,我尝试用以下内容替换上一行:

working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
working_file.stdin.write(b'3')
working_file.stdin.write(b'7')
我没有收到任何错误,因此可能数据正在某个地方,但子流程没有收到数据(我知道这一点,因为当它收到数据时,它会写入日志文件)。很明显,我不理解某些东西,我想更好地理解communicate()在幕后所做的事情,这样我就可以找到另一种写入stdin的方法,但我发现这个对象上的文档有点难以理解。我在缓冲区和流方面也不是专家,所以这让我很困惑

有谁能解释一下comunicate对象是如何工作的,以及我是如何通过多次写入完成类似的工作的。还有人知道哪里是阅读流和缓冲区的好地方吗

非常感谢

jon

根据python:

请注意,如果要将数据发送到进程的stdin,则需要 使用stdin=PIPE创建Popen对象。同样地,为了得到任何东西 除了结果元组中的None之外,还需要指定stdout=PIPE 和/或stderr=管道也


您好,刚刚更改了帖子,显示了使用管道属性创建子流程的过程,我想我做得对,我假设communicate()对象可以工作,也不应该工作,除非存在stdin=PIPE,jt