Python 用子流程模块替换popen2模块
如何更正此代码以使用子流程模块替换popen2Python 用子流程模块替换popen2模块,python,Python,如何更正此代码以使用子流程模块替换popen2 popen3 = popen2.Popen3(cmd, capturestderr=True) rc = popen3.wait() if os.WIFEXITED(rc): rc = os.WEXITSTATUS(rc) if rc < 0: #""" Needed to make sure that catastrophic errors are not processed he
popen3 = popen2.Popen3(cmd, capturestderr=True)
rc = popen3.wait()
if os.WIFEXITED(rc):
rc = os.WEXITSTATUS(rc)
if rc < 0:
#""" Needed to make sure that catastrophic errors are not processed here, hence the rc check.
#"""
if len(stderr) > 0:
inserts= []
inserts.append("Warnings occurred during run of %s" % self.__MODULE_NAME )
inserts.append("Check conversion parameters.")
#self.msgWrite( "98000001", inserts )
if rc == 0:
self.msgDebug("CompartService exited normally", "Exit code with signal: %s" % str(rc))
#
简单,
import subprocess
# the process
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# getting different parts of it.
stdout = proc.stdout.read()
stderr = proc.stderr.read()
rc = proc.wait()
具体到您的代码现在的样子:
from subprocess import Popen, PIPE
proc = Popen(cmd, stderr=PIPE)
rc = proc.wait()
# the rest of your code
如果stderr上存在大量通信量,您的示例将死锁。应该改为使用Popen.communicate。这是事实,但OP甚至没有使用它。我刚刚演示了subprocess.Popen。但是您演示了不正确的用法。无论OP是否使用它,它都会死锁。只要改变它来交流,我们都会快乐地回家。