[Twisted-Python] Creating a new selectable (for Pg async interface)
haering_python at gmx.de
Sun Dec 15 11:13:49 EST 2002
I'd like to learn asynchronuous programming with Twisted, because I
think this will make me a better programmer, no matter if I'll use this
paradigma in the end or not.
So I thought about wrapping PostgreSQL's asynchronous interface to be
usable from a Twisted app. I did once chat a little with Itamar about
this on #twisted, and now I finally got around starting this.
I don't really know much about twisted, so please correct me if this is
A reactor is the Twisted main loop, which selects on selectables,
and invokes doRead() on them when data is available.
Itamar told me I'd need to implement
twisted.internet.abstract.FileDescriptor, so this is what I did (very
from twisted.internet import abstract
from pyPgSQL import libpq
# PostgreSQL stuff
self.conn = libpq.PQconnectdb("host=gargamel dbname=gerhard")
self.fileno = self.conn.socket.fileno
def write(self, data):
def query(self, callback, errback, *args, **kwargs):
print "sending query ..."
self.callback, self.errback = callback, errback
print "query sent."
def operation(self, callback, errback, *args, **kwargs):
if not self.conn.isBusy:
print "data available"
res = self.conn.getResult()
print "got result."
bla = self.conn.getResult()
assert bla is None
print "query sucessfully processed."
# call callback here
def connectionLost(self, reason):
"""The connection was lost.
Now I'm at the point where I'd like to try this code out. *But*
obviously something is still missing. I believe my PgAsyncConn would
need to be registered with the reactor?! Is this true and if yes, how do
I do it?
Btw. I'll try to keep my new baby interface-compatible with
twisted.enterprise.adbapi as much as possible.
Next question: I'd like to try my code out. Is there a good way to test
this under Twisted, apart from writing a little server like the Echo
example of the docs, and trying to call PgAsyncConn from it?
Favourite database: http://www.postgresql.org/
Favourite programming language: http://www.python.org/
Combine the two: http://pypgsql.sf.net/
More information about the Twisted-Python