开发者

How to launch and run external script in background? [duplicate]

This question already has answers here: How to start a background process in Python? (9 answers) Closed 9 years ago.

I tried these two methods:

os.system("python test.py")

subprocess.Popen("python test.py", shell=True)

Both approaches need to wait until test.py finishes which blocks main process. I know "nohup" can do the job. Is there a Python way to launch test.py or any other shell scripts and leave it runni开发者_JS百科ng in background?

Suppose test.py is like this:

for i in range(0, 1000000):
    print i

Both os.system() or subprocess.Popen() will block main program until 1000000 lines of output displayed. What I want is let test.py runs silently and display main program output only. Main program may quie while test.py is still running.


subprocess.Popen(["python", "test.py"]) should work.

Note that the job might still die when your main script exits. In this case, try subprocess.Popen(["nohup", "python", "test.py"])


os.spawnlp(os.P_NOWAIT, "path_to_test.py", "test.py")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