[Twisted-Python] synchronous/asynchronous api: possible interface
exarkun at twistedmatrix.com
exarkun at twistedmatrix.com
Fri Nov 13 08:21:11 MST 2009
On 02:32 pm, doug.farrell at gmail.com wrote:
>Hi all,
>
>I'd like to get some comments on the code below (including, "don't be
>a bonehead!" <g>). I'm trying to write a API library for database
>access that can be used by both synchronous (non-twisted, no reactor)
>code and asynchronous (twisted, with reactor) code where the methods
>of the library can called in either environment. What I'd like is the
>method to return the results of the call (database query results) when
>in synchronous mode, and a deferred that is a result of the database
>call in asynchronous mode.
>
>What I've done below is create a decorator class called CallerMode
>that is used to decorate a function/method that normally returns a
>deferred. If the CallerMode class is in synchronous mode it starts a
>reactor to get the default callbacks to get called, which gets the
>results or exceptions, and stops the reactor.
>
>Am I being a dunce doing this kind of thing? Any suggestions,
>comments, etc. are welcome.
If you add a second call to your example function in synchronous mode, I
think you'll notice there's a problem - you can only start and stop the
reactor once.
Jean-Paul
More information about the Twisted-Python
mailing list