BaRud
BaRud

Reputation: 3218

run 2 or more subprocess call separately

I have two different executable (run1 and run2). I want to run them simultaneously. They share one readonly input file, but otherwise, they are different process. Is it possible to call the call's simultaneously? Currently, as I have coded, cmd2's call wait for cmd1's call to complete.

with open(dosout, "w") as dout, open(jijout, "w") as jout:
    cmd1 = ["mpirun", "-np", "8", "~/WORK/run1", dosinp]
    cmd2 = ["mpirun", "-np", "8", "~/WORK/run2", jijinp]
    call(cmd1, stdout=dout)
    call(cmd2, stdout=jout)
dout.close()
jout.close()

Is it possible with call? or Popen from this answer is the only (or better) way out?

Upvotes: 0

Views: 540

Answers (2)

jfs
jfs

Reputation: 414159

To run two subprocesses concurrently:

from subprocess import Popen

# start processes
with open(dosout, "wb", 0) as dout, open(jijout, "wb", 0) as jout:
    processes = [Popen(["mpirun", "-np", "8", "...WORK/run1", cmd], stdout=file)
                 for cmd, file in [(dosinp, dout), (jijinp, jout)]]

# wait for them to finish
statuses = [p.wait() for p in processes]

Note: the file are closed on exit from the with-statement, don't call .close() manually.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

You'll need to use Popen as described in the answer you linked (and quite a few other places as well, because Popen is a slightly lower-level interface which is often needed).

Upvotes: 1

Related Questions