As the two scripts are completely independent, just use subprocess.Popen()
:
import subprocess
script1 = subprocess.Popen(['/path/to/script1', 'arg1', 'arg2', 'etc'])
script2 = subprocess.Popen(['/path/to/script2', 'arg1', 'arg2', 'etc'])
That's it, both scripts are running in the background1. If you want to wait for one of them to complete, call script1.wait()
or script2.wait()
as appropriate. Example:
import subprocess
script1 = subprocess.Popen(['sleep', '30'])
script2 = subprocess.Popen(['ls', '-l'])
script1.wait()
You will find that script 2 will produce its output and terminate before script 1.
If you need to capture the output of either of the child processes then you will need to use pipes, and then things get more complicated.
1 Here "background" is distinct from the usual *nix notion of a background process running in a shell; there is no job control for example. WRT subprocess
, a new child process is simply being created and the requested executable loaded. No shell is involved, provided that shell=False
as per the default Popen()
option.