Python子进程Popen停顿CGI页面 [英] Python Subprocess Popen Stalling CGI Page

查看:84
本文介绍了Python子进程Popen停顿CGI页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的工具,我需要它来运行解析器并输出另一个分析日志。目前,我拥有它,以便通过Web界面使用。

I have a tool that I am working on and I need it to run a parser and also output another analysis log. Currently I have it so that it's through a web interface.


  1. 用户转到表格并提交文件名进行解析(系统中已存在的文件) )。

  2. 表单将信息提交到Python CGI脚本

  3. Python CGI脚本运行并产生一个子进程来运行解析。

  4. 解析器会找到适当的信息进行分析并生成子进程。

  1. User goes to the form and submits a filename for parsing (file already on system).
  2. Form submits information to Python CGI script
  3. Python CGI script runs and spawns a subprocess to run the parsing.
  4. Parser finds appropriate information for analysis and spawns subprocess also.

我正在使用

import subprocess
...
subprocess.Popen(["./program.py", input])

在我的代码中,并且我从文档中假定我们不等待子进程终止,而是继续运行脚本。我的CGI脚本启动了所有这些操作:

In my code and I assumed from documentation that we don't wait on the child process to terminate, we just keep running the script. My CGI script that starts all this does:

subprocess.Popen(["./program.py", input])
// HTML generation code
// Javascript to refresh after 1 second to a different page

HTML生成代码仅输出我们已处理请求的状态,然后javascript将页面刷新到主页。

The HTML generation code is to output just a status that we've processed the request and then the javascript refreshes the page to the main homepage.

问题

CGI页面一直挂起,直到子进程完成为止,这不是我想要的。我以为Popen不会等待子进程完成,但是每当我运行此工具时,它都会停顿直到完成。我希望脚本结束,让子进程在后台运行,并使网页仍然正常运行,而用户没有想到一切都只是由于加载信号而停滞了。

The CGI page hangs until the subprocesses finish, which is not what I want. I thought Popen doesn't wait for the subprocesses to finish but whenever I run this tool, it stalls until it's complete. I want the script to finish and let the subprocesses run in the background and let the webpages still function properly without the user thinking everything is just stalled with the loading signals.

I似乎找不到任何原因Popen会这样做,因为在我读过的每一本书中,Popen都不表示等待,但事实似乎如此。

I can't seem to find any reason why Popen would do this because everywhere I read it says it does not wait, but it seems to.

奇怪的是,Apache日志显示:请求正文读取超时以及脚本完成之前。

Something odd also is that the apache logs show: "Request body read timeout" as well before the script completes. Is Apache actually stalling the script then?

抱歉,我无法显示完整的代码,因为它是机密的,但希望逻辑能被理解。

Sorry I can't show complete code as it's "confidential" but hopefully the logic is there to be understood.

推荐答案

不确定确切的工作原理,但我遵循了该线程的答案:
如何在Python中运行另一个脚本而不等待它完成

Not sure exactly why this works but I followed the answer in this thread: How do I run another script in Python without waiting for it to finish?

要执行的操作:

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

而不是:

p = subprocess.Popen([sys.executable, '/path/to/script.py'])

由于某些原因,现在CGI脚本将终止并且子进程继续运行。
关于为什么存在差异的任何见解会有所帮助吗?我不明白为什么必须定义其他两个参数会导致这种情况。

And for some reason now the CGI script will terminate and the subprocesses keep running. Any insight as to why there is a difference would be helpful? I don't see why having to define the other two parameters would cause such a stall.

这篇关于Python子进程Popen停顿CGI页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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