Run python script as background process from inside python script

My python script should start a background process and then continue processing until it is complete, without waiting for a return.

The background script will be processed for some time and will not generate any screen output.

No data is required between processes.

I tried using various method subprocesses, multiprocessing, but I obviously missed something.

Does anyone have a simple example?

TIA

+4
source share
3 answers

how about this:

import subprocess from multiprocessing import Process Process(target=subprocess.call, args=(('ls', '-l', ), )).start() 

This is not all elegant, but it fulfills all your requirements.

+3
source

Plain:

 subprocess.Popen(["background-process", "arguments"]) 

If you want to check later if the background process has completed its task, save the link to the Popen object and use the poll() method.

+2
source

There is a good record of the various parts / parts on how to do this in Invoking an External Command in Python (per @lecodesportif).

The essence of the quick answer is:

 retcode = subprocess.call(["ls", "-l"]) 
+1
source

All Articles