简体   繁体   English

Python Subprocess Popen Stalling CGI Page

[英]Python Subprocess Popen Stalling CGI Page

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. 目前我拥有它,因此它通过Web界面。

  1. User goes to the form and submits a filename for parsing (file already on system). 用户转到表单并提交用于解析的文件名(文件已在系统上)。
  2. Form submits information to Python CGI script 表单将信息提交给Python CGI脚本
  3. Python CGI script runs and spawns a subprocess to run the parsing. Python CGI脚本运行并生成子进程以运行解析。
  4. Parser finds appropriate information for analysis and spawns subprocess also. Parser找到适当的信息进行分析并生成子进程。

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: 启动所有这一切的我的CGI脚本:

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. HTML生成代码只输出我们处理了请求的状态,然后javascript将页面刷新到主页面。

The Problem 问题

The CGI page hangs until the subprocesses finish, which is not what I want. CGI页面挂起,直到子进程完成,这不是我想要的。 I thought Popen doesn't wait for the subprocesses to finish but whenever I run this tool, it stalls until it's complete. 我认为Popen不会等待子进程完成,但每当我运行这个工具时,它都会停止直到它完成。 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. 我似乎无法找到任何理由为什么Popen会这样做,因为在我读到的任何地方都说它不会等待,但它似乎。

Something odd also is that the apache logs show: "Request body read timeout" as well before the script completes. 奇怪的是,在脚本完成之前,apache日志显示:“请求正文读取超时”。 Is Apache actually stalling the script then? Apache实际上是在拖延脚本吗?

Sorry I can't show complete code as it's "confidential" but hopefully the logic is there to be understood. 对不起,我无法显示完整的代码,因为它是“机密的”,但希望逻辑是可以理解的。

Apache probably waits for the child process to complete. Apache可能会等待子进程完成。 You could try to demonize the child (double fork, setsid) or better just submit the job to a local service eg, by writing to a predefined file or using some message broker or via higher level interface such as celery 您可以尝试妖魔化孩子(double fork,setsid)或者更好地将作业提交给本地服务,例如,通过写入预定义文件或使用某些消息代理或通过更高级别的界面(如芹菜)

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? 不知道为什么这个有效,但我在这个帖子中回答了问题: 如何在Python中运行另一个脚本而不等待它完成?

To do: 去做:

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

Instead of: 代替:

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

And for some reason now the CGI script will terminate and the subprocesses keep running. 由于某种原因,现在CGI脚本将终止并且子进程继续运行。 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. 我不明白为什么必须定义其他两个参数会导致这样的停顿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM