相关疑难解决方法(0)

了解Popen.communicate

我有一个名为的脚本1st.py,它创建了一个REPL(read-eval-print-loop):

print "Something to print"
while True:
    r = raw_input()
    if r == 'n':
        print "exiting"
        break
    else:
        print "continuing"
Run Code Online (Sandbox Code Playgroud)

然后我1st.py使用以下代码启动:

p = subprocess.Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)
Run Code Online (Sandbox Code Playgroud)

然后尝试了这个:

print p.communicate()[0]
Run Code Online (Sandbox Code Playgroud)

它失败了,提供了这个追溯:

Traceback (most recent call last):
  File "1st.py", line 3, in <module>
    r = raw_input()
EOFError: EOF when reading a line
Run Code Online (Sandbox Code Playgroud)

你能解释一下这里发生了什么吗?当我使用时p.stdout.read(),它会永远挂起.

python subprocess communicate

56
推荐指数
2
解决办法
15万
查看次数

标签 统计

communicate ×1

python ×1

subprocess ×1