如何在 Python 中创建子进程?

发布于 2024-10-08 14:24:56 字数 48 浏览 3 评论 0原文

我想创建一个流程的子流程。

展示如何实现这一目标的工作示例是什么?

I would like to create a subprocess of a process.

What would be a working example which shows how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东风软 2024-10-15 14:24:56

subprocess 文档开始。

如果你想获得输出:

>>> import subprocess
>>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]
>>> output
'Linux'

如果你只想调用而不处理输出:

>>> subprocess.call(['echo', 'Hi'])
Hi
0

subprocess.check_call 是相同的,只是它会在使用无效参数调用命令时抛出 CalledProcessError

很好的子流程教程

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.

A good subprocess tutorial.

晨曦÷微暖 2024-10-15 14:24:56

启动和监视子流程:

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)

还有更多内容。只需检查 Popen 文档即可。

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.

灯下孤影 2024-10-15 14:24:56
import subprocess

subprocess.call(['echo', 'hello world'])
import subprocess

subprocess.call(['echo', 'hello world'])
恏ㄋ傷疤忘ㄋ疼 2024-10-15 14:24:56

如果您想运行一个简单的命令而不是提供一个单独的文件,这对我有用。

import subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
print process.returncode

要获取进程的返回码,您可以使用process.returncode
要获得响应,您可以使用process.communicate()

,以防万一您感到困惑,

如果您收到, 您可以使用command="ls"来测试此代码>returncode 除了 0 之外,那么您可以在此处检查该错误代码的含义:http://tldp.org/LDP/abs/html/exitcodes.html

有关子进程的更多详细信息:http://docs.python.org/library/subprocess.html

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()

in case if you are confuse you can just test this code by using 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

For more details about Subprocess: http://docs.python.org/library/subprocess.html

长发绾君心 2024-10-15 14:24:56
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
subprocess.call(os.popen(tempFileName), shell=True)
os.remove(tempFileName)
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
subprocess.call(os.popen(tempFileName), shell=True)
os.remove(tempFileName)
許願樹丅啲祈禱 2024-10-15 14:24:56

根据user225312的回答,我准备了下面的一个衬垫,它可以帮助您测试子进程:

python -c "import subprocess;
output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]; 
print output"

结果如下:
Linux xxx.xxx.xxx.xxx 3.10.0-957.1.3.el7.x86_64 #1 SMP 11 月 29 日星期四 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Based on user225312's answer, I prepared the below one liner, it may help you to test the subprocess:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文