Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'url:sourceforge': 0.03; 'subject:: [': 0.03; 'socket': 0.05; 'try:': 0.07; 'missed': 0.09; 'subject:How': 0.09; 'python': 0.09; 'descriptor': 0.09; 'may,': 0.09; 'port,': 0.09; 'read()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'requested.': 0.09; 'timeout': 0.09; 'looked': 0.10; 'def': 0.10; 'thread': 0.11; 'aug': 0.13; 'read.': 0.13; 'language': 0.14; 'blocks': 0.16; 'descriptor.': 0.16; 'for,': 0.16; 'inputs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set:': 0.16; 'specified.': 0.16; 'url:py': 0.16; 'url:svn': 0.16; 'wake': 0.16; 'bytes': 0.17; 'input': 0.18; 'subject:] ': 0.19; 'trying': 0.21; 'flags': 0.22; 'port.': 0.22; 'header:X -Complaints-To:1': 0.28; 'trouble': 0.28; '(unless': 0.29; 'asks': 0.29; 'sleep': 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; '(from': 0.30; 'fri,': 0.30; 'returned': 0.30; 'code': 0.31; 'gets': 0.32; 'file': 0.32; 'running': 0.32; '+0200,': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'monitor': 0.33; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'characters': 0.36; 'method': 0.36; 'should': 0.36; 'data': 0.37; 'behind': 0.38; 'some': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'traffic': 0.61; 'strategy': 0.64; 'serial': 0.66; 'subject:wait': 0.84; 'url:serial': 0.84; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: [Newbie] How to wait for asyncronous input Date: Fri, 10 Aug 2012 19:12:02 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-76-253-109-229.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344640333 news.xs4all.nl 6928 [2001:888:2000:d::a6]:38988 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26905 On Fri, 10 Aug 2012 23:25:51 +0200, pozz 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 > 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/