调用python子进程作为守护程序并退出 [英] Call a python subprocess as daemon and exit

查看:99
本文介绍了调用python子进程作为守护程序并退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一对python程序,其中一个应调用第二个。

I'm using a pair of python programs, one of which should call the second.

但这应该以第一个程序使第二个守护程序(或在后台进程中运行),然后退出,而无需等待第二个程序结束。

But this should be done in a way that the first program makes the second one a daemon (or running in the background process), then exits, without waiting for the second program to end.

在Python中这可能吗?

Is this possible in Python?

我一直在寻找os.fork子过程模块,但是我很困惑作为实现此目标的正确方法...

I've been looking at os.fork, subprocess module, but I'm quite confused as the correct way to achieve this...

推荐答案

您可以使用 subprocess.Popen 为此:

You can use subprocess.Popen for this:

import subprocess

cmd = ['/usr/bin/python', '/path/to/my/second/pythonscript.py']
subprocess.Popen(cmd)

您可能希望重定向 stdout stderr 在某个地方,您可以通过将 stdout =< file_obj> 传递到 Popen c来实现。导师。

You might want to redirect stdout and stderr somewhere, you can do that by passing stdout=<file_obj> to the Popen constructor.

这篇关于调用python子进程作为守护程序并退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