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

Pablo Martí pablo at minimoesfuerzo.org
Sun Sep 16 06:27:10 EDT 2007


On Sat, 2007-09-15 at 23:36 -0400, Nadav Aharony wrote:
> Thanks Pablo!
> this looks pretty clear and neat, I'll try it out.
> 
> Just one newbie question - is the "_"  in "acquire_cb(_)"  just a
> placeholder for my own parameters, or does it have an actual meaning?
> I've never seen this done in python before, and couldn't find other
> examples for it online...

It is just a convention for cases where you're not interested in the
argument ( DeferredLock.release() returns a reference of the lock )

Regards,
Pablo
> 
> 
> Pablo Martí wrote: 
> > 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()
> >         self.check_queue()
> > 
> >     def check_queue(self):
> >         self.queue.get().addCallback(self._process_one)
> >     
> >     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
> >             pass
> > 
> >         self.lock.acquire().addCallback(acquire_cb)
> > 
> >     def queue_petition(self, petition):
> >         self.queue.put(petition)
> >         return petition.deferred
> > 
> > Regards,
> > Pablo
> >   
> > 
> > 
> > 
> > _______________________________________________
> > Twisted-Python mailing list
> > Twisted-Python at twistedmatrix.com
> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> >   
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list