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.

bosnjak
  • 8,424
  • 2
  • 21
  • 47
user3265370
  • 121
  • 1
  • 2
  • 12
  • This question seems related: http://stackoverflow.com/questions/2176053/python-execute-multiple-scripts-simultaneously-from-same-interpreter – skamsie Mar 25 '14 at 09:00

1 Answers1

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.

bosnjak
  • 8,424
  • 2
  • 21
  • 47
  • which method you prefer? – user3265370 Mar 25 '14 at 10:23
  • 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? – user3265370 Mar 25 '14 at 10:24
  • i should put #!/usr/bin/env python in every script on the first line? – user3265370 Mar 25 '14 at 11:08
  • [Errno 2] No such file or directory – user3265370 Mar 25 '14 at 11:14
  • I have tried this myself and it works. You are doing something differently. Please paste your source code. – bosnjak Mar 25 '14 at 11:14
  • actually it running the programs and giving the results but at the top it gives this error and then starts. its weird. what can be the problem? – user3265370 Mar 25 '14 at 11:17
  • I can not know unless you paste your code. There might be one file whose name you misspelled or something. Check the actual output, which file can't be found, and make sure it exists in the directory. – bosnjak Mar 25 '14 at 11:19
  • it runs but when i try to cancel the built in sublime, it doesnt stop. the program is keep inputing results in my database? – user3265370 Mar 25 '14 at 11:37
  • Change the line to `subprocess.Popen([os.path.join(os.getcwd(), s)])` as in my answer. Remove the "python" argument. But this is not the solution to your problem, and we can take this communication to that other thread. – bosnjak Mar 25 '14 at 11:50
  • 1
    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). – jfs Mar 25 '14 at 16:31
  • @J.F.Sebastian: initially i didn't, but then i guessed it would be more complete to include them since its likely that the script will read from the pipe, but you are right, it is out of the scope of my answer, so i reverted to previous revision. – bosnjak Mar 25 '14 at 23:12
  • +1. Note: there are several issues with how you are trying to specify the commands to run. [Here's code example that show how it could be done instead](http://stackoverflow.com/a/22640720/4279): try to find usage examples when your code fails (symlinks, parent started as `-mparent`, etc) and see if my code continues to work. – jfs Mar 26 '14 at 07:34