By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Setting thread priorities

P: n/a
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
May 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi John

On May 13, 4:46 pm, John Nagle <n...@animats.comwrote:
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/

May 13 '07 #2

P: n/a
On 2007-05-13, Gerald Kaszuba <ge************@gmail.comwrote:
Hi John

On May 13, 4:46 pm, John Nagle <n...@animats.comwrote:
> 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.
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.

--
Grant Edwards
gr****@visi.com
May 13 '07 #3

P: n/a
On May 13, 2:46 am, John Nagle <n...@animats.comwrote:
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

>>from threading import Thread
from time import sleep
class Worker(Thread):
...: 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
>>w1=Worker(1.0); w2=Worker(0.75)
>>w1.start(); w2.start()
>> -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: 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

>>>
May 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.