<div dir="ltr">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. <div>

<br></div><div>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):</div>

<div><br></div><div><div>    @classmethod</div><div>    def from_id(cls, id):</div><div>        doc = db.get_document(id) # blocking db call<br></div><div>        self = cls._new_document(doc) # synchronous initialization stuff</div>

<div>        self.__init__() # module init code is custom and may block</div><div>        return self</div><div><br></div><div>I imagine rewriting this to the following:</div><div><br></div><div><div>    @classmethod</div>

<div>    @inlineCallbacks</div><div>    def from_id(cls, id):</div><div>        doc = yield db.get_document(id) # blocking/async db call<br></div><div>        self = cls._new_document(doc) # always synchronous</div><div>
        yield self.__init__() # blocking/async call</div>
<div>        returnValue(self)</div><div><br></div></div><div>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.<br>

</div><div><br></div><div>Thanks in advance!</div><div><div><div> </div><div>Clayton Daley</div></div>
</div></div></div>