Tag:

如何终止用shell = True启动的pythonsubprocess

我使用以下命令启动subprocess: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 但是,当我试图杀死使用: p.terminate() 要么 p.kill() 该命令一直在后台运行,所以我想知道如何才能真正终止进程。 请注意,当我运行命令: p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) 发出p.terminate()时,它终止成功。

检索subprocess.call()的输出

我怎样才能得到使用subprocess.call()进程运行的输出? 将一个StringIO.StringIO对象传递给stdout会产生这样的错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call return Popen(*popenargs, **kwargs).wait() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles c2pwrite = stdout.fileno() AttributeError: StringIO instance has no attribute 'fileno' >>>