Python popen command. Wait until the command is finished(Python 弹出命令.等到命令完成)
问题描述
我有一个脚本,我用 popen 一个 shell 命令启动.问题是脚本不会等到 popen 命令完成并立即继续执行.
I have a script where I launch with popen a shell command. The problem is that the script doesn't wait until that popen command is finished and go continues right away.
om_points = os.popen(command, "w")
.....
如何告诉我的 Python 脚本等到 shell 命令完成?
How can I tell to my Python script to wait until the shell command has finished?
推荐答案
根据您希望如何处理脚本,您有两种选择.如果您希望命令在执行时阻止而不做任何事情,您可以使用 subprocess.call
.
Depending on how you want to work your script you have two options. If you want the commands to block and not do anything while it is executing, you can just use subprocess.call
.
#start and block until done
subprocess.call([data["om_points"], ">", diz['d']+"/points.xml"])
如果你想在它执行的时候做一些事情或者把事情输入到 stdin
中,你可以在 popen
调用之后使用 communicate
.
If you want to do things while it is executing or feed things into stdin
, you can use communicate
after the popen
call.
#start and process things, then wait
p = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"])
print "Happens while running"
p.communicate() #now wait plus that you can send commands to process
如文档中所述,wait
可能会死锁,因此建议进行交流.
As stated in the documentation, wait
can deadlock, so communicate is advisable.
这篇关于Python 弹出命令.等到命令完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 弹出命令.等到命令完成


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01