如何在CherryPy中执行异步后处理?

sti*_*tic 4 python asynchronous cherrypy

上下文: 想象一下,你有一个标准的CherryPy hello word app:

   def index(self):
      return "Hello world!"
   index.exposed = True
Run Code Online (Sandbox Code Playgroud)

并且您希望进行一些后处理,即记录请求处理或只记录我们从特定IP调用的事实.你会做的可能是:

def index(self):
   self.RunMyPostProcessing()
   return "Hello world!"
index.exposed = True
Run Code Online (Sandbox Code Playgroud)

但是,这会增加您的请求处理时间.(顺便说一句.如果你想在每个函数上调用它,你可能会使用装饰器,甚至是一些更复杂的方法).

问: 有没有建立一个全球性的线程知道队列(缓冲)到每个请求可以写消息(事件)需要被记录的方式,而一些神奇的功能会抓住它和后处理?你知道这种事情的模式吗?

我打赌CherryPy支持这样的东西:-)

先感谢您...

fum*_*chu 7

"全局线程感知队列"称为Queue.Queue.我刚刚在http://tools.cherrypy.org/wiki/BackgroundTaskQueue上添加了一个配方