[Twisted-Python] deferToThread and trial

Jonathan Jacobs jonathan+twisted at jsphere.com
Fri May 30 03:40:53 MDT 2014


Hi Piper,  

`successResultOf` is intended to be called on a Deferred which has a result (meaning `Deferred.errback` or `Deferred.callback` has been called on it already.) [1] In this case presumably your blocking call hasn’t finished (or even started?) by the time the assertion is executed, hence the Deferred has no result.

The usual case for using `successResultOf` is when you have a Deferred that you’re delivering results to synchronously (probably by way of calling `Deferred.callback` from your test) to determine if the callback chain produces the expected final result.

Without debating the merits of preferring to avoid interaction with the real world in unit tests, you can return a Deferred from your test method to run the reactor for you until the Deferred has fired and its callbacks have run. [2] You probably want to add some callbacks, to perform some assertions about the result, to the Deferred you’ll be returning.

[1] <http://twistedmatrix.com/documents/current/api/twisted.trial._synctest._Assertions.html#successResultOf>
[2] <http://twistedmatrix.com/documents/current/core/howto/testing.html#leave-the-reactor-as-you-found-it>

--  
Jonathan


On Friday 30 May 2014 at 8:13 AM, Piper Masden wrote:

> I have some Klein code that uses deferToThread for I/O. It looks something like this:
>  
> @app.route('/', methods=['GET']
> def index(request, *args, **kwargs):
>     d = deferToThread(some_blocking_db_select_function)
>  
>     def serialize(db_object):
>         return json.dumps({
>             'id': db_object,
>             })
>     d.addCallback(serialize)
>     return d
>  
> I have a test that executes this function, and the deferToThread returns a Deferred, but that deferred never fires its callback, and so when I use successResultOf expecting a success result, no result is found.  
>  
> When I use twistd to run the Klein application, everything works fine (the deferred fires and I get a json string in the body of the response). What's different about the trial environment that deferToThread might not fire its callback? Do I need to explicitly set up a thread pool in trial that I don't have to set up using twistd?  Any help would be appreciated.
>  
>  


-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20140530/f653d60d/attachment-0002.html>


More information about the Twisted-Python mailing list