您的位置:首页 > 编程语言 > Python开发

Python使用subprocess.Popen导致子进程hang住

2013-02-05 19:05 567 查看
subprocess用于在python内部创建一个子进程,比如调用shell脚本等。

举例:

p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stdin = subprocess.PIPE, shell = True)
p.wait()
// hang here
print "subprocess finished"


在python的官方文档中对这个进行了解释:http://docs.python.org/2/library/subprocess.html

原因是stdout产生的内容太多,超过了系统的buffer

解决方法是使用communicate()方法。

p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stdin = subprocess.PIPE, shell = True)
stdout, stderr = p.communicate()
p.wait()
print "subprocess finished"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python