简体   繁体   English

当使用python脚本作为CGI时,Subprocess Popen无法运行后台进程

[英]When using the python script as CGI, Subprocess Popen not able to run background process

I am trying to run another Python script in background from a CGI python script and want this script to run the process in background without waiting for the other script to complete. 我试图从CGI python脚本在后台运行另一个Python脚本,并希望此脚本在后台运行该过程,而无需等待其他脚本完成。 Somehow when I am running the same from Linux shell, I can run the other python script in background. 不知何故,当我从Linux shell运行相同的程序时,我可以在后台运行其他python脚本。 But when I tried doing the same through CGI, The front end keeps on loading until the other script completes and not just make it run in background. 但是当我尝试通过CGI做同样的事情时,前端一直在加载,直到另一个脚本完成,而不仅仅是让它在后台运行。

I have tried running the same on the Linux Shell and it works. 我已经尝试在Linux Shell上运行相同的功能。 When I shifted to CGI that is when the script waits for the other process to complete. 当我转移到CGI时,脚本等待另一个进程完成。

python1.py: python1.py:

command = [sys.executable,'python2.py', str(senddata)]
proc=subprocess.Popen(command,shell=False,stdin=None,stdout=None,stderr=None,close_fds=True)

print("Content-type: text/html\r\n\r\n")

print("The script is running in background! Expect an email in 10 minutes.")

python2.py: This script takes 2-5 minutes to execute and then sends an email to the group. python2.py:此脚本需要2-5分钟才能执行,然后向该组发送一封电子邮件。

The expected output is to have this message: 预期的输出是有这样的信息:

The script is running in background! 该脚本在后台运行! Expect an email in 10 minutes. 预计10分钟内会收到一封电子邮件。

And run python2.py in background without waiting for it to complete. 并在后台运行python2.py而不等待它完成。

The webserver will keep the client response active (causing the client to stay in a "loading" state) until all of the output from the CGI program has been collected and forwarded to the client. Web服务器将使客户端响应保持活动状态(使客户端保持“加载”状态),直到CGI程序的所有输出都已收集并转发到客户端。 The way the webserver knows that all output has been collected is that it sees the standard output stream of the CGI process being closed. Web服务器知道已收集所有输出的方式是它看到CGI进程的标准输出流被关闭。 That normally happens when the CGI process exits. 这通常发生在CGI进程退出时。

The reason why you're having this problem is that when subprocess.Popen is told to execute a program with stdout=None , the spawned program will share its parent's standard output stream. 您遇到此问题的原因是当subprocess.Popen被告知执行stdout=None的程序时,生成的程序将共享其父标准输出流。 Here that means that your background program shares the CGI process's standard output. 这意味着您的后台程序共享CGI进程的标准输出。 That means that from the webserver's point of view that stream remains open until both of the processes exit. 这意味着从Web服务器的角度来看,流保持打开状态,直到两个进程都退出。

To fix, launch the background process with stdout=subprocess.PIPE . 要修复,请使用stdout=subprocess.PIPE启动后台进程。 If the background process misbehaves if its stdout gets closed when the CGI process dies, try launching it with stdout=open('/dev/null') instead. 如果后台进程在CGI进程死亡时stdout被关闭时行为不正常,请尝试使用stdout=open('/dev/null')启动它。

The background process's stdin and stderr will have the same issue; 后台进程的stdinstderr会有同样的问题; they will be shared with the CGI process. 它们将与CGI流程共享。 AFAIK sharing those will not cause trouble as long as the background process does not attempt to read from its standard input, but if it does do that (or if you just want to be cautious) you can treat them the same way as you treat stdout and either set them to subprocess.PIPE or associate them with /dev/null . 只要后台进程不尝试从标准输入中读取,AFAIK共享那些不会造成麻烦,但如果它确实这样做(或者如果你只是想要谨慎),你可以像对待stdout一样对待它们并将它们设置为subprocess.PIPE或将它们与/dev/null关联。

More detail is at https://docs.python.org/2/library/subprocess.html#popen-constructor 更多细节请访问https://docs.python.org/2/library/subprocess.html#popen-constructor

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

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