[英]How do I create a subprocess in Python?
I would like to create a subprocess of a process. 我想创建一个进程的子进程。
What would be a working example which shows how to accomplish this? 什么是一个工作示例,说明如何实现这一目标?
Start with the subprocess documentation. 从子进程文档开始。
If you want to get the output: 如果你想获得输出:
>>> import subprocess
>>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]
>>> output
'Linux'
If you just want to call and not deal with the output: 如果你只想打电话而不是处理输出:
>>> subprocess.call(['echo', 'Hi'])
Hi
0
subprocess.check_call is the same except that it throws up a CalledProcessError
in case the command is called with invalid parameters. subprocess.check_call是不同之处在于它引发了一个相同的CalledProcessError
在情况下命令被称为无效的参数。
A good subprocess tutorial . 一个很好的子流程教程 。
import subprocess
subprocess.call(['echo', 'hello world'])
This is what worked for me if you want to run a simple command instead of giving a seperate file 如果你想运行一个简单的命令而不是给出一个单独的文件,这对我有用
import subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
print process.returncode
To get returncode of process you can use process.returncode
To get response you can use process.communicate()
要获取进程的返回码,可以使用process.returncode
要获得响应,可以使用process.communicate()
in case if you are confuse you can just test this code by using command="ls"
如果您感到困惑,可以使用command="ls"
测试此代码
if you are getting returncode
other than 0
then you can check here what that error code means: http://tldp.org/LDP/abs/html/exitcodes.html 如果您获得的returncode
不是0
那么您可以在此处查看错误代码的含义: http : //tldp.org/LDP/abs/html/exitcodes.html
For more details about Subprocess: http://docs.python.org/library/subprocess.html 有关Subprocess的更多详细信息: http : //docs.python.org/library/subprocess.html
Launching and monitoring a subprocess: 启动和监控子流程:
import subprocess, time, os, signal
args=['/usr/bin/vmstat','-n','2']
app=subprocess.Popen(args=args, stdout=open('somefile','w'))
print "Your app's PID is %s. You can now process data..." % app.pid
time.sleep(5)
if app.poll() == None: print "Process is still running after 5s."
print "The app outputed %s bytes." % len(open('somefile','r').read())
print "Stopping the process..."
os.kill(app.pid, signal.SIGTERM)
There is more to it. 还有更多。 Just check the Popen docs. 只需查看Popen文档。
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
subprocess.call(os.popen(tempFileName), shell=True)
os.remove(tempFileName)
Based on user225312's answer, I prepared the below one liner, it may help you to test the subprocess: 根据user225312的答案,我准备了下面的一个班轮,它可以帮助你测试子过程:
python -c "import subprocess;
output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0];
print output"
result like: Linux xxx.xxx.xxx.xxx 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
结果如: Linux xxx.xxx.xxx.xxx 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.