[Twisted-Python] Transmit queue - how would I go at it?

Pablo Martí pablo at minimoesfuerzo.org
Fri Sep 14 03:37:31 EDT 2007

On Fri, 2007-09-14 at 00:58 -0400, Nadav Aharony wrote:
> Hi, 
> I am working on an application where clients request files, and since
> each file is very large, the server ACKs the requests and then queues
> them to be served later, one at a time. 
> I am relatively new to Twisted and was wondering what would be the
> best way to approach this.

Hi Nadav,

I had a similar requirement with my application. You must offer an
asynchronous interface while dealing with each request synchronously. I
think that you should push your requests to a
twisted.internet.defer.DeferredQueue and process one at a time with the
aid of twisted.internet.defer.DeferredLock.

If we consider that each of your petitions has an associated deferred
that will be called back when its finished:
(I haven't run this)

from twisted.internet import defer

class OneAtATime(object):
    def __init__(self):
        super(OneAtATime, self).__init__()
        self.queue = defer.DeferredQueue()
        self.lock = defer.DeferredLock()

    def check_queue(self):
    def _process_one(self, petition):

        def acquire_cb(_):
            # do whatever with your petition
            # make sure that when you're done you call
            # self.lock.release() and self.check_queue()
            # on a state machine, this would be while transitioning
            # to the "idle" state


    def queue_petition(self, petition):
        return petition.deferred


More information about the Twisted-Python mailing list