Labs / Python parallel computing

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

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

Contents
A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

Abstract:

This article introduces the usage of Python's ThreadPoolExecutor, covering the basic concepts of thread pools, Future objects, task submission methods (map/submit), tips for obtaining results, and the usage of VizTracer, a performance evaluation tool for multithreading.

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

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 Across the Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial Online: 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 entire network: 13 Python implementation of classes and objects - installation and usage of Sourcetrail, a visualization tool for reading code

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 - Organization of Multiple Files: Modules and Packages

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 on the Whole Network: 30 Python's Built-in Data Types - The object Root Class

The Best Beginner-Friendly Object Oriented Programming Course on the Whole Network: 31 Python's Built-in Data Types - 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 Across the Network: 41 Common Composite Data Types in Python - Queues (FIFO, LIFO, Priority Queue, Deque and Circular Queue)

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 Across the 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

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

A Quick Guide to Python Parallel Computing: 06 Python Multithreaded Programming – Communication Based on Queues

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

[Electronic DIY Essential] Works with All 5-Wire Stepper Motors! Compatible with Seeed XIAO Series Development Boards

Master the Electronic Design Competition in One Read: Award-Winning Tips Every Electronics Student Must See

Master the Optoelectronic Design Competition in One Read: A Must-See for Electronics Enthusiasts!

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:

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

Body Text

1. Basic Concepts and Usage of Thread Pools

The cost of the system starting a new thread is relatively high, as it involves interaction with the operating system. In this case, using a thread pool can effectively improve performance, and it is especially advisable to consider using a thread pool when the program needs to create a large number of threads with very short lifecycles.

The thread pool creates a large number of idle threads as soon as the system starts. As long as a program submits a function to the thread pool, the thread pool will start an idle thread to execute it. After the function finishes executing, the thread will not die; instead, it will return to the thread pool to be idle again, waiting to execute the next function.

Furthermore, using a thread pool can effectively control the number of concurrent threads in the system. When a system contains a large number of concurrent threads, the system performance will drop sharply, and even cause the Python interpreter to crash, while the maximum thread count parameter of the thread pool can control the number of concurrent threads in the system to not exceed this value.

1.1 ThreadPoolExecutor Class

The base class of thread pools is concurrent.futures module's Executor, Executor provides two subclasses, namely ThreadPoolExecutor and ProcessPoolExecutor, where ThreadPoolExecutor is used to create thread pools, while ProcessPoolExecutor is used to create process pools.

If you use a thread pool to manage concurrent programming, you only need to submit the corresponding task function to the thread pool, and the thread pool will handle the rest.

Executor provides the following common methods:

1.2 Future Object

After the program submits the task function (via submit) to the thread pool, the submit method will return a Future object. The Future class is mainly used to obtain the return value of the thread task function. Since thread tasks are executed asynchronously in a new thread, the function executed by the thread is equivalent to a "to be completed" task, which is why Python uses Future to represent it.

Future provides the following methods:

After you finish using a thread pool, you should call the shutdown () method of this thread pool, which will initiate the shutdown sequence of the thread pool. A thread pool that has had the shutdown () method called will no longer accept new tasks, but will complete execution of all previously submitted tasks. When all tasks in the thread pool have finished executing, all threads in the thread pool will die.

1.3 Steps for Using Thread Pool

The steps to use a thread pool to execute thread tasks are as follows:

  1. Call ThreadPoolExecutor class constructor to create a thread pool;
  2. Define an ordinary function as a thread task;
  3. Call ThreadPoolExecutor object's submit () method to submit thread tasks;
  4. When you do not want to submit any tasks, call the ThreadPoolExecutor object's shutdown () method to shut down the thread pool.

1.3.1 Submit Tasks to the Thread Pool

In the following example, we define a task function task_function, which takes a task as a parameter and prints the task information. We also create a thread pool and use the submit () method to submit each task to the thread pool for execution. By using a thread pool, we can process multiple tasks in the queue concurrently and improve the execution efficiency of the program. Note that the size of the thread pool defaults to the number of CPU cores of the system, but you can also specify the size of the thread pool by passing the max_workers parameter.

import concurrent.futures

# Simulated task function
def task_function(task):
    print(f"Processing task: {task}")

# Create a thread pool
with concurrent.futures.ThreadPoolExecutor() as executor:
    # Create a task queue
    task_queue = ["Task 1", "Task 2", "Task 3", "Task 4", "Task 5"]

    # Submit tasks to the thread pool
    for task in task_queue:
        executor.submit(task_function, task)

    # Wait for all tasks to complete
    executor.shutdown()
A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

1.3.2 Obtain Thread Execution Result

The following code calls the Future 's result () method to obtain the return value of the thread task:

