Running several programs from one program

2019-09-14 17:42发布

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条回答
家丑人穷心不美
2楼-- · 2019-09-14 18:18

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.

查看更多
登录 后发表回答