2016-10-06 40 views
0

这个问题的子进程通信的多个时间不是可以用Python

Communicate multiple times with a process without breaking the pipe?

重复这个问题就解决了,因为它的使用情况下输入可以一起发送,但如果你的程序,这是不正确的是交互式的(如这里的用例所示)。


文件subprocess.Popen说:

communicate(input=None) 
    Interact with process: Send data to stdin. Read data from stdout 
    and stderr, until end-of-file is reached. Wait for process to 
    terminate. ... 

是否有可能终止其与前一个终端或网络套接字与子多次沟通,怎么样?

例如,如果子进程为bc,则父进程可能希望根据需要发送不同的输入进行计算。由于输入发送到bc可能取决于用户输入,所以不可能一次发送所有输入。

回答

1

Basicly Non-blocking read on a subprocess.PIPE in python

经由fnctl设置PROC管(proc.stdout,proc.stdin,...)到非阻塞模式,然后写/直接读取它们。

您可能想要使用epoll或通过selectio模块进行选择以获得更高的效率。

0

这原来是不是很困难:

proc = subprocess.Popen(['bc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 
os.write(proc.stdin.fileno(), b'100+200\n') 
print(os.read(proc.stdout.fileno(), 4096))