且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python-从Web应用程序启动长时间运行的进程

更新时间:2022-11-06 14:53:22

好,我终于明白了!这似乎可行:

Ok, I finally figured this out! This seems to work:

from subprocess import Popen
from win32process import DETACHED_PROCESS

pid = Popen(["C:\python24\python.exe", "long_run.py"],creationflags=DETACHED_PROCESS,shell=True).pid
print pid
print 'done' 
#I can now close the console or anything I want and long_run.py continues!

注意:我添加了shell = True.否则在子进程中调用print会给我错误"IOError:[Errno 9]错误的文件描述符"

Note: I added shell=True. Otherwise calling print in the child process gave me the error "IOError: [Errno 9] Bad file descriptor"

DETACHED_PROCESS过程创建标志,该标志传递给基础WINAPI

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