Python子进程:在开始下一个之前等待命令完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17190031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python subprocess: wait for command to finish before starting next one?
提问by Richard
I've written a Python script that downloads and converts many images, using wget and then ImageMagick via chainedsubprocesscalls:
我编写了一个 Python 脚本,可以通过链接subprocess调用使用 wget 和 ImageMagick 下载和转换许多图像:
for img in images:
convert_str = 'wget -O ./img/merchant/download.jpg %s; ' % img['url']
convert_str += 'convert ./img/merchant/download.jpg -resize 110x110 '
convert_str += ' -background white -gravity center -extent 110x110'
convert_str += ' ./img/thumbnails/%s.jpg' % img['id']
subprocess.call(convert_str, shell=True)
If I run the content of convert_strmanually at the command line, it appears to work without any errors, but if I run the script so it executes repeatedly, it sometimes gives me the following output:
如果我convert_str在命令行手动运行内容,它似乎可以正常工作,但如果我运行脚本使其重复执行,它有时会给我以下输出:
--2013-06-19 04:01:50--
http://www.lkbennett.com/medias/sys_master/8815507341342.jpg
Resolving www.lkbennett.com... 157.125.69.163
Connecting to www.lkbennett.com|157.125.69.163|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22306 (22K) [image/jpeg]
Saving to: `/home/me/webapps/images/m/img/merchant/download.jpg'
0K .......... .......... . 100% 1.03M=0.02s
2013-06-19 04:01:50 (1.03 MB/s) -
`/home/annaps/webapps/images/m/img/merchant/download.jpg' saved [22306/22306]
/home/annaps/webapps/images/m/img/merchant/download.jpg
[Errno 2] No such file or directory:
' /home/annaps/webapps/images/m/img/merchant/download.jpg'
Oddly, despite the No such file or directorymessage, the images generally seem to have downloaded and converted OK. But occasionally they look corrupt, with black stripeson them (even though I'm using the latest version of ImageMagick), which I assume is because they aren't completely downloaded before the command executes.
奇怪的是,尽管有No such file or directory消息,但图像通常似乎已下载并转换为 OK。但有时它们看起来很损坏,上面有黑色条纹(即使我使用的是最新版本的 ImageMagick),我认为这是因为它们在命令执行之前没有完全下载。
Is there any way I can say to Python or to subprocess: "don't run the second command until the first has definitely completed successfully?". I found this questionbut can't see a clear answer!
我有什么办法可以对 Python 或说subprocess:“在第一个命令绝对成功完成之前不要运行第二个命令?”。我发现了这个问题,但看不到明确的答案!
采纳答案by Sylvain Leroux
Normally, subprocess.callis blocking.
通常,subprocess.call是阻塞的。
If you want non blockingbehavior, you will use subprocess.Popen. In that case, you have to explicitly use Popen.waitto wait for the process to terminate.
如果您想要非阻塞行为,您将使用subprocess.Popen. 在这种情况下,您必须显式使用Popen.wait等待进程终止。
See https://stackoverflow.com/a/2837319/2363712
见https://stackoverflow.com/a/2837319/2363712
BTW, in shell, if you wish to chain process you should use &&instead of ;-- thus preventing the second command to be launched if the first one failed. In addition, you should test the subprocess exit status in your Python program in order to determine if the command was successful or not.
顺便说一句,在 shell 中,如果您希望链接进程,则应使用&&而不是;-- 从而防止在第一个命令失败时启动第二个命令。此外,您应该在 Python 程序中测试子进程退出状态,以确定该命令是否成功。
回答by bnlucas
See Using module 'subprocess' with timeout
Not sure if this is the properway of doing it, but this is how I accomplish this:
不确定这是否是proper这样做的方式,但这就是我实现此目的的方式:
import subprocess
from threading import Thread
def call_subprocess(cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
if err:
print err
thread = Thread(target=call_subprocess, args=[cmd])
thread.start()
thread.join() # waits for completion.

