Labs / Python parallel computing

A Quick Guide to Python Parallel Computing: 05 Python Multithreaded Programming – Scheduled Execution of Threads

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents
A Quick Guide to Python Parallel Computing: 05 Python Multithreaded Programming – Scheduled Execution of Threads

Abstract:

This paper mainly elaborates on how to implement scheduled tasks in Python, which are mainly realized in four approaches: via the threading.Timer class, via the sched module, via the threading.Event event mechanism, and via the third-party library APScheduler. It also analyzes the respective application scenarios and basic methods in combination with sample codes.

347622978

For more information about us, you can refer to the cloud document: Freak Embedded Studio Cloud Docs, or visit our wiki: https://github.com/leezisheng/Doc/wik

FreakStudio's Blog

Previous Recommendations:

You who are learning embedded systems, don’t you even know Object Oriented yet? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 03 Python Implementation of Classes and Objects – Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 04 Python Implementation of Classes and Objects - Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 05 Python Implementation of Classes and Objects - PyCharm Code Tab

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 06 Python Implementation of Classes and Objects - Data Encapsulation of Custom Classes

The most beginner-friendly Object Oriented programming tutorial across the entire network: 07 Python implementation of classes and objects - type annotations

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 09 Python Implementation of Classes and Objects - Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 13 Python Implementation of Classes and Objects – Installation and Usage of Sourcetrail, a Visual Code Reading Tool

The Most Beginner-Friendly Object Oriented Programming Course on the Internet: The Most Beginner-Friendly Object Oriented Programming Course on the Internet: 14 Python Implementation of Classes and Objects - Static Methods and Class Methods of Classes

The most beginner-friendly Object Oriented programming tutorial across the entire network: 15 Python implementation of classes and objects - the __slots__ magic method

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Internet: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial across the network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 18 Python Implementation of Classes and Objects - Multiple Inheritance and Drawing Curve Graphs of Serial Port Data with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 19 Python Implementation of Classes and Objects – Using PyCharm to Automatically Generate File Comments and function Comments

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 20 Python Implementation of Classes and Objects - Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 22 Python Implementation of Classes and Objects - Exceptions and Syntax Errors

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 24 Python Implementation of Classes and Objects - Exception Capture and Handling

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 25 Python Implementation of Classes and Objects - Python Determines Input Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 26 Python Implementation of Classes and Objects - Context Managers and the with Statement

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 27 Python Implementation of Classes and Objects - Implementation of Exception Hierarchy and Custom Exception Classes in Python

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 28 Python Implementation of Classes and Objects - A Comprehensive Summary of Python Programming Principles, Philosophy and Specifications

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function

The most beginner-friendly Object Oriented programming tutorial across the web: 30 Built-in data types in Python - the object root class

The most beginner-friendly Object Oriented programming tutorial across the entire network: 31 Built-in data types in Python - Object and Type

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 32 Python's Built-in Data Types - Class and Instance

The most beginner-friendly Object Oriented programming tutorial on the entire network: 33 Built-in data types in Python – the relationship between objects and types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 34 Python's Built-in Data Types - Commonly Used Composite Data Types in Python: Tuples and Named Tuples

The most beginner-friendly Object Oriented programming tutorial on the entire network: 35 Python's built-in data types - docstrings and the __doc__ attribute

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 36 Python's Built-in Data Type - Dictionary

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 37 Common Composite Data Types in Python - Lists and List Comprehensions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 38 Common Composite Data Types in Python – Implementing stack, Queue and Deque with Lists

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 39 Common Composite Data Types in Python - Sets

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 40 Common Composite Data Types in Python – Usage of Enumerations and the enum Module

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 41 Common Composite Data Types in Python - Queues (FIFO, LIFO, Priority Queues, Deques and Circular Queues)

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 42 Commonly Used Composite Data Types in Python - Collections Container Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 43 Common Composite Data Types in Python - Extending Built-in Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 44 Python Built-in functions and Magic Methods – Overriding Magic Methods of Built-in Types

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 45 Implementing Common Data Structures in Python – Linked List, Tree, Hash Table, Graph and Heap

The most beginner-friendly Object Oriented programming tutorial across the network: 46 Python function methods and interfaces - functions and event-driven frameworks

