开发者

start python script as background process from within a python script [duplicate]

开发者 https://www.devze.com 2023-01-14 03:04 出处:网络
This question already has answers here: How to start a background process in Pytho开发者_如何学Pythonn?
This question already has answers here: How to start a background process in Pytho开发者_如何学Pythonn? (9 answers) Closed 9 years ago.

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


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.


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.


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"])
0

精彩评论

暂无评论...
验证码 换一张
取 消