在同一时间运行多个Python程序

编程入门 行业动态 更新时间:2024-10-13 02:13:59
本文介绍了在同一时间运行多个Python程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有python脚本的 run.py

I have python script run.py:

def do(i): # doing something with i, that takes time start_i = sys.argv[1] end_i = sys.argv[2] for i in range(start_i, end_i): do(i)

然后我运行此脚本:

Then I run this script:

python run.py 0 1000000

30分钟后脚本完毕。但是,它太长了我。

After 30 minutes script is completed. But, it's too long for me.

所以,我创建bash脚本的 run.sh

So, I create bash script run.sh:

python run.py 0 200000 & python run.py 200000 400000 & python run.py 400000 600000 & python run.py 600000 800000 & python run.py 800000 1000000

然后我运行此脚本:

Then I run this script:

bash run.sh

6分钟后脚本完毕。相当不错的。我很高兴。

After 6 minutes script is completed. Rather good. I'm happy.

不过,我认为,还有另一种方式来解决问题(而​​无需创建bash脚本),是不是有?

But I think, there is another way to solve the problem (without creating bash script), isn't there?

推荐答案

您正在寻找的多处理包,尤其是泳池类:

You're looking for the multiprocessing package, and especially the Pool class:

from multiprocessing import Pool p = Pool(5) # like in your example, running five separate processes p.map(do, range(start_i, end_i))

除了整合成一个单一的命令这个,这个还有其他优点在你调用蟒蛇run.py 0 200000和放大器的方法; 等,如果一些进程花费比别人更长(因此,蟒蛇run.py 0 200000 可能别人之前完成),这将确保所有5个线程继续工作,直到所有的人都做了。

Besides consolidating this into a single command, this has other advantages over your approach of calling python run.py 0 200000 & etc. If some processes take longer than others (and therefore, python run.py 0 200000 might finish before the others), this will make sure all 5 threads keep working until all of them are done.

请注意,根据您的计算机的体系结构,同时运行太多进程可能会降低这些键(对于新手,​​这取决于有多少个核的处理器,以及你是在同一时间运行还有什么)。

Note that depending on your computer's architecture, running too many processes at the same time might slow them all down (for starters, it depends on how many cores your processor has, as well as what else you are running at the same time).

更多推荐

在同一时间运行多个Python程序

本文发布于:2023-11-24 00:24:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   时间   程序   在同一   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!