Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26905
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: [Newbie] How to wait for asyncronous input |
| Date | 2012-08-10 19:12 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <k03u8u$lv4$1@nnrp.ngi.it> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3191.1344640333.4697.python-list@python.org> (permalink) |
On Fri, 10 Aug 2012 23:25:51 +0200, pozz <pozzugno@gmail.com> declaimed
the following in gmane.comp.python.general:
> 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))
>
I sure hope that "data" gets returned to the main thread under some
condition <G>
> 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.
>
What you apparently missed is that serial.read() BLOCKs until data
is available (unless the port was opened with a read timeout set).
http://pyserial.sourceforge.net/pyserial_api.html
api> read(size=1)¶
api> Parameters: * size – Number of bytes to read.
api>
api> Returns: Bytes read from the port.
api>
api> Read size bytes from the serial port. If a timeout is set it
may return less characters as requested. With no timeout it will block
until the requested number of bytes is read.
serial.read() may, there for, be using select() behind the scenes.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[Newbie] How to wait for asyncronous input pozz <pozzugno@gmail.com> - 2012-08-10 23:25 +0200
Re: [Newbie] How to wait for asyncronous input Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-10 19:12 -0400
Re: [Newbie] How to wait for asyncronous input pozz <pozzugno@gmail.com> - 2012-08-11 09:07 +0200
Re: [Newbie] How to wait for asyncronous input Hans Mulder <hansmu@xs4all.nl> - 2012-08-11 11:24 +0200
Re: [Newbie] How to wait for asyncronous input Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-11 09:56 +0000
Re: [Newbie] How to wait for asyncronous input Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-11 14:21 -0400
csiph-web