Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102699

Re: Help using Thread (or other method)

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Help using Thread (or other method)
Date 2016-02-09 11:17 +1100
Message-ID <mailman.116.1454977078.2317.python-list@python.org> (permalink)
References <56B901AF.5030602@etrix.com.au>

Show all headers | View raw


On Tue, Feb 9, 2016 at 7:59 AM, Brendan Simon (eTRIX)
<brendan.simon@etrix.com.au> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Help using Thread (or other method) Chris Angelico <rosuav@gmail.com> - 2016-02-09 11:17 +1100

csiph-web