Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Brendan Simon (eTRIX)" Newsgroups: comp.lang.python Subject: Help using Thread (or other method) Date: Tue, 9 Feb 2016 07:59:27 +1100 Organization: eTRIX pty ltd Lines: 67 Sender: Brendan Simon Message-ID: Reply-To: brendan.simon@etrix.com.au Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de y1BzDUYKJfuk/jd7dxsqRATzQxDMfPmFVMtNWNWPS5Cg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; '(using': 0.07; '[],': 0.07; 'data:': 0.07; 'run,': 0.07; '0.1': 0.09; 'descriptor': 0.09; 'module)': 0.09; 'subject:method': 0.09; 'subject:using': 0.09; 'timeout': 0.09; 'utilizing': 0.09; 'subject:Help': 0.10; 'thread': 0.10; 'python': 0.10; 'def': 0.13; 'apps': 0.15; "(i'm": 0.16; 'advice,': 0.16; 'descriptors,': 0.16; 'etc?': 0.16; 'mapped': 0.16; 'module).': 0.16; 'pulling': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sees': 0.16; 'station.': 0.16; 'subject:Thread': 0.16; 'threads': 0.16; 'timeout:': 0.16; 'wake': 0.16; 'app': 0.16; 'memory': 0.17; 'sender:addr:gmail.com': 0.18; '(not': 0.20; '(see': 0.20; 'first,': 0.20; 'arm': 0.22; 'assuming': 0.22; 'select': 0.23; 'help.': 0.23; 'seems': 0.23; 'requests': 0.25; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; 'linux': 0.26; 'separate': 0.27; 'skip:# 10': 0.27; 'far,': 0.29; 'gil': 0.29; 'queue': 0.29; 'readline': 0.29; 'subject:other': 0.29; 'character': 0.29; "i'm": 0.30; 'server.': 0.30; 'seconds': 0.31; 'skip:s 30': 0.31; 'post': 0.31; 'skip:_ 10': 0.32; 'posting': 0.32; 'embedded': 0.32; 'problem': 0.33; 'http': 0.33; 'right?': 0.33; 'stream': 0.33; 'file': 0.34; 'this?': 0.34; 'running': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'i.e.': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'thanks': 0.37; 'doing': 0.38; 'received:209': 0.38; 'data': 0.39; 'does': 0.39; 'application': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'high': 0.60; 'devices': 0.61; 'sample': 0.63; 'interest': 0.64; 'grab': 0.64; 'soon': 0.65; '\xc2\xa0\xc2\xa0': 0.66; 'here': 0.66; 'skip:\xc2 10': 0.67; 'urgent': 0.67; 'header:Reply-To:1': 0.67; '8bit%:100': 0.70; 'serial': 0.70; 'records': 0.70; 'reply- to:no real name:2**0': 0.71; 'gathering': 0.76; 'sounds': 0.76; 'cpu,': 0.84; 'ok?': 0.84; 'temperature': 0.84; 'weather': 0.84; 'worried': 0.84; 'interrupt': 0.91; 'device.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:to:reply-to:from:subject:organization:message-id:date :user-agent:mime-version:content-type; bh=XEEfsgJmSs62bG4YO8hUfDZQC76K0qVl1coes2J/mbo=; b=OM3F2/7C8NVElGaB79b/INLGdp4ItpYKBizEfWSSAULjybL8OLWTLoI8eeT+diiP/z oTIjtOsIGmdLrPlNQSmXAyHZ2oelcT1Hbmw6Dd56jPNFwDa3Dtd9Bw+Yyb8vNZQqguhq ExxU6HTc2xyWSI3o2beuRYJbIpKANVctul5vHjqOWlPuxsOmBfx7pAOA4tJ6abhmX80B ZpGvC081lfpLKgfJj7z4MtZ4DE1L5Tpiwb6gTd8G2fbn6d4E0aC8/dzVMW3ymhgMH+YR Eh43WY7nHQUtQms26qRc/1OalfWFini9Q5P/zLwnNJ7opbhEEXKzRT0jUvI8HM70/t0x GTaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:to:reply-to:from:subject:organization :message-id:date:user-agent:mime-version:content-type; bh=XEEfsgJmSs62bG4YO8hUfDZQC76K0qVl1coes2J/mbo=; b=bknjdFZbc0eixa1rhhTGvalKN0g2K3smzQ7fRknhpwJ1AB8dl8IjC4wEgVsos0fxgj u2YfikDrPsu8f/xPDO3PbUI8/RUkTnjCTp1s74LxVvKMKDI2HfcqjyMnPvRsVbsw4/OD ymMUOkSzyyc8LRoyy1W1ZoDfCVtxHwQnbfhn22v0cUffWQtzYqV+KhFC1NRVLpyeuk4M 5Tl8+rwM3etP7q0WZ4PLTeHWTBZwx11YMEsplk9DNOCnS9NSuqiA9HLIxQZUupvdkdHt BUtQyrdGQDQQK7O4/uMPjmD/aob5EioXhE0U4gyR0E3OwBD4J508MiXwt3Wt8UpQW3oa LsTw== X-Gm-Message-State: AG10YOQNX2+EshW4cMFLy+isDOiyr1SV3QyXdsTrTEY9ZiURdV+9gZCh694w7mXMs0YpRA== X-Received: by 10.98.74.93 with SMTP id x90mr25500117pfa.80.1454965171516; Mon, 08 Feb 2016 12:59:31 -0800 (PST) X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102691 Hi. Need some urgent help. I have a python app that uses `select` to wait for data from an arm embedded linux kernel interrupt (every second). The mainloop of the app then grabs data from some memory mapped area, processes it and then does a http post to a server. The problem is the http post can take multiple seconds to respond and blocks (using the requests module). So I've put the posting in a separate thread (using Thread module) and send data to it via a queue (using the Queue module). The http post can send multiple records by pulling multiple items off the queue. Sounds good so far, right? This seems to work ok, but I also need to grab data from some other serial devices (gps, weather station) to put in the post. Various sample apps on the web use Threads to do this, so I am doing the same. One of the threads uses `select` (see below), following by readlines() on the device. def __init__(self): select_timeout = 1 serial_timeout = 0.1 def wait_and_process(self): '''wait for data and process it.''' r = select.select([self.serial_dev], [], [], self.select_timeout) if not r[0]: #print("DEBUG: TIMEOUT: wait_and_process") return #print("DEBUG: Weather Data Captured") for s in self.serial_dev.readlines(): #print("INFO: Data: {}".format(s)) temperature = extract_temperature(s) Is using `select` here redundant? It's used to block the thread until the first character is ready to be read from the serial device? Then I use serial_dev.readlines() to read the stream of chars from the weather station. Is that ok? Does readline block until it sees an end-of-line char? i.e. does it only wake up the thread when it has a _complete string_ or a timeout of 0.1 seconds? or will the process block other threads from running while it is gathering the chars until the newline? I'm assuming the gathering of chars will happen in the background and allow my main thread to run, correct? My application mainloop (not the threads) needs to be high priority and always process as soon as it gets an interrupt. Is using select a good way of doing this? How can I ensure that no other threads are utilizing the CPU, etc? I'm worried about the GIL (I'm using CPython 2.7). Is there a better way of structuring this to ensure I process the interrupt and not get interrupt overrun? Is using select only in the mainloop, with multiple file descriptors, a better way of doing things, so that I can process the file descriptor of interest first, before any others if set? Is using the multiprocessing module a better option? Thanks for any advice, Brendan.