Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

[Newbie] How to wait for asyncronous input

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pozzugno at gmail

Aug 10, 2012, 2:25 PM

Post #1 of 4 (322 views)
Permalink
[Newbie] How to wait for asyncronous input

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.
--
http://mail.python.org/mailman/listinfo/python-list


pozzugno at gmail

Aug 11, 2012, 12:07 AM

Post #2 of 4 (318 views)
Permalink
Re: [Newbie] How to wait for asyncronous input [In reply to]

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 (I don't know why, but I think it will be possible).

--
http://mail.python.org/mailman/listinfo/python-list


hansmu at xs4all

Aug 11, 2012, 2:24 AM

Post #3 of 4 (315 views)
Permalink
Re: [Newbie] How to wait for asyncronous input [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Aug 11, 2012, 2:56 AM

Post #4 of 4 (317 views)
Permalink
Re: [Newbie] How to wait for asyncronous input [In reply to]

On Sat, 11 Aug 2012 11:24:48 +0200, Hans Mulder wrote:

> On 11/08/12 09:07:51, pozz wrote:

>> 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.

Correct.

> 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.

It doesn't need to be a global flag. You can make the flag an attribute
of the thread, and then have the thread check self.flag.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.