如何在后台启动和运行外部脚本?

编程入门 行业动态 更新时间:2024-10-08 08:31:41
本文介绍了如何在后台启动和运行外部脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了这两种方法:

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

这两种方法都需要等到test.py完成后才阻塞主进程.我知道"nohup"可以胜任.有没有Python方法来启动test.py或任何其他Shell脚本并使它在后台运行?

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 running in background?

假设test.py是这样的:

Suppose test.py is like this:

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

os.system()或subprocess.Popen()都将阻塞主程序,直到显示1000000行输出为止.我想要的是让test.py静默运行并仅显示主程序输出.当test.py仍在运行时,主程序可能会退出.

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"]) 应该可以工作.

请注意,当您的主脚本退出时,该作业可能仍然会死.在这种情况下,请尝试subprocess.Popen(["nohup", "python", "test.py"])

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

更多推荐

如何在后台启动和运行外部脚本?

本文发布于:2023-11-05 10:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   后台   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!