TEXT   35

Multi Threaded Quixote Applications

Guest on 27th June 2022 05:42:27 AM

  1. Multi-Threaded Quixote Applications
  2. ===================================
  3.  
  4. Starting with Quixote 0.6, it's possible to write multi-threaded Quixote
  5. applications.  In previous versions, Quixote stored the current
  6. HTTPRequest object in a global variable, meaning that processing
  7. multiple requests in the same process simultaneously was impossible.
  8.  
  9. However, the Publisher class as shipped still can't handle multiple
  10. simultaneous requests; you'll need to subclass Publisher to make it
  11. re-entrant.  Here's a starting point::
  12.  
  13.   import thread
  14.   from quixote.publish import Publisher
  15.  
  16.   [...]
  17.  
  18.   class ThreadedPublisher (Publisher):
  19.       def __init__ (self, root_namespace, config=None):
  20.           Publisher.__init__(self, root_namespace, config)
  21.           self._request_dict = {}
  22.  
  23.       def _set_request(self, request):
  24.           self._request_dict[thread.get_ident()] = request
  25.  
  26.       def _clear_request(self):
  27.           try:
  28.               del self._request_dict[thread.get_ident()]
  29.           except KeyError:
  30.               pass
  31.  
  32.       def get_request(self):
  33.           return self._request_dict.get(thread.get_ident())
  34.  
  35. Using ThreadedPublisher, you now have one current request per thread,
  36. rather than one for the entire process.

Raw Paste


Login or Register to edit or fork this paste. It's free.