3

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. 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.

I am using

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

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

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.

The Problem

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 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.

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.

4

2 回答 2

1

Apache 可能会等待子进程完成。您可以尝试妖魔化孩子(双叉,setsid),或者最好将作业提交给本地服务,例如,通过写入预定义文件或使用一些消息代理或通过更高级别的接口(如 celery)

于 2012-07-03T19:27:30.013 回答
1

不确定为什么会这样,但我遵循了这个线程中的答案: 如何在 Python 中运行另一个脚本而不等待它完成?

去做:

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 脚本将终止并且子进程继续运行。关于为什么存在差异的任何见解都会有所帮助?我不明白为什么必须定义其他两个参数会导致这样的停顿。

于 2012-07-03T19:40:07.153 回答