470,831 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

Setting thread priorities

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
3 30705
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
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
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.

Similar topics

reply views Thread by Gary Robinson | last post: by
31 posts views Thread by AlexeiOst | last post: by
3 posts views Thread by Stu Banter | last post: by
7 posts views Thread by Droopy | last post: by
6 posts views Thread by Dave A | last post: by
9 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.