如何在Windows上从python启动守护进程? [英] How to start daemon process from python on windows?

查看:746
本文介绍了如何在Windows上从python启动守护进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本可以生成一个将无限期运行的进程吗?

Can my python script spawn a process that will run indefinitely?

我对python或生成的守护进程不太熟悉,所以我对此进行了总结:

I'm not too familiar with python, nor with spawning deamons, so I cam up with this:

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NEW_CONSOLE
subprocess.Popen(executable, close_fds = True, startupinfo = si)

该进程继续运行经过python.exe,但是一旦关闭cmd窗口,该进程便被关闭。

The process continues to run past python.exe, but is closed as soon as I close the cmd window.

推荐答案

使用答案 Janne Karila指出,这是一种可以运行一个流程的方法,该流程在其父级死亡时不会死亡,无需使用 win32process 模块。

Using the answer Janne Karila pointed out this is how you can run a process that doen't die when its parent dies, no need to use the win32process module.

DETACHED_PROCESS = 8
subprocess.Popen(executable, creationflags=DETACHED_PROCESS, close_fds=True)

DETACHED_PROCESS 流程传递到基础 noreferrer> CreateProcess 函数。

DETACHED_PROCESS is a Process Creation Flag that is passed to the underlying CreateProcess function.

这篇关于如何在Windows上从python启动守护进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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