15

I am trying to create 86 instances of task.py to run simultaneously.

import sys
import subprocess

for file in range(86):
    subprocess.call([sys.executable,'task.py',str(file)+'in.csv',str(filen)+'out.csv'])
2
  • 8
    What is the problem? Commented Oct 3, 2013 at 10:13
  • 2
    @Alex the problem was "subprocess.call waits for command to complete. Use subprocess.Popen instead:" Commented Oct 3, 2013 at 10:59

1 Answer 1

22

subprocess.call waits for command to complete. Use subprocess.Popen instead:

import sys
import subprocess

procs = []
for i in range(86):
    proc = subprocess.Popen([sys.executable, 'task.py', '{}in.csv'.format(i), '{}out.csv'.format(i)])
    procs.append(proc)

for proc in procs:
    proc.wait()
Sign up to request clarification or add additional context in comments.

8 Comments

Is this solution a "multithread" solution? Or actually it launches multiple instance of python?
@Lisa, As the name of the module suggest, it launches multiple instances (sub"process"). (OP's original code also launches multiple instances)
I was just searching for something like this for so much time. Thank you
I can't get it to work when looping and appending: out =subprocess.Popen("hugo server -D", stdout = subprocess.PIPE) outputs.append(out) And then iterating through outputs and calling output.wait(). It opens up the first program, but then doesn't open up the next one unless I close the first program.
@MasayoMusic, Replace "hugo server -D" -> ['hugo', 'server', '-D']. If that does not work, please post a separate question so that others can answer you.
|

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.