歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python使用subprocess.Popen導致子進程hang住

Python使用subprocess.Popen導致子進程hang住

日期:2017/3/1 10:01:55   编辑:Linux編程

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"

Copyright © Linux教程網 All Rights Reserved