Python子进程:在开始下一个命令之前等待命令完成? [英] Python subprocess: wait for command to finish before starting next one?

查看:635
本文介绍了Python子进程:在开始下一个命令之前等待命令完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Python脚本,使用wget下载并转换许多图像,然后通过链接子流程调用ImageMagick:

I've written a Python script that downloads and converts many images, using wget and then ImageMagick via chainedsubprocess calls:

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)

如果我在命令行手动运行 convert_str 的内容,它似乎没有任何错误,但如果我运行脚本使其重复执行,它有时会给我以下输出:

If I run the content of convert_str manually 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:

--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'

奇怪的是,尽管没有这样的文件或目录消息,图像通常似乎已下载并转换为OK。但偶尔他们看起来很腐败,有黑条纹他们(即使我使用的是最新版本的ImageMagick),我认为这是因为在命令执行之前它们没有完全下载。

Oddly, despite the No such file or directory message, the images generally seem to have downloaded and converted OK. But occasionally they look corrupt, with black stripes on 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.

我有什么办法可以对Python或 subprocess 说:不要运行第二个命令直到第一个肯定已成功完成?我发现了这个问题但是可以看不清楚答案!

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 question but can't see a clear answer!

推荐答案

通常 subprocess.call 正在阻止。

Normally, subprocess.call is blocking.

如果你想要非阻塞行为,你将使用 subprocess.Popen 。在这种情况下,您必须明确使用 Popen.wait 等待进程终止。

If you want non blocking behavior, you will use subprocess.Popen. In that case, you have to explicitly use Popen.wait to wait for the process to terminate.

参见 https://stackoverflow.com/a/2837319/2363712

BTW,在shell中,如果你想进行连锁处理,你应该使用&& 而不是; - - 因此,如果第一个命令失败,则阻止启动第二个命令。此外,您应该在Python程序中测试子进程退出状态,以确定命令是否成功。

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.

这篇关于Python子进程:在开始下一个命令之前等待命令完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