from concurrent.futures import ThreadPoolExecutor
import threading,time

def test(value1, value2=None):
    print("%s threading is printed %s, %s"%(threading.current_thread().name, value1, value2))
    time.sleep(2)
    return 'finished'
def test_result(future):
    print(future.result())

if __name__ == "__main__":
    threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
    for i in range(0,10):
        future = threadPool.submit(test, i,i+1)
        print(future.result())

    threadPool.shutdown(wait=True)
    print('main finished')
A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

However, this method will block the current main thread, and the blocking will not be released until the subtask is completed. result () If the program does not want to directly call the result () method to block the thread, it can use the Future 's add_done_callback () method to add a callback function, which takes the form of fn (future). When the thread task is completed, the program will automatically trigger this callback function and pass the corresponding Future object as a parameter to this callback function.

from concurrent.futures import ThreadPoolExecutor
import threading,time

def test(value1, value2=None):
    print("%s threading is printed %s, %s"%(threading.current_thread().name, value1, value2))
    time.sleep(2)
    return 'finished'

def test_result(future):
    future.add_done_callback(test_result)

if __name__ == "__main__":
    threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
    for i in range(0,10):
        future = threadPool.submit(test, i,i+1)
        print(future.result())

    threadPool.shutdown(wait=True)
    print('main finished')
A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

Additionally, since the thread pool implements the Context Manager Protocol, programs can use the with statement to manage the thread pool, which eliminates the need to manually close it.

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

1.3.3 Using the map method of ThreadPoolExecutor

Exectuor also provides a map (func, *iterables, timeout= None, chunksize= 1) method, whose function is similar to that of the built-in function map (); the difference is that the map () method of the thread pool will start a thread for each element in iterables to execute the func function concurrently. This approach is equivalent to starting len (iterables) threads and collecting the execution results of each thread.

Below is an example demonstrating how to use the ThreadPoolExecutor map method to process multiple tasks in a queue:

import concurrent.futures

# Simulated task function
def task_function(task):
    print(f"Processing task: {task}")
    return task.upper()

# Create a thread pool
with concurrent.futures.ThreadPoolExecutor() as executor:

    # Create a task queue
    task_queue = ["Task 1", "Task 2", "Task 3", "Task 4", "Task 5"]

    # Use the map method to process the task queue
    results = executor.map(task_function, task_queue)

    # Get the execution results of the tasks
    for result in results:
        print(f"Task result: {result}")
A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

In the example above, we defined a simulated task function task_function that takes a task as a parameter and prints the task's information. In this function, we convert the task to uppercase and return it.

Then, we use ThreadPoolExecutor to create a thread pool. After that, we create a task queue task_queue which contains multiple tasks.

Then, we use the executor.map () method to pass the task function and the task queue as parameters to the map method. The map method will automatically assign tasks to threads in the thread pool for concurrent execution, and return an iterator of results results.

Finally, we use a for loop to iterate over the results iterator, retrieve the execution result of each task, and print them out.

By using the map method, multiple tasks in the queue can be handled more concisely, and the execution results of the tasks can be obtained conveniently. Please note that the map method will return results in the order of the tasks in the task queue, even if the execution order of the tasks may vary.

2. Performance Evaluation of Multithreading

As introduced earlier, the GIL is a lock introduced by the CPython interpreter, which prevents true parallel execution at the interpreter level. Before executing any thread, the interpreter must wait for the currently running thread to release the GIL. In fact, the interpreter enforces that any thread intending to run must acquire the GIL before accessing any of the interpreter's resources, such as stacks or Python objects. This is precisely the purpose of the GIL — to prevent concurrent access to Python objects from different threads. In this way, the GIL can protect the interpreter's memory and ensure the proper functioning of garbage collection.But in reality, this prevents programmers from improving program performance by executing multiple threads in parallel. If we remove the GIL of CPython, multithreading can truly run in parallel. The GIL does not actually affect threads running in parallel on multiple processors; it merely restricts an interpreter to running only one thread at a time.

Here, we use VizTracer to perform performance evaluation on multi-threading.

First, proceed with the installation: pip install viztracer

Here, we use the code of the LIFO queue for testing:

viztracer Multi_Thread.py

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

Follow the prompts and enter the command: vizviewer E:\Python并行计算学习笔记\Demo\result.json

Open the analysis result page, and you can view the function call status and running time of the two threads:

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

Adjust the cursor, and we can see more details:

A Quick Guide to Python Parallel Computing: 07 Python Multithreaded Programming – Usage of Thread Pools and Performance Evaluation of Multithreading

For detailed usage instructions, please visit: https://viztracer.readthedocs.io/en/latest/index.html

Or watch the video: https://www.bilibili.com/video/BV1d34y1C78W/