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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With