2016-10-06 34 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))