2017-05-26 58 views
0

我想执行一个长时间运行的Python 2.7 CGI脚本异步并返回一个完整的HTML浏览器,所以它不超时(并没有等待脚本完成)。 ..我运行在Windows XAMPP和缩写代码是异步Python CGI调用暂停浏览器直到完成

我的问题是浏览器仍然等待,直到整个脚本完成...我做错了什么?我读过其他类似的问题,他们评论说,添加stdout和stderr参数可能会解决这个问题,但它不适用于我...我也尝试设置close_fds = True并消除stdout/sterr参数,这也不起作用... script.py工作正常,没有任何输出...

或者还有另一种方法,你会推荐?感谢您提供任何帮助!

#!c:\program files\anaconda2\python.exe 
import cgi 
import subprocess 
import sys 

subprocess.Popen([sys.executable, 'c:/path/script.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 

print 'Content-type:text/html\r\n\r\n' 
print '<html>' 
print '<head></head>' 
print '<body></body>' 
+0

[使用ASYNCIO做在Django周期性任务(https://stackoverflow.com/questions/43838872/using-asyncio-to-do-periodic-task-in-django) – e4c5

+0

的可能的复制洛尔,我不是建议你使用asyncio。请花点时间阅读答案 – e4c5

+0

谢谢!我希望有一些东西比芹菜轻一些,因为它很少运行,所有的基础设施似乎有点多。你可能会推荐另一种解决方案吗? –

回答

0

Popen有一些标志将父进程从父进程中分离出来...即使子进程仍在运行,它也允许cgi“完成”。

kwargs = {} 
CREATE_NEW_PROCESS_GROUP = 0x00000200 # note: could get it from subprocess 
DETACHED_PROCESS = 0x00000008   # 0x8 | 0x200 == 0x208 
kwargs.update(creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) 
subprocess.Popen([sys.executable, 'c:/path/script.py'], close_fds=True, **kwargs) 
相关问题