Setting thread priorities

Nick Vatamaniuc vatamane at gmail.com
Mon May 14 01:28:03 EDT 2007


On May 13, 2:46 am, John Nagle <n... at animats.com> wrote:
>    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


>>>




More information about the Python-list mailing list