2

Using Pyramid with Akhet, how do I execute a method after a response has been returned to the client? I believe this was done with the __after__ method in Pylons. I'm trying to execute a DB query and don't want it to block the request response.

tshepang
  • 12,111
  • 21
  • 91
  • 136
jamieb
  • 9,847
  • 14
  • 48
  • 63

1 Answers1

1

You can use a response callback for your case.

EDITED after Michael Merickel's comment: The response callback blocks the request to which is added, but you shouldn't worry about that callback blocking other requests since each request runs in a different thread. If you still need not to block the request with the callback, you can spawn a different thread or process (if you can afford it) or look into message queuing systems as mentioned in the comment below.

Danny Navarro
  • 2,733
  • 1
  • 18
  • 22
  • 4
    The response callbacks, as well as `__after__` in Pylons, are executed after a response object has been generated, but make no mistake that the code executed here still happens in-band with the request and before the response object is returned to the WSGI server. Neither Pyramid nor Pylons offer a solution for executing code out-of-band. You'll need to use an external queuing system or thread to accomplish this. – Michael Merickel Dec 19 '11 at 00:00
  • Thanks for pointing out the blocking of the request with the callback. I updated the answer taking your comment into account. I still want to make clear that Pyramid requests run in different threads and that in many cases that's more than enough to take care of non-blocking issues, instead of making the callback truly non-blocking. – Danny Navarro Dec 19 '11 at 07:26