5

Context: Imagine that you have a standard CherryPy hello word app:

   def index(self):
      return "Hello world!"
   index.exposed = True

and you would like to do some post-processing, i.e. record request processing or just log the fact that we were called from specific IP. What you would do is probably:

def index(self):
   self.RunMyPostProcessing()
   return "Hello world!"
index.exposed = True

However, that will add to your request processing time. (btw. And probably you will use decorators, or even some more sophisticated method if you would like to call it on every function).

Question: Is there a way of creating a global threading aware queue (buffer) to which each request can write messages (events) that needs be logged, while some magic function will grab it and post-process? Would you know a pattern for such a thing?

I bet that CherryPy supports something like that :-)

Thank you in advance...

4

2 回答 2

7

“全局线程感知队列”称为 Queue.Queue。

于 2009-08-21T16:48:03.197 回答
2

当我在寻找这个并且它现在已经过时时,我发现提供正确的(2012ish)答案很有用。只需在处理您的 url 的函数的开头添加它:

cherrypy.request.hooks.attach('on_end_request', mycallbackfunction)

文档中有更多关于钩子的信息,但对我来说不是很清楚。

于 2012-11-25T01:14:19.833 回答