Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple python scripts concurrently

How can I run multiple python scripts? At the moment I run one like so python script1.py.

I've tried python script1.py script2.py and that doesn't work: only the first script is run. Also, I've tried using a single file like this;

import script1 import script2  python script1.py python script2.py 

However this doesn't work either.

like image 633
Sami Avatar asked Feb 16 '15 20:02

Sami


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 you run multiple instances of python?

You can certainly run multiple instances of the same python script (and the shell script too) -- each runs as a separate process, with its own memory, variables, etc.


2 Answers

With Bash:

python script1.py & python script2.py & 

That's the entire script. It will run the two Python scripts at the same time.

Python could do the same thing itself but it would take a lot more typing and is a bad choice for the problem at hand.

I think it's possible though that you are taking the wrong approach to solving your problem, and I'd like to hear what you're getting at.

like image 126
Christopher Peterson Avatar answered Sep 23 '22 18:09

Christopher Peterson


The simplest solution to run two Python processes concurrently is to run them from a bash file, and tell each process to go into the background with the & shell operator.

python script1.py & python script2.py & 

For a more controlled way to run many processes in parallel, look into the Supervisor project, or use the multiprocessing module to orchestrate from inside Python.

like image 31
logc Avatar answered Sep 23 '22 18:09

logc