[英]What is the proper way to handle periodic housekeeping tasks in Python/Pyramid/CherryPy?
我有一个使用Web服务器的Pyramid / CherryPy的python Web应用程序。
它需要执行一些定期的内务处理任务-清除过时的会话,释放其资源等。
解决此问题的正确方法是什么? 我可以很容易地运行一个额外的“管家”线程(并使用一个单独的调度程序,例如APscheduler ),但是让一个单独的线程进入正在运行的服务器线程似乎是一个非常笨拙的解决方案。 CherryPy已经在(多线程)事件循环中运行服务器,似乎应该可以通过这种方式安排定期事件。
帮自己一个忙,只使用cron。 无需滚动自己的调度软件。
@fumanchu的答案使我得到了这个答案,但是我使用了cherrypy.process.plugins.BackgroundTask
插件的实例结束了:
def doHousekeeping():
print("Housekeeper!")
-
def runServer():
cherrypy.tree.graft(wsgi_server.app, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
server = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30
# Subscribe this server
server.subscribe()
cherrypy.engine.housekeeper = cherrypy.process.plugins.BackgroundTask(2, doHousekeeping)
cherrypy.engine.housekeeper.start()
# Start the server engine (Option 1 *and* 2)
cherrypy.engine.start()
cherrypy.engine.block()
结果在CherryPy事件循环内每隔2秒调用doHousekeeping()
。
它也没有涉及像拖拽整个OS那样愚蠢的事情,只是周期性地调用任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.