【发布时间】:2012-07-18 18:47:33
【问题描述】:
我使用subprocess在两个进程之间交换数据
我编辑了一个repeat.py 文件:
此文件是来自http://www.doughellmann.com/PyMOTW/subprocess/的示例
import sys
sys.stderr.write('repeater.py: starting\n')
sys.stderr.flush()
while True:
next_line = sys.stdin.readline()
if not next_line:
break
sys.stdout.write(next_line)
sys.stdout.flush()
sys.stderr.write('repeater.py: exiting\n')
sys.stderr.flush()
在ipython中运行这个文件
In [1]: import subprocess
In [2]: f=subprocess.Popen(['python','~/repeat.py'],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
In [3]: f.stdin.write('teststs\n')
In [4]: f.communicate()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'teststs' is not defined
Out[4]: ('', None)
为什么没有定义teststs?
【问题讨论】:
-
@Sven Marnach 感谢您的评论,我删除
shell=True后运行正常@ -
很好——我把评论变成了答案。
标签: python multiprocessing subprocess