【发布时间】:2017-02-01 04:23:28
【问题描述】:
所以我有一个封装状态机的cherry py 服务器。在幕后,这个状态机完成了管理进程、线程和缓存的所有繁重工作,而且它是线程安全的。因此,在启动我的樱桃 py 时,我将其附加到应用程序中,并且请求调用 app.state_machine.methods。
在伪代码中我这样做:
from flask_app import app
import cherrypy
#configure app
app.state_machine = State_Machine.start()
try:
cherrypy.tree.graft(app, "/")
cherrypy.server.unsubscribe()
server = cherrypy._cpserver.Server()
server.socket_host="0.0.0.0"
server.socket_port = 5001
server.thread_pool = 10
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
except KeyboardInterrupt:
logging.getLogger(__name__).warn("Recieved keyboard interrupt")
except Exception:
logging.getLogger(__name__).exception("Unknown exception from app")
finally:
logging.getLogger(__name__).info("Entering Finally Block")
state_machine.shutdown()
这里的意图是键盘中断应该传播到应用程序并调用 state_machine.shutdown,它适用于例如烧瓶开发服务器。
但是,cherrypy 吞下 KeyBoard 中断并等待其子线程关闭,因为它们包含对 app.state_machine 的引用,它们将无限期地死锁,直到调用 app.state_machine.shutdown()。
那么如何修改cherrypy的关闭程序,让它正确调用shutdown呢?
【问题讨论】:
-
我认为,您可以实现a CherryPy plugin 来执行状态机启动和关闭,绑定到 CherryPy 应用程序周期。
标签: python multithreading webserver cherrypy