[Twisted-Python] deferred TCP writes

Brian Granger bgranger at scu.edu
Tue Apr 18 12:21:46 MDT 2006


On 4/18/06, Itamar Shtull-Trauring <itamar at itamarst.org> wrote:
> On Tue, 2006-04-18 at 10:44 -0700, Brian Granger wrote:
> > I have run into this issue as well when trying to order different
> > writes and make code run _after_ a write was completed.  I ended up
> > calling my code using deferToThread and putting in time.sleep calls to
> > make sure things got ordered correctly.  Ugly hack though.
>
> Eeeeew. Don't do that.

I though my hack would get a good response :)

> > I echo the question: why doesn't write return a deferred?  Surely
> > there are times you want to trigger events on the completion of a
> > write.
>
> This is (more or less) what the producer API is for. Register a
> IConsumer with transport.registerProducer and you will get callbacks
> telling you when the transport buffer becomes empty or full.
>
> Returning Deferred from every transport.write would make everything
> rather slow... a wrapper that does this could be probably be written
> using the producer/consumer API.
>

Fantastic, I wasn't aware of this API.  I will defintely use this
approach from now on.

Thanks for the pointer.

Brian

--
Brian Granger
Santa Clara University
ellisonbg at gmail.com




More information about the Twisted-Python mailing list