Help using Thread (or other method)

Chris Angelico rosuav at gmail.com
Mon Feb 8 19:17:50 EST 2016


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



More information about the Python-list mailing list