在同一时间运行多个Python程序 [英] Run several python programs at the same time

查看:127
本文介绍了在同一时间运行多个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程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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