Python - Thread Scheduling

Python supports multiple threads in a program. A multi-threaded program can execute multiple sub-tasks independently, which allows the parallel execution of tasks.

Python interpreter maps Python thread requests to either POSIX/pthreads, or Windows threads. Hence, similar to ordinary threads, Python threads are handled by the host operating system.

However, there is no support for thread scheduling in the Python interpreter. Hence, thread priority, scheduling schemes, and thread pre-emption is not possible with the Python interpreter. The scheduling and context switching of Python threads is at the disposal of the host scheduler.

Python does have some support for task scheduling in the form of sched module as the standard library. It can be used in the creation of bots and other monitoring and automation applications. The sched module implements a generic event scheduler for running tasks at specific times. It provides similar tools like task scheduler in windows or Linux.

The scheduler class is defined in the sched built-in module.

scheduler(timefunc=time.monotonic, delayfunc=time.sleep)

The methods defined in scheduler class include −

  • scheduler.enter() − Events can be scheduled to run after a delay, or at a specific time. To schedule them with a delay, enter() method is used.

  • scheduler.cancel() − Remove the event from the queue. If the event is not an event currently in the queue, this method will raise a ValueError.

  • − Run all scheduled events.

Events can be scheduled to run after a delay, or at a specific time. To schedule them with a delay, use the enter() method, which takes four arguments.

  • A number representing the delay

  • A priority value

  • The function to call

  • A tuple of arguments for the function

Example 1

This example schedules two different events −

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def schedule_event(name, start):
   now = time.time()
   elapsed = int(now - start)
   print('elapsed=',elapsed, 'name=', name)

start = time.time()
print('START:', time.ctime(start))
scheduler.enter(2, 1, schedule_event, ('EVENT_1', start))
scheduler.enter(5, 1, schedule_event, ('EVENT_2', start))

It will produce the following output

START: Mon Jun 5 15:37:29 2023
elapsed= 2 name= EVENT_1
elapsed= 5 name= EVENT_2

Example 2

Let's take another example to understand the concept better −

import sched
from datetime import datetime
import time

def addition(a,b):
   print("Performing Addition : ",
   print("Time : ", time.monotonic())
   print("Result : ", a+b)

s = sched.scheduler()

print("Start Time : ",

event1 = s.enter(10, 1, addition, argument = (5,6))
print("Event Created : ", event1)
print("End Time : ",

It will produce the following output

Start Time : 2023-06-05 15:49:49.508400
Event Created : Event(time=774087.453, priority=1, sequence=0, action=<function addition at 0x000001FFE71A1080>, argument=(5, 6), kwargs={})
Performing Addition : 2023-06-05 15:49:59.512213
Time : 774087.484
Result : 11
End Time : 2023-06-05 15:49:59.559659
Kickstart Your Career

Get certified by completing the course

Get Started