4

This question stems from my lack of knowledge surrounding the structure of UNIX commands and the SUBPROCESS module, so please forgive my naivete in advance.

I have a command, that looks something like this

path/to/openmpi/mpirun -machinefile machine.file -np 256 /path/to/excecutable </dev/null &> output.out &

I know how the structure of MPIrun works, and I think my executable writes its data to stdout and I redirect it to a file called output.out. I have used this command in python scripts using os.sys(), but I would like to use subprocess so that when the executable finished running (in the background), the python script can resume doing 'things.'

I have no idea where to start, so if someone has any tips or can show me the proper way to format the subprocess command, I would be really grateful. All personal attempts at using subprocess result in epic failures.

Thanks!!!

Michael R
  • 259
  • 5
  • 16

1 Answers1

5

It's pretty straightforward.

from subprocess import call
call(["path/to/openmpi/mpirun", "-machinefile machine.file -np 256 /path/to/excecutable </dev/null &> output.out &"])

Generally, you'd provide the arguments to the command as a list, but I think this should work just as well. If not, break up each argument into a new element of the list.

This answer goes more into the limitations of this method.

Community
  • 1
  • 1
Jared Andrews
  • 272
  • 2
  • 12
  • I think this works, but how do I go about having subprocess wait for the executable to finish its task before moving on to other things? In this scenario, wouldn't subprocess think it was done as soon as it issues the command? – Michael R Jan 20 '16 at 19:32
  • 1
    From the Python docs: `subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)` "Run the command described by args. Wait for command to complete, then return the returncode attribute." So it should wait, I believe. – Jared Andrews Jan 20 '16 at 19:51