[Newbie] How to wait for asyncronous input

Hans Mulder hansmu at xs4all.nl
Sat Aug 11 11:24:48 CEST 2012


On 11/08/12 09:07:51, pozz wrote:
> Il 11/08/2012 01:12, Dennis Lee Bieber ha scritto:
>>     What you apparently missed is that serial.read() BLOCKs until data
>> is available (unless the port was opened with a read timeout set).
>> [...]
>>
>>     serial.read() may, there for, be using select() behind the scenes.
> 
> Hmm..., so I could open the serial port with timeout=0 so the read(),
> that runs in a different thread, would block forever, so putting the
> thread in a sleep state until some bytes arrive.
> 
> When the main thread wants to close the serial port, the receiving
> thread can be killed


How would you do that?

IFAIK, there is no way in Python to kill a thread.


The usual work-around is to have a global flag that you'd set
to True when the main thread wants to close the port.  The read
would have a timeout of 1 second of so, so that the reading
thread is woken up every second, so it can check that flag and
wind up when the flag is set.


Hope this helps,

-- HansM



More information about the Python-list mailing list