Async PySerial (was Re: pySerial Windows write problem)

Peter Hansen peter at engcorp.com
Fri Jul 29 14:16:07 CEST 2005


Neil Benn wrote:
> PySerial doesn;t have any kind of event firing to notify you when data 
> is available.  The way I get round this is to have a loop polling (in a 
> seperate thread) to see if any data is available (it's a method on the 
> interface), then read all the data in and fire this off to my 
> listeners/observers with the read data.  

On that note, I've got a preliminary version of something I call "Bent" 
(think "slightly Twisted") which is focused for now on providing an 
asynchronous version of PySerial which is designed around the 
event-driven model instead of its current polled scheme.

It shares a number of features with the Twisted-based PySerial port 
(also done by Chris Liechti, for the Twisted team, judging by the code 
comments) but doesn't require one to adopt the whole Twisted framework. 
  Basically it provides you with the equivalent of one "reactor" per 
thread for PySerial stuff.  On Win32 only for now.  There are other 
limitations too.  Not ready for prime time.

On the plus side, it's been letting me convert my serial stuff to be 
fully event-driven and with the resulting much lower latencies and 
better CPU efficiency, while keeping my code more "traditional" instead 
of having to force it entirely into the Twisted point of view.

Just an FYI.  And, obviously, to hear back from those interested.  I 
don't know if this is something that should be contributed to the 
PySerial project (it's more of a rewrite than an add-on), or set up as a 
new project, or passed around quietly behind the scenes for a while.
No docs yet, no contrived examples (but various pieces of working code 
in real daily use).  Did I mention it wasn't ready for prime time?

If you've ever been annoyed having to do .read(1) to get characters one 
at a time in PySerial, or .read(100) with a timeout, or whatever, this 
might be of interest to you...

-Peter



More information about the Python-list mailing list