[Twisted-Python] Synchronous @inlineCallback interpreter

I have a modular application that communicates over RPC connections (with both sync and async server/clients available). I want to migrate some of the modules over to Twisted. Currently, all modules inherit important utility functions from a parent class. On paper, I now need two versions of this parent class (synchronous & asynchronous) to be inherited by modules of the respective types. I'm looking for ways to maximize the common code between the two versions of the parent object. One that comes to mind (motivating the subject of this post) would be to write a synchronous interpreter for @inlineCallback calls. For example, here's a synchronous factory function from the parent class (already partly reconfigured to be friendly to my idea): @classmethod def from_id(cls, id): doc = db.get_document(id) # blocking db call self = cls._new_document(doc) # synchronous initialization stuff self.__init__() # module init code is custom and may block return self 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) Assuming the db connector and init function are appropriate to the environment, my gut reaction is that this could (when combined with a synchronous implementation of the decorator) run correctly in both 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. 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. Thanks in advance! Clayton Daley

Le 2014-04-25 01:45, Clayton Daley a écrit :
I am no expert, but the code for inlineCallbacks looks pretty straightforward, except some special cases handled down the way.
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.

On 24 Apr, 11:45 pm, clayton.daley@gmail.com wrote:
You might find that replacing the subclassing with composition helps simplify the code re-use problem. You may also be interested in https://github.com/radix/synchronous- deferred Jean-Paul

Le 2014-04-25 01:45, Clayton Daley a écrit :
I am no expert, but the code for inlineCallbacks looks pretty straightforward, except some special cases handled down the way.
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.

On 24 Apr, 11:45 pm, clayton.daley@gmail.com wrote:
You might find that replacing the subclassing with composition helps simplify the code re-use problem. You may also be interested in https://github.com/radix/synchronous- deferred Jean-Paul
participants (3)
-
Clayton Daley
-
exarkun@twistedmatrix.com
-
pierre@jaury.eu