0

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

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

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工具吗?

4

1 回答 1

0

地方

queue = BackgroundTaskQueue(cherrypy.engine)

例如,在名为tasks.py的单独文件中。这样您就可以创建模块任务。现在您可以在其他模块中“导入任务”,并且队列是单个实例

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

import tasks
def test(): print('works!')
tasks.queue.put(log, test)
于 2015-11-09T21:48:16.297 回答