5

My python script needs to start a background process and then continue processing to completion without waiting for a return.

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

There is no inter-process data required.

I have tried using various methods subprocess, multiprocessing but am clearly missing something.

Does anyone have a simple example?

TIA

user434315
  • 53
  • 1
  • 3

3 Answers3

3

how about this:

import subprocess
from multiprocessing import Process

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

It's not all that elegant, but it fulfils all your requirements.

Stefano Palazzo
  • 3,905
  • 1
  • 24
  • 39
2

Simple:

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

If you want to check later whether the background process completed its job, retain a reference to the Popen object and use it's poll() method.

Marius Gedminas
  • 10,342
  • 3
  • 37
  • 38
1

There is a nice writeup of the various pieces/parts on how to do it at Calling an external command in Python (per @lecodesportif).

The gist of a quick answer is:

retcode = subprocess.call(["ls", "-l"])
Community
  • 1
  • 1
heckj
  • 6,120
  • 2
  • 31
  • 45