[Twisted-Python] Synchronous @inlineCallback interpreter

pierre at jaury.eu pierre at jaury.eu
Thu Apr 24 18:14:22 MDT 2014


Le 2014-04-25 01:45, Clayton Daley a écrit :
> I imagine rewriting this to the following:
> 
>     @classmethod
>     @inlineCallbacks
>     def from_id(cls, id):
>         doc = yield db.get_document(id) # blocking/async db call
> 
>         self = cls._new_document(doc) # always synchronous
>          yield self.__init__() # blocking/async call
>         returnValue(self)
> 
> places. I've read that the @inlineCallback code is complicated so I
> figured I'd run the idea by the experts before diving down that rabbit
> hole.

I am no expert, but the code for inlineCallbacks looks pretty
straightforward, except some special cases handled down the way.

> Does anyone see any obvious pitfall? I'm happy to do the legwork
> to try and build it, but I'd also welcome any pointers from your (no
> doubt extensive) experience writing the twisted version.

I would mostly argue about a semantic problem. inlineCallbacks help
asynchronous code *look* like synchronous code thanks to the sequential
writing. It is however essentially asynchronous and does return a
Deferred.

I do not see any immediate technical pitfall here. I would however be
very careful and double think my design if I were to use Deferred
objects as part of a blocking process.

kaiyou.




More information about the Twisted-Python mailing list