Setting thread priorities

J

John Nagle

There's no way to set thread priorities within Python, is there?
We have some threads that go compute-bound, and would like to
reduce their priority slightly so the other operations, like
accessing the database and servicing queries, aren't slowed
as much.

John Nagle
 
G

Gerald Kaszuba

Hi John

There's no way to set thread priorities within Python, is there?

Not exactly. You can however use the ctypes module to access the o/s
methods of pthread_setschedparam() for UNIX and SetThreadPriority()
for Windows.

I'm not sure why this hasn't been implemented in Python.

Gerald
http://geraldkaszuba.com/
 
G

Grant Edwards

Hi John



Not exactly. You can however use the ctypes module to access the o/s
methods of pthread_setschedparam() for UNIX and SetThreadPriority()
for Windows.

I'm not sure why this hasn't been implemented in Python.

AFAICT, Python's threading paradigm seems to be based on the
assumption that alls threads spend most of their time blocking
on I/O or some other event. In that case priorities don't
matter. Priorities only matter if multiple threads are ready to
run.
 
N

Nick Vatamaniuc

There's no way to set thread priorities within Python, is there?
We have some threads that go compute-bound, and would like to
reduce their priority slightly so the other operations, like
accessing the database and servicing queries, aren't slowed
as much.

John Nagle

John,

You can implicitly create a priority system if you let some threads
yield more to other threads. Say if your one thread has to have a
higher priority, you would make other threads sleep longer. Of course,
as someone mentioned, Python threads are mostly there to take
advantage of I/O blocking and not to take advantage of multiple CPU
cores (it can't do that at the moment anyway).

Check out the small snippet of code I wrote below. Each thread is
initialized with some priority value between 0.0 (the lowest) and up.
Then look at the run trace and notice that on average the 0.75
priority thread is called more often than the 1.0 priority.

Hope this helped,
-Nick Vatamaniuc

...: def __init__(self,pri):
...: Thread.__init__(self)
...: self.pri=pri
...: def run(self):
...: for i in range(20):
...: sleep(1.0*self.pri)
...: print " -> thread with priority:",self.pri
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 0.75
-> thread with priority: 1.0
-> thread with priority: 1.0
-> thread with priority: 1.0
-> thread with priority: 1.0
-> thread with priority: 1.0

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top