Setting thread priorities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Nagle

    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
  • Gerald Kaszuba

    #2
    Re: Setting thread priorities

    Hi John

    On May 13, 4:46 pm, John Nagle <n...@animats.c omwrote:
    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_setsche dparam() for UNIX and SetThreadPriori ty()
    for Windows.

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

    Gerald
    Ada pola baru dalam cara orang main togel online, tapi jarang dibahas secara langsung. JALANTOTO jadi salah satu yang sering muncul dalam perubahan ini tanpa banyak disadari.


    Comment

    • Grant Edwards

      #3
      Re: Setting thread priorities

      On 2007-05-13, Gerald Kaszuba <gerald.kaszuba @gmail.comwrote :
      Hi John
      >
      On May 13, 4:46 pm, John Nagle <n...@animats.c omwrote:
      > 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_setsche dparam() for UNIX and SetThreadPriori ty()
      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
      grante@visi.com

      Comment

      • Nick Vatamaniuc

        #4
        Re: Setting thread priorities

        On May 13, 2:46 am, John Nagle <n...@animats.c omwrote:
        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,p ri):
        ...: 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

        >>>

        Comment

        Working...