CherryPy 后台任务
我需要一个调用 cherrypy.process.plugins.BackgroundTask
的简单示例。
我尝试了一下,但似乎无法让它工作(文档中没有示例)。
这是我的代码:
def func():
print "blah blah blah"
wd = cherrypy.process.plugins.BackgroundTask(15000,func)
wd.run()
I need a simple example of invoking the cherrypy.process.plugins.BackgroundTask
.
I tried it out but can't seem to get it to work (no examples in the docs).
Here is my code:
def func():
print "blah blah blah"
wd = cherrypy.process.plugins.BackgroundTask(15000,func)
wd.run()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是,您想要调用
wd.start()
,而不是wd.run()
。另外,由于
BackgroundTask
是守护进程,除非您执行其他操作来保持解释器处于活动状态,否则当您的线程在后台浮动而无法查看输出时,Python 将退出。也就是说,我一直在尝试制作一个可行的示例,但尚未成功。这是我正在使用的代码,可能很糟糕:
最后,查看
BackgroundTask
的源代码,我发现似乎是一个错误 - 异常处理程序依赖于self.bus
属性不存在(bus
在其他插件的构造函数中显式设置,但在此类中未设置)。我不认为这个错误与我未能使其正常工作有关。The short answer is, you want to call
wd.start()
, notwd.run()
.Also, because
BackgroundTask
is daemonic, unless you are doing something else to keep the interpreter alive, Python will exit while your thread floats in the background with no way to see the output.That said, I've been futzing around with trying to make a working example and haven't succeeded yet. This is the code I am using, which may suck:
Finally, looking at the source of
BackgroundTask
, I see what appears to be a bug -- the exception handler relies on aself.bus
attribute that doesn't exist (bus
is explicitly set in the other plugins' constructors, but not this class). I don't think that bug is related to my failure to get this working.