[Twisted-Python] Calling deferToThread from a thread
exarkun at divmod.com
Thu Sep 18 06:22:12 MDT 2008
On Thu, 18 Sep 2008 10:42:35 +0100, Reza Lotun <rlotun at gmail.com> wrote:
>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.
deferToThread isn't special. Like pretty much any Twisted API, it is
only safe to call in the reactor thread. It doesn't matter if the
reactor thread is the main thread or another one - whichever, that's
the only thread Twisted APIs are safe in.
>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??
Your best bet is to look at py.test's support for xunit style tests.
Whenever you need to deal with Deferreds, you should be using trial's
TestCase. Anything else you do will be fragile and prone to break (as
trial is, but at least we will fix it when it breaks so that you don't
More information about the Twisted-Python