简体   繁体   English

Python子进程通信杀死了我的进程

[英]Python subprocess communicate kills my process

Why does communicate kill my process? 为什么沟通会杀死我的过程? I want an interactive process but communicate does something so that I cannot take raw_input any more in my process. 我需要一个交互式过程,但交流会做一些事情,这样我就无法在过程中接受raw_input了。

from sys import stdin 
from threading import Thread
from time import sleep

if __name__ == '__main__':
    print("Still Running\n")
    x = raw_input()    
    i = 0
    while ('n' not in x ) :
        print("Still Running " + str(i) + " \r\n")
        x = raw_input()
        i += 1

    print("quit")



print(aSubProc.theProcess.communicate('y'))
print(aSubProc.theProcess.communicate('y'))

exception! 例外!

self.stdin.write(input)
ValueError: I/O operation on closed file

communicate and wait methods of Popen objects, close the PIPE after the process returns. communicatewait Popen对象的方法,请在过程返回后关闭PIPE If you want stay in communication with the process try something like this: 如果您想与流程保持联系,请尝试以下操作:

import subprocess
proc = subprocess.Popen("some_process", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
proc.stdin.write("input")
proc.stdout.readline()

Why does communicate kill my process? 为什么沟通会杀死我的过程?

From the docs for Popen.communicate(input=None, timeout=None) : Popen.communicate(input=None, timeout=None)的文档中:

Interact with process: Send data to stdin. 与进程交互:将数据发送到stdin。 Read data from stdout and 从stdout读取数据并
stderr, until end-of-file is reached. stderr,直到到达文件末尾。 Wait for process to terminate. 等待进程终止。 emphasize mine 强调我的

You may call .communicate() only once. 您只能调用.communicate()一次。 It means that you should provide all input at once: 这意味着您应该一次提供所有输入:

#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE

p = Popen([sys.executable, 'child.py'], stdin=PIPE, stdout=PIPE)
print p.communicate(os.linesep.join('yyn'))[0]

Output 产量

Still Running

Still Running 0 

Still Running 1 

quit

Notice the doubled newlines: one from '\\r\\n' and another from print statement itself in your script for the child process. 注意双换行符:在子进程的脚本中,一个来自'\\r\\n' ,另一个来自print语句本身。

Output shows that the child process received three input lines successfully ( 'y' , 'y' , and 'n' ). 输出显示,子进程成功接收了三行输入( 'y''y''n' )。

Here's a similar code using subprocess.check_output() 's input parameter from Python3.4: 这是使用Python3.4中subprocess.check_output subprocess.check_output()input参数的类似代码:

#!/usr/bin/env python3.4
import os
import sys
from subprocess import check_output

output = check_output(['python2', 'child.py'], universal_newlines=True,
                      input='\n'.join('yyn'))
print(output, end='')

It produces the same output. 它产生相同的输出。


If you want to provide a different input depending on responses from the child processes then use pexpect module or its analogs to avoid issues mentioned in Why not just use a pipe (popen())? 如果要根据子进程的响应提供不同的输入,请使用pexpect模块或其类似物来避免为什么不仅仅使用管道(popen())中提到的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM