我有以下Python代碼片段,無法解釋爲什麼它的行爲方式如此。無法使用popen啓動兩個交互式shell
import subprocess
bash1 = subprocess.Popen(["/bin/bash","-l", "-i"], stdin=subprocess.PIPE)
print "Checkpoint 1"
bash2 = subprocess.Popen(["/bin/bash","-l", "-i"], stdin=subprocess.PIPE)
print "Checkpoint 2"
bash1.communicate("echo 'works1'")
bash2.communicate("echo 'works2'")
print "OK"
當我運行它,我得到以下的輸出:
[[email protected] ~]$ python test.py
Checkpoint 1
Checkpoint 2
[1]+ Stopped python test.py
[[email protected] ~]$ [[email protected] ~]$ echo 'works1'
works1
[[email protected] ~]$ logout
[[email protected] ~]$ fg
python test.py
[[email protected] ~]$ echo 'works2'
works2
[[email protected] ~]$ logout
OK
[[email protected] ~]$
- 爲什麼Python進程停止在第二POPEN電話嗎? (由tty輸入停止),以及如何避免它?
- 爲什麼在echo'works1'完成後我會收到註銷消息,以及如何避免它?
您是否嘗試在通信調用結束時添加換行符? 'bash1.communicate(「echo'works1'\ n」)'。 – Bakuriu 2013-02-18 17:27:53
我試過了,但結果是一樣的。 – 2013-02-18 17:33:51