473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 34674
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary Robinson | last post by:
In the application we're writing (http://www.goombah.com) it would be helpful for us to give one thread a higher priority than the others. We tried the recipe here:...
4
by: Trev Hunter | last post by:
Hi All, I was just wondering if it is safe to change thread priorities with thread pool threads (threads used by System.Threading.Timer and those used by the ThreadPool class itself). Say I...
31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
3
by: Stu Banter | last post by:
I have a couple of small single-thread Console utilities doing cpu-intensive stuff. Is there an easy way to set the program execution priority to LOW from within the program ? Now I pause the...
7
by: Droopy | last post by:
Hi, I don't understand why sometimes, a loop running in a thread takes much more time than usual. I have a thread that must call each 20 ms a C++ unmanaged function. I made a C++ managed...
6
by: Dave A | last post by:
I have a low priority background thread doing some async socket communications. The thread that kicks off the AsyncRead/AsyncWrite has its priority set to 'below normal'. But when the code...
0
by: herbert | last post by:
My application is a message gateway. It uses several threads to send, receive and log messages. The send thread is time critical, transmitting emergency stops. Using default settings it takes...
5
by: Tsair | last post by:
I set the thread culture in MAIN() as below in order to show the date in format DAY/MONTH/YEAR, but the datagridview alway show the date in M/d/yyyy. How to set the default Date format from...
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.