[Twisted-Python] Calling deferToThread from a thread
rlotun at gmail.com
Thu Sep 18 03:42:35 MDT 2008
I have a data store api that mandates the return of deferreds for each
call. I have a number of implementations for the api - an in-memory
one for testing, and a number of other network + disk based apis.
My issue is this - I'm using py.test for unit testing (I'm aware of
trial, but for compatibility reasons with the rest of the project I
haven't looked into using it). To test deferred-based code I have set
up a separate thread which the reactor runs in (the setup and teardown
code is shown below for anyone who is interested).
net_thread = threading.Thread(target=net)
This seems to work generally fine. The problem (or at least I *think*
it's the problem) is that in one of my implementations uses
deferToThread internally to create a deferred which it then returns to
the caller. Is it thread safe to call a deferToThread in a separate
thread? Normally I'd do a reactor.callFromThread, but I'm not sure how
to get a deferred by this construct.
I see two options:
1. In my unit test I write a function f which makes the api call and
gets the deferred, and sets it to a local variable within the test
instance. Then I schedule f to run in the reactor thread via
2. I look into trial which has support for this kind of stuff??
Thanks for any help.
More information about the Twisted-Python