从子进程启动时如何停止 Bottle Web 服务器
我想将伟大的 Bottle Web 框架嵌入到一个小型应用程序中(第一个目标是 Windows 操作系统)。这个应用程序通过子进程模块启动了 Bottle Web 服务器。
import subprocess
p = subprocess.Popen('python websrv.py')
Bottle 应用程序非常简单,
@route("/")
def index():
return template('index')
run(reloader=True)
它将默认的网络服务器启动到 Windows 控制台。
一切看起来都不错,除了我必须按 Ctrl-C 来关闭 Bottle 网络服务器这一事实。我希望主应用程序在关闭时终止网络服务器。我找不到办法做到这一点(不幸的是,p.terminate() 在这种情况下不起作用)
有什么想法吗?
提前致谢
I would like to embed the great Bottle web framework into a small application (1st target is Windows OS). This app starts the bottle webserver thanks to the subprocess module.
import subprocess
p = subprocess.Popen('python websrv.py')
The bottle app is quite simple
@route("/")
def index():
return template('index')
run(reloader=True)
It starts the default webserver into a Windows console.
All seems Ok except the fact that I must press Ctrl-C to close the bottle webserver. I would like that the master app terminates the webserver when it shutdowns. I can't find a way to do that (p.terminate() doesn't work in this case unfortunately)
Any idea?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两种方法可以关闭重载服务器:
1) 终止 p (使用 os.kill(p.pid) 或 p.terminate() ),然后更改'websrv.py' 的修改时间 (
os.utime('websrv.py')
) 触发子进程自动关闭。2) 使用 os.kill(p.pid, signal.SIGINT) 终止 p,这与 Ctrl-C 关闭相同。
There are two ways to shutdown a reloading server:
1) You terminate p (using
os.kill(p.pid)
orp.terminate()
) and then change the modification time of 'websrv.py' (os.utime('websrv.py')
) to trigger an automatic shutdown of the child process.2) You terminate p with
os.kill(p.pid, signal.SIGINT)
which is identical to aCtrl-C
shutdown.如果 Bottle 处于重新加载模式,则终止进程似乎不起作用。在这种情况下,它自己启动一个子进程。
如果 reload 设置为 False,则终止似乎可以正常工作。
It seems that the terminate process doesn't work if Bottle is in reload mode. In this case, it starts iteself a subprocess.
If reload is set to False, the terminate seems to work Ok.
从 0.8.1 开始,重新加载服务器足够智能来清理孤立进程。现在,您可以通过多种方式终止服务器:
Ctrl-C
或向父进程发送SIGINT
。 (推荐)Starting with 0.8.1 the reloading server is smart enough to clean up orphan processes. You now have several ways to terminate the server:
Ctrl-C
or sendSIGINT
to the parent process. (recommended)我无法从请求中关闭 Bottle 服务器,因为 Bottle 似乎在子进程中运行请求。
我最终发现解决方案是:
在请求内(该请求被传递到瓶子服务器并取消它)。
也许尝试在你的过程中这样做,看看瓶子是否收到消息。
I had trouble closing a bottle server from within a request as bottle seems to run requests in subprocesses.
I eventually found the solution was to do:
inside the request (that got passed up to the bottle server and axed it).
Maybe try doing that in your process and see if bottle gets the message.