Async serial communication/threads sharing data

Jean-Paul Calderone exarkun at divmod.com
Sun Mar 22 21:53:10 EDT 2009


On Sun, 22 Mar 2009 12:30:04 -0500, Nick Craig-Wood <nick at craig-wood.com> wrote:
> [snip]
>
>I wrote a serial port to TCP proxy (with logging) with twisted.  The
>problem I had was that twisted serial ports didn't seem to have any
>back pressure.  By that I mean I could pump data into a 9600 baud
>serial port at 10 Mbit/s.  Twisted would then buffer the data for me
>using 10s or 100s or Megabytes of RAM.  No data would be lost, but
>there would be hours of latency and my program would use up all my RAM
>and explode.
>
>What I wanted to happen was for twisted to stop taking the data when
>the serial port buffer was full and to only take the data at 9600
>baud.
>
>I never did solve that problem :-(
>

This is what Twisted's producers and consumers let you do.  There's a
document covering these features:

http://twistedmatrix.com/projects/core/documentation/howto/producers.html

In the case of a TCP to serial forwarder, you don't actually have to
implement either a producer or a consumer, since both the TCP connection
and the serial connection are already both producers and consumers.  All
you need to do is hook them up to each other so that when the send buffer
of one fills up, the other one gets paused, and when the buffer is empty
again, it gets resumed.

Hope this helps!

Jean-Paul



More information about the Python-list mailing list