Python 共享樱桃糖';请求处理程序之间的BackgroundTaskQueue对象

Python 共享樱桃糖';请求处理程序之间的BackgroundTaskQueue对象,python,cherrypy,object-sharing,Python,Cherrypy,Object Sharing,我正在使用cherrypy构建一个web服务。我遇到了这个插件,我想用它在一个单独的线程上处理特定的耗时操作 文档说明了使用方法应如下所示: import cherrypy from complicated_logging import log bgtask = BackgroundTaskQueue(cherrypy.engine) bgtask.subscribe() class Root(object): def index(self): bgtask.put

我正在使用cherrypy构建一个web服务。我遇到了这个插件,我想用它在一个单独的线程上处理特定的耗时操作

文档说明了使用方法应如下所示:

import cherrypy
from complicated_logging import log

bgtask = BackgroundTaskQueue(cherrypy.engine)
bgtask.subscribe()

class Root(object):

    def index(self):
        bgtask.put(log, "index was called", ip=cherrypy.request.remote.ip))
        return "Hello, world!"
    index.exposed = True
但是,恕我直言,像这样使用bgtask对象不是很优雅。我希望其他python模块的处理程序也使用这个对象

有没有办法订阅此插件一次,然后在其他处理程序之间“共享”bgtask对象(例如,将其保存在
cherrypy.request
)中

这是怎么做到的?这是否需要编写cherrypy工具?

Place

queue = BackgroundTaskQueue(cherrypy.engine)
例如,在名为tasks.py的单独文件中。这样可以创建模块任务。 现在,您可以在其他模块中“导入任务”,队列是一个实例

例如,在名为test.py的文件中:

import tasks
def test(): print('works!')
tasks.queue.put(log, test)