Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run several python programs at the same time

Tags:

python

bash

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

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

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

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

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

like image 553
imkost Avatar asked Aug 26 '12 00:08

imkost


People also ask

How many python programs can run at the same time?

2 Answers. Show activity on this post. You can run only one Python application per interpreter, and you can only run one interpreter per process. If you want to run multiple applications then you will need to run multiple processes.

Can PyCharm run multiple scripts?

With PyCharm, you can run entire applications as well as particular scripts.

How do I run a python program in parallel?

Multiprocessing in Python enables the computer to utilize multiple cores of a CPU to run tasks/processes in parallel. Multiprocessing enables the computer to utilize multiple cores of a CPU to run tasks/processes in parallel.


1 Answers

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

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).

like image 82
David Robinson Avatar answered Oct 05 '22 09:10

David Robinson