[Twisted-Python] Accessing the request object from a different thread

Phil Mayers p.mayers at imperial.ac.uk
Sat Jan 16 07:54:07 EST 2010

> All of the accesses to the request object are read operations.  If
> those read operations occur on data that does not get messed with by
> the main thread, then this should be thread-safe code.  However, it
> seems that the official word is "do not use request objects from other
> threads."  Maybe I am mistaken.  In any case, is there a safe way to
> get at request headers and parameter values outside of the reactor
> loop.

The rule is not to call any twisted functions IIRC.

You can do this safely:

   args_copy = dict(request.args)
   hdr_copy = dict(request.requestHeaders.getAllRawHeaders())
   deferToThread(func, args_copy, hdr_copy)

More information about the Twisted-Python mailing list