Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Help using Thread (or other method) Date: Tue, 9 Feb 2016 11:17:50 +1100 Lines: 29 Message-ID: References: <56B901AF.5030602@etrix.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de bqs1d1bg6yRRY6J12rIibAj9KUZgN/ijtdy8uuRWhElQ== 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; 'received:209.85.223': 0.03; 'anyway.': 0.04; 'cpython': 0.05; 'model,': 0.05; 'great.': 0.07; 'cc:addr:python-list': 0.09; 'subject:method': 0.09; 'subject:using': 0.09; 'threads,': 0.09; 'url:github': 0.09; 'utilizing': 0.09; 'subject:Help': 0.10; 'python': 0.10; "(i'm": 0.16; '2016': 0.16; 'async': 0.16; 'etc?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gil.': 0.16; 'quoted': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Thread': 0.16; 'threads': 0.16; 'wrote:': 0.16; '(not': 0.20; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'trying': 0.22; 'am,': 0.23; 'select': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'rest': 0.26; 'message- id:@mail.gmail.com': 0.27; 'initial': 0.28; 'blocking': 0.29; 'gil': 0.29; 'subject:other': 0.29; "i'm": 0.30; 'code': 0.30; 'primary': 0.31; 'another': 0.32; 'problem': 0.33; 'http': 0.33; 'tue,': 0.34; 'this?': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'received:209.85': 0.36; 'subject:: ': 0.37; '(2)': 0.37; 'doing': 0.38; "won't": 0.38; 'received:209': 0.38; '(1)': 0.38; 'mean': 0.38; 'application': 0.39; 'ever': 0.60; 'high': 0.60; 'your': 0.60; 'fire': 0.63; 'soon': 0.65; 'risk': 0.68; 'obvious': 0.76; 'chrisa': 0.84; 'cpu,': 0.84; 'cpu.': 0.84; 'foremost,': 0.84; 'worried': 0.84; 'to:none': 0.91; 'responses': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=rW3fLHkhgbgdlvJWOvaYxaJfveikftU0uHFVkXSXdeY=; b=y+6NM3Xy//Mo3izDGkg50Brau9TXxi0v7zKT6Y/375O2ZRn5bkAbZIQcy3I0eWGOr0 HuV71pO+UzqXWQS9+7pWc68XoxY+BYvZWWy3bDjOe7A/5qa49IxmVnVIXskvZ97Vi9U0 XBqhg3ayjyj+43ywtNDoNd2xVSXrz6reKzlphxRbSZKDN7Qpfaj1mcje+94jEGkuUoIS GC3cJlfy6FJFD+dYpWCHSDPInND/S/Q1xBRJplPxb5jqDBoR9pfN75LtfVxtVWjEp/C9 7cqoV8c/EGe2DqMjtRq273w3Uelo9Rz3N0ZyTmbCMUGOkE27YgU4cqn+c2FgzwH5Uf+R UiPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=rW3fLHkhgbgdlvJWOvaYxaJfveikftU0uHFVkXSXdeY=; b=jn1cCn3hawUkisWqCFc7Oo7jaLXbXEsUMZTUUjPcEaKzgx+CbixNUkMGIsXu1Uqxba n7qt6fyn8z9pKVv7ky7OCHqmRkQvejZ1cWnmN1SLAp/lvLGt73eiYGTlvFBUEoZHepSu HaAy8gwiOnuE4XADnmzE/njRQJIDVNT1cn1jemn4rG9TvNA+OJzdvDAX2H4P7kba5wdw 0TNhfoPAFjKOOe5wCN90wKgwks7JINw0Qw3UBxMFmWD7ul5NS6FoF3Yhd/vvL0Lad72a q/79/t2byauWkBMzVouxUtz5u6LSquRdjfJuuqb9duHwOcgxHmXL8yRle4J8IpjJzXHC gPlA== X-Gm-Message-State: AG10YORSvKqFej57u5E+OPPfF2iw2pWZvR7HRUnWdMXuSmqjsSvozECf/tSbZZcTRongu9hm3tejfBtGtO+Ggg== X-Received: by 10.107.47.162 with SMTP id v34mr28167456iov.19.1454977070126; Mon, 08 Feb 2016 16:17:50 -0800 (PST) In-Reply-To: <56B901AF.5030602@etrix.com.au> 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:102699 On Tue, Feb 9, 2016 at 7:59 AM, Brendan Simon (eTRIX) wrote: > 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). > First and foremost, don't worry about the GIL. It's almost never a problem, so wait until you have proof of its guilt before you start trying to fire it. In your situation, your quoted problem is that HTTP requests take time - in other words, that your code is blocking - so nothing's going to be spinning in the CPU. The most obvious solutions to your initial problem are (1) threads, and (2) async I/O. If there's only one part of your code that's ever at risk of problematic blocking, you might be able to do that part asynchronously and most of the rest of the code with simple blocking calls; what that'd mean is that you won't process HTTP responses until your code next "falls idle" in its primary loop (waiting for another interrupt), which is pretty much what you'd have with any other scheme anyway. You could look into a Python 3.5 async/await model, using a library like this (never used it, just found it on Google): https://github.com/KeepSafe/aiohttp Otherwise, threads are great. ChrisA