The most beginner-friendly Object Oriented programming tutorial across the network: 47 Python function methods and interfaces - Callback function

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 48 Python function Methods and Interfaces - Positional Arguments, Default Arguments, Variable Arguments and Keyword Arguments

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 49 Python function Methods and Interfaces – The Difference Between function and Methods, and Lambda Anonymous Functions

The most beginner-friendly Object Oriented programming tutorial on the entire network: 50 Python function methods and interfaces - Interfaces and Abstract Base Classes

The most beginner-friendly Object Oriented programming tutorial on the entire network: 51 Python function methods and interfaces - Implementing interfaces with Zope

The most beginner-friendly Object Oriented programming tutorial across the network: 52 Python function methods and interfaces - Protocol and interfaces

The most beginner-friendly Object Oriented programming tutorial across the entire network: 53 Python Strings and Serialization - Strings and Character Encodings

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 54 Python Strings and Serialization - String Formatting and the format Method

The most beginner-friendly Object Oriented programming tutorial on the entire network: 55 Python Strings and Serialization - Byte Sequence Types and Mutable Byte Strings

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 56 Python Strings & Serialization - Regular Expression and re Module Application

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 57 Python Strings and Serialization - Serialization and Deserialization

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 58 Python Strings and Serialization – Definition and Implementation of Serialized Web Objects

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 59 Python Parallelism and Concurrency - Parallelism & Concurrency and Threads & Processes

Master Python Parallel Computing in One Article: 00 Basic Concepts of Parallel Computing

A Quick Guide to Python Parallel Computing: 01 Python Multithreaded Programming – Basic Concepts, Switching Process, GIL Lock Mechanism, and Producer-Consumer Model

A Quick Guide to Python Parallel Computing: 02 Python Multithreaded Programming - threading Module, Thread Creation & Query, and Daemon Threads

A Quick Guide to Python Parallel Computing: 03 Python Multithreaded Programming - Multithread Synchronization (Part 1) — Based on Mutex Locks, Recursion Locks and Semaphores

A Quick Guide to Python Parallel Computing: 04 Python Multithreaded Programming - Multithread Synchronization (Part 2) — Based on Condition Variables, Events and Barriers

More exciting content can be found here:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

Understand the Debugging Principle of CM3 Microcontroller in One Article

After half a month of dedicated work, the ultimate summary of embedded technology stack is now released

The "Martial Arts Manual" for Computer Science Competitions

An open-source project collection for MicroPython: awesome-micropython, which covers MicroPython tool libraries in various aspects

Avnet ZUBoard 1CG Development Board — A New Choice for Deep learning

Engineers should not blindly worship open-source code; they also need to focus on mastering fundamental skills.

What? A motor drive module with customizable color schemes? !

What? Three brand-new expansion boards have just been released for the XIAO main controller!

A Step-by-Step Guide to Publishing Your Third-Party Library for Arduino

A 10,000-Word Comprehensive Guide: Step-by-Step Instructions to Publish Third-Party Libraries for MicroPython/Python

Document Acquisition:

You can download the document via the following link:

https://github.com/leezisheng/Doc

This document is a learning guide on parallel computing and Python concurrent programming, which covers the core knowledge points including the basic concepts of parallel computing, Python multi-threaded programming, multi-process programming and coroutine programming:

10

Body Text

1. Using the threading.Timer class

Timer inherits from the Thread class, which is a subclass of Thread and also a thread class with the capabilities and features of a thread. This class is used to define how often to execute a function. Its instance is a thread that can delay the execution of the target function, and you can cancel it before the target function is actually executed.

threading.Timer(interval, function, args=None, kwargs=None)

Create a timer that, after an interval of interval seconds, will call args and keyword arguments kwargs to invoke function. If args is None, an empty list will be used. If kwargs is None, an empty dictionary will be used.

Just like threads, timers are also started by calling their Timer.start method. A timer can be stopped (before its action begins) by calling the cancel () method. The time interval that a timer waits before executing its action may not be exactly the same as the interval specified by the user.

The following is the sample code for a scheduled alarm clock:

