[Twisted-Python] synchronous/asynchronous api: possible interface

Tom Leys leyssw at ihug.co.nz
Fri Nov 13 14:18:48 EST 2009

Hi Doug.

I think that a decorator is a fairly nice idea, however you will 
probably run into the following issue:

Once a reactor has been started and stopped, starting the reactor again 
will crash your program. You will need to come up with a way to get 
around this problem if you plan to call your database code more than 
once without restarting the python process.



Message: 9
Date: Fri, 13 Nov 2009 09:32:25 -0500
From: Doug Farrell <doug.farrell at gmail.com>
Subject: [Twisted-Python] synchronous/asynchronous api: possible
To: Twisted general discussion <twisted-python at twistedmatrix.com>
	<80c54aa0911130632l63cad82aq54a93cff139c8b02 at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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.


More information about the Twisted-Python mailing list