0

I have 12 programs which i intend to run simultaneously. Is there any way i run all of them via one single program which when built runs the 12 programs?

I am using sublime and the programs are python.

1

1 Answer 1

3

If you just want to execute the programs one by one in a batch, you can do it in a bash script. Assuming they are executables in the same folder, you can have an .sh file with the following contents:

#!/bin/bash
python ./my_app1.py &
python ./my_app2.py &
python ./my_app3.py

If the scripts themselves have the #!/usr/bin/env python at the top to identify the interpreter, you can do chmod +x on them, and turn your runner.sh file into:

#!/bin/bash
./my_app1.py &
./my_app2.py &
./my_app3.py

On the other hand, if you want to do this from a python script, you can use:

import subprocess
import os

scripts_to_run = ['my_app1.py', 'my_app2.py', 'my_app3.py']

for s in scripts_to_run:
    subprocess.Popen([os.path.join(os.getcwd(), s)])

Note 1: don't forget to include the #!/usr/bin/env python in every script on the first line.
Note 2: it is important to use subprocess.Popen() instead subprocess.call() because the latter is a blocking function that will wait for the application to finish before proceeding. With subproces.Popen() you get the concurrent execution.

Sign up to request clarification or add additional context in comments.

12 Comments

which method you prefer?
I prefer the python script one because i have no experience in the bash programming. One question:my_app1.py have to be in the same directory? there is no location information needed?
i should put #!/usr/bin/env python in every script on the first line?
[Errno 2] No such file or directory
Do not set stdout, stderr to PIPE unless you read from them eventually. It may lead to a deadlock if any of the scripts generates enough output. Don't introduce data dependant bugs; it is worse even if your code just failed immediately (at least it would shows that there is a problem).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.