Popen
itself already does exactly what you are trying to accomplish; it starts a new process and allows it to run in parallel with your Python script.
In some more detail, you are apparently looking for something like
predictor = subprocess.Popen(
["python", "predictor.py", "--port", "8086"])
... # other python code here
result = predictor.communicate()
You'll need to take care of waiting for the process you started, so probably put the communicate
(or wait
etc) in the code which runs when your script finishes.
For what it's worth, &
is a shell feature; you could accomplish the same thing more crudely and expensively by (taking out the split
and) using shell=True
; but you generally want to avoid that when you can. See also Actual meaning of shell=True
in subprocess
Notice also that " ".split()
is only suitable for the most trivial cases. You want to use shlex.split()
instead; or, as here, simply manually split the command line into tokens yourself.
I seriously doubt that nohup
makes sense here, so I took it out. It's usually not a great idea to run Python explicitly as a subprocess of itself; perhaps instead look into the multiprocessing
library which lets you run an individual Python function in a separate process, with more fine-grained control over it.