我有一个python应用程序,它使用金字塔/CherryPy作为web服务器。
它有一些定期的内务管理任务,需要运行-清除陈旧的会议,释放他们的资源,等等.
处理这个问题的正确方法是什么?我可以很容易地运行一个额外的“内务管理”线程(并使用一个单独的调度程序,比如APscheduler),但是让一个单独的线程到达正在运行的服务器线程似乎是一个非常笨拙的解决方案。CherryPy已经在一个(多线程)事件循环中运行服务器,似乎应该可以通过它来调度周期性事件。
发布于 2014-04-30 04:24:27
看看http://cherrypy.readthedocs.org/en/latest/progguide/extending/customplugins.html的“主”频道
发布于 2014-04-30 04:47:21
我的答案是@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()在doHousekeeping()事件循环中以2秒间隔调用CherryPy的结果。
它也不需要做一些像拖拽整个操作系统那样愚蠢的事情,只是为了周期性地调用一个任务。
发布于 2014-04-29 11:10:17
帮你自己一个忙就用cron吧。不需要你自己的调度软件。
https://stackoverflow.com/questions/23361597
复制相似问题