Created
September 21, 2023 11:08
-
-
Save meta-ks/54b2227368a47a6fc2b3bdaa6a8e596a to your computer and use it in GitHub Desktop.
This script demonstrates the adjustment of thread priorities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import os | |
import signal | |
import time | |
""" | |
This script demonstrates the adjustment of thread priorities and handling Ctrl+C to gracefully stop threads. | |
Thread Priority Mechanism: | |
- The `thread_priority_test` function creates multiple threads with different niceness values. | |
- It uses the `os.nice()` function to set thread niceness, which is similar to thread priority. | |
- If the niceness is set to 0 (default), it attempts to set thread priority explicitly on Linux systems using the `setpriority` function. | |
- Each thread runs a loop until a termination signal is received (e.g., Ctrl+C). | |
- During the loop, it continuously updates its niceness value and maintains a dictionary of statistics. | |
Ctrl+C Signal Handling: | |
- The `signal_handler` function is set up to handle Ctrl+C signals. | |
- When Ctrl+C is detected, it sets the `kill_event`, signaling all threads to stop gracefully. | |
Usage: | |
- Threads are created with different niceness values (0, -2, -4). | |
- The script uses the `threading` module to manage threads. | |
- To stop the threads, press Ctrl+C, which triggers the signal handler and stops all threads. | |
- The statistics of thread niceness and counts are stored in the `cntr_stats_dict`. | |
Note: | |
- Thread priorities might not work on all systems, and explicit priority settings are typically limited to Unix/Linux. | |
- The script provides a basic demonstration of priority adjustment and signal handling. | |
Sample output with diff niceness: | |
{19: 206808916, 15: 215252820, 10: 219746028, 5: 226310260, 0: 231552779, -5: 242480906, -10: 249047980, -15: 254036155, -20: 253950489} | |
Niceness -20 (highest priority) enjoyed max count as it might have got more os time. | |
""" | |
# Set up a signal handler for Ctrl+C | |
def signal_handler(sig, frame): | |
print("Ctrl+C detected. Stopping threads...") | |
kill_event.set() | |
def thread_priority_test(tag, niceness): | |
print(f'[*]Starting {tag} thread....') | |
ix = 0 | |
os.nice(niceness) | |
# if niceness == 0: | |
# setpriority(tag, linux_nice=20) | |
while not kill_event.is_set(): | |
niceness = os.nice(0) | |
ix += 1 | |
cntr_stats_dict[niceness] = ix | |
kill_event = threading.Event() | |
nvalues = range(0, -5, -2) | |
cntr_stats_dict = dict() | |
threads = [threading.Thread(target=thread_priority_test, args=(f'Test_{nvalue}', nvalue)) for nvalue in nvalues] | |
signal.signal(signal.SIGINT, signal_handler) | |
for thread in threads: | |
thread.start() | |
while not kill_event.is_set(): | |
time.sleep(3) | |
print(cntr_stats_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script demonstrates the adjustment of thread priorities using os.nice()
Thread Priority Mechanism:
thread_priority_test
function creates multiple threads with different niceness values.os.nice()
function to set thread niceness, which is similar to thread priority.Ctrl+C Signal Handling:
signal_handler
function is set up to handle Ctrl+C signals.kill_event
, signaling all threads to stop gracefully.Usage:
threading
module to manage threads.cntr_stats_dict
.Note:
Sample output with diff niceness:
.
.
.
{19: 30465492, 15: 32170064, 10: 32333765, 5: 33807670, 0: 34757313, -5: 35629011, -10: 36933416, -15: 37232012, -20: 38131850}
{19: 30629167, 15: 32293583, 10: 32511094, 5: 33979049, 0: 34933933, -5: 35851529, -10: 37110963, -15: 37382310, -20: 38321260}
{19: 30757137, 15: 32382210, 10: 32615585, 5: 34209006, 0: 35110487, -5: 36092311, -10: 37275568, -15: 37575795, -20: 38537091}
{19: 30924631, 15: 32529657, 10: 32810417, 5: 34379821, 0: 35350722, -5: 36272427, -10: 37426454, -15: 37817350, -20: 38724243}
.
.
.
{19: 205842802, 15: 214267971, 10: 218849437, 5: 225239074, 0: 230610955, -5: 241416151, -10: 247995994, -15: 253017885, -20: 252717624}
{19: 205977182, 15: 214406245, 10: 218945774, 5: 225340416, 0: 230725614, -5: 241551976, -10: 248129734, -15: 253131631, -20: 252900676}
{19: 206104915, 15: 214537418, 10: 219056155, 5: 225481394, 0: 230844420, -5: 241665192, -10: 248241036, -15: 253251922, -20: 253072552}
{19: 206262874, 15: 214702527, 10: 219151508, 5: 225571215, 0: 230989691, -5: 241776664, -10: 248389458, -15: 253365640, -20: 253217771}