[Newbie] How to wait for asyncronous input

pozz pozzugno at gmail.com
Fri Aug 10 17:25:51 EDT 2012


I'm new to Python and I'm trying to code something to learn the language 
details.  I'm in trouble with asyncronous events, like asyncronous 
inputs (from serial port, from socket and so on).

Let's consider a simple monitor of the traffic on a serial port.  I'm 
using pyserial and I looked at the miniterm example:
http://pyserial.svn.sourceforge.net/viewvc/*checkout*/pyserial/trunk/pyserial/serial/tools/miniterm.py

The strategy is to create a thread that continuously call read() method 
while a couple of flags (alive and _reader_alive) are set:

     def reader(self):
         try:
             while self.alive and self._reader_alive:
                 data = character(self.serial.read(1))

Is it an optimized strategy to wait for asyncronous input from serial 
port?  I think in this way we have a thread that is always running and 
that always asks for new bytes from serial port.

In C I should have used the select() on the serial port file descriptor. 
  I think the select() put the thread in a sleep state and wake it up 
when some bytes are received on the file descriptor specified.

It seems the "low-level" approach of select() is more optimized then the 
"high-level" approach of miniterm.



More information about the Python-list mailing list