web.py +子进程=挂起

发布于 2024-10-18 05:16:44 字数 731 浏览 8 评论 0原文

这是我的主文件:

import subprocess, time

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe)
time.sleep(3)

这是 test_web_app.py:

import web

class Handler:
  def GET(self):  pass
app = web.application(['/', 'Handler'], globals())
app.run()

当我运行主文件时,程序会执行,但僵尸进程仍处于挂起状态,我必须手动终止它。这是为什么呢?当程序结束时如何让Popen死掉?仅当我在程序结束之前通过管道传输标准输出并休眠一段时间时,Popen 才会挂起。

编辑 - 这是主文件的最终工作版本:

import subprocess, time, atexit

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe)

def kill_app():
  popen.kill()
  popen.wait()
atexit.register(kill_app)

time.sleep(3)

Here's my main file:

import subprocess, time

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe)
time.sleep(3)

And here's test_web_app.py:

import web

class Handler:
  def GET(self):  pass
app = web.application(['/', 'Handler'], globals())
app.run()

When I run the main file, the program executes, but a zombie process is left hanging and I have to kill it manually. Why is this? How can I get the Popen to die when the program ends? The Popen only hangs if I pipe stdout and sleep for a bit before the program ends.

Edit -- here's the final, working version of the main file:

import subprocess, time, atexit

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe)

def kill_app():
  popen.kill()
  popen.wait()
atexit.register(kill_app)

time.sleep(3)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-10-25 05:16:44

您尚未等待该过程。完成后,您必须调用popen.wait

您可以使用 popen 对象的 poll 方法检查进程是否终止,以查看进程是否已完成。

如果您不需要Web服务器进程的stdout,则可以简单地忽略stdout选项。


您可以使用 atexit 模块来实现一个在主文件退出时调用的钩子。这应该使用 Popen 对象的 kill 方法,然后对其进行 wait 以确保其终止。

You have not waited for the process. Once it's done, you have to call popen.wait.

You can check if the process is terminated using the poll method of the popen object to see if it has completed.

If you don't need the stdout of the web server process, you can simply ignore the stdout option.


You can use the atexit module to implement a hook that gets called when your main file exits. This should use the kill method of the Popen object and then wait on it to make sure that it's terminated.

千纸鹤 2024-10-25 05:16:44

如果你的主脚本在子进程执行时不需要做任何其他事情,我会这样做:(

import subprocess, time

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe, stderr=pipe)
out, err = popen.communicate()

我认为如果你专门将标准输出传输回你的程序,你需要在某个时候读取它以避免创建僵尸进程 - 通信将以相当安全的方式阅读它)。

或者,如果您不关心解析 stdout / stderr,则不必费心通过管道传输它们:

popen = subprocess.Popen('pythonw -uB test_web_app.py')
popen.communicate()

If your main script doesn't need to be doing anything else while the subprocess executes I'd do:

import subprocess, time

pipe = subprocess.PIPE
popen = subprocess.Popen('pythonw -uB test_web_app.py', stdout=pipe, stderr=pipe)
out, err = popen.communicate()

(I think if you specifically pipe stdout back to your program, you need to read it at some point to avoid creating zombie processes - communicate will read it in a reasonably safe way).

Or if you don't care about parsing stdout / stderr, don't bother piping them:

popen = subprocess.Popen('pythonw -uB test_web_app.py')
popen.communicate()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文