如何有效地管理这些子流程?

发布于 2024-09-07 13:41:36 字数 325 浏览 0 评论 0原文

我有一个可以下载然后转换视频的脚本。我试图一次完成所有下载(很多 wget),然后在完成后进行转换。现在我必须等待每个文件单独下载,然后在完成后进行转换。我希望所有下载请求同时运行。

这是我的“下载”脚本中阻止一切的部分。

for id  in ids:
    subprocess.Popen(cmd, shell=True).wait()

我的“转换”脚本正在等待下载脚本完成。 (这是必要的,因为我正在使用一组下载来组织所有内容。)

我可以使用队列,但这已经让这变得足够大了,我希望有一个更简单的解决方案。

谢谢

I've got a script that downloads and then converts videos. I'm trying to do all of the downloads at once(a lot of wgets) and then when they're done, convert them. Right now I have to wait while each file downloads individually and then convert when done. I want all the download requests to run concurrently.

Here's the part in my 'download' script that is holding everything up.

for id  in ids:
    subprocess.Popen(cmd, shell=True).wait()

I have my 'convert' script waiting on the download script to finish. (which is necessary, because i'm using sets of downloads to organize everything.)

I could use a queue but this I've already made this a big enough mess and I'm hoping there is a simpler solution.

Thanks

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

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

发布评论

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

评论(2

树深时见影 2024-09-14 13:41:36

您可以

process_list = []    
for id  in ids:
    process_list.append(subprocess.Popen(cmd, shell=True))
for process in process_list:
    process.wait()

这样使用,您将在同时作业结束时等待。

you could use

process_list = []    
for id  in ids:
    process_list.append(subprocess.Popen(cmd, shell=True))
for process in process_list:
    process.wait()

as such you will wait just at the end of simultaneous jobs.

梦在夏天 2024-09-14 13:41:36

如果您的平台可用,您可以使用 xargs 作为简单的解决方案。

从手册页来看:

   -P max-procs
          Run  up  to max-procs processes at a time; the default is 1.  If
          max-procs is 0, xargs will run as many processes as possible  at
          a  time.   Use the -n option with -P; otherwise chances are that
          only one exec will be done.

当然有更好的解决方案,但如果您需要一个快速的解决方案,这可能是最容易实现的。

You could use xargs as a simple solution, if that is availabe for your platform.

From the man page:

   -P max-procs
          Run  up  to max-procs processes at a time; the default is 1.  If
          max-procs is 0, xargs will run as many processes as possible  at
          a  time.   Use the -n option with -P; otherwise chances are that
          only one exec will be done.

There are certainly better solutions, but if you need a quick one this is probably the easiest to implement.

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