[Twisted-Python] Connection poolin

John Goerzen jgoerzen at complete.org
Sat Jul 26 19:38:30 MDT 2003


Here is something I haven't been able to figure out yet.

I am using IMAP4Client from Twisted.

The behavior I used to use is this:
When I wanted to speak to the IMAP server in my multithreaded
application, I would call a function that returns me an IMAP
connection.  There was a limit on the number of connections open --
let's say 5 for this example.  If an open connection was available,
that function would return it to me immediately.  If 5 were already
open, it would block until one of those was freed.  Otherwise, it
would establish a new connection and return that to me.

My task as a caller was to always release the connection when done.
My pattern, then, was generally one of:

imap = self.imapserver.acquireconnection()
try:
    # select the appropriate mailbox
    # do stuff here
    return result
finally:
   self.imapserver.release(imap)

I have been trying to figure out how to do this the Twisted way.
Obviously blocking is right out.

I've thought that in each method I have that needs a connection, I
could write two methods: one that calls the Twisted version of
acquireconnection and sets its callback to the second, which does the
real work (and possibly chains to others via callbacks.)

There are some problems with that approach.  One is that it is very
verbose and requires a lot of code all over the place to manage.
Another is that there's no easy way to tell in Twisted when we are
done with the given connection.  I suppose I could always add errbacks
all along the way plus a callback, each of which releases the
connection, but that seems inconvenient and error-prone.

Basically, what I'm saying is that I can invent something that works,
but everything I've come up with seems like a nasty kludge, and I'm
looking for the Right Way to do it.

Thanks,
John





More information about the Twisted-Python mailing list