Search code examples
pythonspawn

Python: Spawning another program


I have a Python program from which I spawn a sub-program to process some files without holding up the main program. I'm currently using bash for the sub-program, started with a command and two parameters like this:

result = os.system('sub-program.sh file.txt file.txt &')

That works fine, but I (eventually!) realised that I could use Python for the sub-program, which would be far preferable, so I have converted it. The simplest way of spawning it might be:

result = os.system('python3 sub-program.py file.txt file.txt &')

Some research has shown several more sophisticated alternatives, but I have the impression that the latest and most approved method is this one:

subprocess.Popen(["python3", "-u", "sub-program.py"])

Am I correct in thinking that that is the most appropriate way of doing it? Would anyone recommend a different method and why? Simple would be good as I'm a bit of a Python novice.

If this is the recommended method, I can probably work out what the "-u" does and how to add the parameters for myself.

Optional extras:

  • Send a message back from the sub-program to the main program.
  • Make the sub-program quit when the main program does.

Solution

  • Thanks skyking!

    With

    import subprocess
    

    at the beginning of the main program, this does what I want:

    with open('output.txt', 'w') as f:   
      subprocess.Popen([spawned.py, parameter1, parameter2], stdout = f)
    

    The first line opens a file for the output from the sub-program started in the second line. In the second line, the square brackets contain the stuff for the sub-program - name followed by two parameters. The parameters are available in the sub-program in sys.argv[1] and sys.argv[2]. After that come the subprocess parameters - the f says to output to the text file mentioned above.