import threading
def thread_Timer():
    print("该起床啦...5秒之后再次呼叫你起床...")
    # Declare global variables
    global t1
    # Create and initialize the thread
    t1 = threading.Timer(5, thread_Timer)
    # Start the threads
    t1.start()
if __name__ == "__main__":
    # Create and initialize the thread
    t1 = threading.Timer(5, thread_Timer)
    # Start the threads
    t1.start()

A thread timer is created in the main thread, which executes the thread_Timer thread function after 5 seconds, and when the thread_Timer function finishes, the timer thread thread_Timer is set again, which completes a recursion operation to repeat the timed task at 5-second intervals!

Note that the above code will produce inexplicable errors in Python 3.12. For details and solutions, please refer to: https://github.com/python/cpython/issues/113964

113964 ambrose5 3.12 3.10 3.11

2. Using the sched module

sched is a scheduling module in the Python standard library, suitable for one-off task scheduling, and executes tasks based on time.sleep.

import sched
import time

# Create the scheduler
scheduler = sched.scheduler(time.time, time.sleep)

def task():
    print(f"任务执行了!当前时间:{time.strftime('%X')}")

# Run task after 5 seconds
scheduler.enter(5, 1, task, ())

print("任务已安排,主线程继续运行...")
scheduler.run()  # Start scheduling

In the above code, scheduler.enter (5,1, task, ()) runs the task function after 5 seconds, scheduler.run () starts the scheduler and will block the main thread until the task is completed.

It is applicable to one-off tasks, does not support periodic tasks, will not create new threads, and will block the main thread during task execution; the scheduling accuracy is limited by time.sleep, and may not be very accurate.

3. Using the threading.Event class

threading.Event is a kind of thread synchronization mechanism that is applicable to periodic tasks and enables dynamic task termination while the task is running.

import threading
import time

def periodic_task(interval, stop_event):
    while not stop_event.is_set():  # Keep looping as long as stop_event is not set
        print(f"执行周期性任务... 当前时间:{time.strftime('%X')}")
        stop_event.wait(interval)  # Wait for the specified time interval

# Create a stop event
stop_event = threading.Event()

# Start the scheduled task, running every 5 seconds
thread = threading.Thread(target=periodic_task, args=(5, stop_event))
thread.start()

# Run for a while, then stop
time.sleep(20)
stop_event.set()  # Trigger the stop event
thread.join()  # Wait for the thread to finish
print("定时任务已停止。")

threading.Thread starts an independent thread to run periodic_task without blocking the main thread. stop_event.wait (interval) controls the interval and allows the task to be stopped at any time. stop_event.set () terminates the task, and thread.join () waits for the task to finish.

It is suitable for periodic tasks and more flexible than sched. Meanwhile, tasks can be terminated at any time to avoid the infinite loop of while True, and it does not block the main thread, making it ideal for scheduled background tasks; however, since threading is affected by the GIL (Global Interpreter Lock), it is not suitable for CPU-intensive tasks.

4. Using the APScheduler Library

APScheduler is a powerful scheduled task library for Python, supporting one-time tasks, fixed-interval tasks, scheduled (Cron) tasks, etc. It is suitable for complex scheduled tasks that require precise scheduling (such as running at 8:00 every day), and supports multiple scheduling methods including interval execution, specific-time execution, and Cron expressions.

First, install the APScheduler library via the pip tool:

pip install apscheduler

The following is a routine for scheduled task execution:

from apscheduler.schedulers.background import BackgroundScheduler
import time

def job():
    print(f"定时任务执行了!当前时间:{time.strftime('%X')}")

scheduler = BackgroundScheduler()
scheduler.add_job(job, 'interval', seconds=5)  # Run every 5 seconds
scheduler.start()

try:
    while True:
        time.sleep(1)  # Keep the main thread running, otherwise the task will terminate
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()  # Shut down the scheduler

Here is a routine for executing a task at a specific time:

from apscheduler.schedulers.blocking import BlockingScheduler
import datetime

scheduler = BlockingScheduler()

def job():
    print("特定时间任务执行!")

# Run at 2025-03-30 18:30:00
run_time = datetime.datetime(2025, 3, 30, 18, 30, 0)
scheduler.add_job(job, 'date', run_date=run_time)

print("任务已安排...")
scheduler.start()  # Block the main thread