How to do asynchronous post processing in CherryPy?

Context: Imagine you have a standard hello word application for CherryPy:

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

and you would like to do some post-processing, that is, processing a write request, or simply register the fact that we were called from a specific IP address. You would probably do:

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

However, this will add your request processing time. (By the way, and probably you will use decorators or an even more complicated method if you want to name it for each function).

Question: Is there a way to create a global chain (buffers) that supports threads, for which each request can write messages (events) that should be written to the log, and some magic functions will capture it and the post-process? Do you know a pattern for such a thing?

I'm sure CherryPy supports something like this :-)

Thanks in advance...

+4
source share
3 answers

The "global threading queue" is called Queue.Queue. I just added a recipe for this at http://tools.cherrypy.org/wiki/BackgroundTaskQueue

+7
source

the on_end_request custom tool may be what you want.

+2
source

Since I was looking for this and now it is deprecated, I found it helpful to give the correct answer (2012). Just add this at the beginning of the function that processes your URL:

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

The documentation has more information about hooks, but this is not very clear to me.

+2
source

All Articles