Labs / Python OOP

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

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

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

Abstract:

In Python, "parallelism" and "concurrency" are commonly used to describe the execution modes of programs, while "thread" and "process" are two common approaches to implementing parallelism and concurrency. A thread is the smallest execution unit within a process, and multiple threads can share the same process's Memory Space. A process is an instance of a program that is being executed on a computer, and each process has its own independent Memory Space and resources.

FreakStudio's Blog

Previous Recommendations:

You who are learning embedded systems still don't know Object Oriented? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the 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 Tutorial Across the Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 03 Python Implementation of Classes and Objects - Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Course 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 Labels

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 on 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 Across the 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 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 Tutorial on the Whole Network: 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 – Automatically Generate File Comments and Function Comments with PyCharm

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 Online: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

The most beginner-friendly Object Oriented programming tutorial on the entire 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 Most Beginner-Friendly Object Oriented Programming Tutorial 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 Whole Network: 33 Built-in Data Types in Python – The Relationship Between Object and Type

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 Tutorial 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

More exciting content can be found here:

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

A Complete Guide to Debugging Principles of CM3 Microcontrollers

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

The "Secret Martial Arts Manual" for Computer Science Competitions

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

Deploy SenseCraft Model to Grove Vision AI V2 Image Processing Module

Document and Code Access:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19

This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications, taking the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples. Meanwhile, it uses the Sourcetrail code software to visualize the code for reading, so as to facilitate readers' understanding.

The link to obtain the relevant sample code is as follows:

https://github.com/leezisheng/Python-OOP-Demo

Body Text

Basic Concepts of Parallelism and Concurrency

In the above example simulating the sensor-host interaction, we can observe that our program always executes sequentially. For instance, when we use the serial port assistant to cooperate with the host MasterClass class: the host class sends commands, and we input analog data via the serial port assistant to send to the host side, yet there has been no involvement of the interaction between the MasterClass host class and the SensorClass sensor class — that is, the instance of SensorClass receiving the command sent by the instance of MasterClass, parsing it, and then executing the specified operation.This is because our previous code always adopted a single-threaded sequential execution approach. Meanwhile, in the implementation of the ReadSerial serial port reading method (the dev.readline () method) in SerialClass, the parent class of both SensorClass and MasterClass, a blocking method was used, meaning that when the sensor class is waiting for commands or the master class is waiting for data, it has to wait until data is received before proceeding to the next operation.

If the MasterClass host class and SensorClass sensor class are required to work alternately or simultaneously, it is necessary to use multithreading/multiprocessing in Python to implement parallel/concurrent multitasking.

Concurrency means that an application can execute different tasks alternately, which requires a single processor to switch between different tasks multiple times per second. Parallelism, on the other hand, refers to multiple processors or multi-core processors processing multiple different tasks simultaneously. The difference between the two is illustrated in the following figure:

1 2 1.1 2.1

Multi-task switching or processing can be accomplished either by multiple processes or by multiple threads within a single process.

A process is the smallest unit for resource allocation. A program has at least one process, and each process has its own independent address space, memory, data stack, etc. , so processes consume a large amount of resources. Since the resources of processes are independent of each other, inter-process communication is inconvenient, and inter-process communication (IPC) must be used.

A thread is the smallest unit of program execution, and a process has at least one thread. Threads share the data in a process and use the same address space, so creating a thread is fast and incurs less overhead than creating a process. Threads under the same process share data such as global variables and Static Variables, making inter-thread communication very convenient, but this also brings about issues of data synchronization and mutual exclusion; how to properly handle synchronization and mutual exclusion is the key difficulty in writing multi-threaded programs.

The relationship between the two is shown in the following figure:

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

Before explaining multithreading and multiprocessing, let's first review some of the classes and their interrelationships that we have already implemented:

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

Among them, the DateProcess data processing class has not yet established a connection with the host class, which we will implement in the subsequent code.

Multithreading

The operating mechanism of multithreading is similar to starting and running multiple different programs simultaneously. Unlike processes, each independent process has an entry point for the program, a clear execution sequence, and an exit point. However, threads cannot execute independently; they must rely on an application to provide execution control.

Each thread has its own set of CPU registers, which is called the thread's context. The context reflects the state of the CPU registers when the thread was last executed. In the thread's context, the instruction pointer and stack pointer registers are particularly critical. Threads always run within the context of a process, and these addresses are used to identify memory locations in the address space of the process that owns the thread. In this way, threads can work together to achieve more efficient task processing.

Before using multithreading, we need to import the threading module in Python:

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

Creation and Execution of Multithreading

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

There are two ways to use threads in Python: function or wrapping thread objects with a class:

The syntax is as follows:

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

Here, we implement multi-threaded operation for the SensorClass sensor class. First, make it inherit from the Thread class, call the initialization method of Thread in the initialization method of SensorClass, and create a new run () method at the same time. In the run () method, first complete the initialization and startup of the sensor, then create a while loop. Each loop will generate a new data value (simulating the collected sensor data), and execute corresponding operations according to the received commands. The sample code is as follows:

class SensorClass(SerialClass,Thread):
    '''
        Sensor class, inherits from SerialClass\Thread
    '''

    ... ...

    # Class initialization
    def __init__(self,port:str = "COM11",id:int = 0,state:int = WORK_MODE["RESPOND_MODE"]):
        try:
            # Check whether the input port number is of type str
            if type(port) is not str:
                raise TypeError("InvalidPortError:",port)
            # Check whether the ID is between 0 and 99
            if id < 0 or id > 99:
                # After an exception is raised, the code after it will not be executed
                # This exception is raised when the type of an argument passed to a function or method is incorrect or the value of the argument is invalid.
                raise InvalidIDError("InvalidIDError:",id)

            # Call the parent class initialization method; the super() function connects the parent class and the child class
            super().__init__(port)
            self.sensorvalue = 0
            self.sensorid    = id
            self.sensorstate = state
            print("Sensor Init")
            logging.info("Sensor Init")
            # Thread initialization method
            Thread.__init__(self)
        except TypeError:
            # When an exception occurs, output the following statement to remind the user to re-enter the port number
            print("Input error com, Please try new com number")
        except InvalidIDError as e:
            # When an exception occurs, output the following statement to remind the user to re-enter the ID
            print("Input error ID, Please try id : 0~99")
            print(e.args)

    ... ...

    # Method that represents thread activity in multithreading
    # All code in the run method (or code called from within this method) runs in a separate thread.
    def run(self):
        # Declare global variable, mutex lock
        global lock

        # Initialize counter variable
        data_count = 0
        # Initialize the sensor
        self.InitSensor()
        # Start the sensor
        self.StartSensor()

        while True:
            # Generate data
            data_count  = data_count + 1
            # Original signal
            signal      = math.sin(data_count) * 10
            # Simulated noise
            noise       = random.uniform(0, 5)
            # Final data
            data        = int(signal + noise)

            # Acquire the mutex
            lock.acquire()

            # Receive command
            cmd = self.RecvMasterCMD()

            # Perform the corresponding operation according to the command
            if cmd == SensorClass.STOP_CMD:
                # If a stop command is received, stop the sensor
                self.StopSensor()
                # Output prompt message
                print("Sensor stop work !!!")
                return
            elif cmd == SensorClass.SENDID_CMD:
                # If a send-ID command is received, send the sensor ID number
                self.SendSensorID()
            elif cmd == SensorClass.SENDVALUE_CMD:
                # If a send-data command is received, send the data
                self.SendSensorValue(data)
            elif cmd == SensorClass.NONE_CMD:
                # If no command is received
                print("Not Recv cmd!!!")

            # Release the mutex
            lock.release()
            # Delay 0.5 s
            time.sleep(0.5)

At the same time, we need to modify the attributes in SerialClass, the parent class of SensorClass, so that it will not keep blocking during the data receiving process:

# Set the timeout
self.dev.timeout     = 0.5

In the main program, a mutex and a SensorClass object are created, and a new thread is started in the main thread to execute the run () method of the SensorClass object. In the main thread, the program continuously checks whether the new thread has exited the run () method; if not, it acquires the mutex, prints a message, then releases the mutex and delays for 0.5 seconds. After the new thread exits the run () method, the main thread outputs a debug message indicating that the multi-threaded operation has ended.

The sample code is as follows:

if __name__ == "__main__":
    # Create a mutex lock
    lock = Lock()
    # Initialize the thread
    s_thread = SensorClass(port = "COM11",id = 0,state = SensorClass.WORK_MODE["RESPOND_MODE"])
    # Start the thread; the start method runs it concurrently
    s_thread.start()
    # The run() method is just an ordinary method of the class and still runs in the main thread
    # s_thread.run()
    # The join method ensures the thread child thread finishes before the next thread can run
    # timeout is the timeout period; the thread is ended once it reaches the timeout
    # s_thread.join(timeout=5)

    # Check whether the s_thread thread has already exited the run method
    while s_thread.is_alive():
        # Acquire the mutex
        lock.acquire()
        # Print information
        print("Multi threaded work,This is the main thread for creating and running")
        # Release the mutex
        lock.release()
        # Delay 0.5 s
        time.sleep(0.5)

    # Multithreading finished, output debug information
    print("End of multi-threaded running")

Next, let's take a look at the operation result:

V2.6

As can be seen, every program has a thread called the main thread, and the code executed from the very beginning runs within this thread. s_thread is a child thread created in the main thread, and a new thread will not start running until we call the thread's start () method. We can also use the join method to ensure that the next thread can only be executed after the child thread thread finishes executing.

Thread Synchronization

In a multi-threaded environment, unpredictable results may occur when multiple threads modify a certain data concurrently. Consider a scenario where there is a list with all elements initialized to 0: the thread named "set" is responsible for traversing the list from the end to the beginning and modifying each element to 1, while the thread named "print" is responsible for traversing the list from the start to the end and printing its contents.During the execution of these two threads, if the "set" thread is interrupted by the "print" thread while modifying the list, the printed result may show that some elements in the list are 0 while others are 1, which is the so-called data inconsistency problem.

To address this issue, we introduce the concept of locks. A lock has two states: locked and unlocked. When a thread, such as "set", needs to access shared data, it must first attempt to acquire the lock. If the lock has already been acquired by another thread, such as "print", the "set" thread will be blocked until the "print" thread releases the lock. This ensures that only one thread can access the shared data at any time. With this handling, when the list is printed, all elements will either be 0 or all be 1, thus avoiding the awkward situation of data inconsistency.

Python's Thread object provides two locking mechanisms, Lock and Rlock, to implement thread synchronization. Both of these objects offer the acquire and release methods. For data or shared resources that need to be accessed by only one thread at a time, such as the operation of printing information to the Console, we can place the relevant code between the acquire and release methods. In this way, we can ensure that the consistency and integrity of data are protected in a multi-threaded environment.

In the sample code, we create a mutex and pass it to the thread, ensuring that both threads can safely call the Printf function to output relevant information.

# Acquire the mutex
lock.acquire()
# Print information
print("Multi threaded work,This is the main thread for creating and running")
# Release the mutex
lock.release()

In addition to mutexes and recursive locks, threads also have other methods applied for thread synchronization, such as semaphores, events, barriers, etc. , which will not be elaborated in detail here.

Actually, to effectively manage memory, perform garbage collection, and invoke machine code in libraries, Python has a mechanism called the Global Interpreter Lock (GIL). It cannot be disabled, which means that multithreading in Python is not true multithreading: while the Python interpreter can start multiple threads, only one of them is actually running within the interpreter at any given time.The GIL issue exists in most Python implementations that people use (such as CPython), and this problem has been resolved in some non-standard implementations, such as IronPython and Jython.

thread1 thread2 thread3 1 2 3

Inter-thread Communication

In addition to using thread synchronization methods such as mutex locks and recursion locks to ensure that shared memory will not be accessed by two threads at the same time, we can also use the Queue module to ensure that data requiring interaction between two threads is accessed safely. The Queue module provides synchronous, thread-safe queue classes, including the FIFO (First-In-First-Out) queue Queue, the LIFO (Last-In-First-Out) queue LifoQueue, and the priority queue PriorityQueue. All these queues implement lock primitives and can be directly used in multi-thread scenarios, and queues can be used to achieve synchronization between threads.

Thread Pool

The cost for a system to start a new thread is relatively high, as it requires interaction with the operating system. In this scenario, adopting a thread pool is an effective way to improve performance, and it should be given priority for use especially when a program needs to create a large number of threads with a short lifecycle. A thread pool pre-creates a large number of idle threads when the system starts, and the program only needs to submit a function to the thread pool, which will then start an idle thread to execute this function. After the function finishes executing, the thread will not terminate; instead, it returns to the thread pool to remain idle and wait for the execution of the next function.

Furthermore, thread pools also help to precisely control the number of concurrent threads in a system. If there are an excessive number of concurrent threads in the system, it may lead to a significant drop in system performance, and even cause the Python interpreter to crash. By setting the maximum thread count parameter, a thread pool can effectively prevent the number of concurrent threads from exceeding the system's tolerance, thereby ensuring the stable operation of the system.

Starting from Python 3.2, the standard library provides us with the concurrent.futures module, which offers two classes: ThreadPoolExecutor (thread pool) and ProcessPoolExecutor (process pool).

Compared with modules such as threading, this module returns a future object via submit, which is a promise-like object. Such objects are used for "call-and-answer" type interactions, where the processing can take place in another thread, and we can query it for the result at some point in the future.

Through it, you can obtain the execution status of a specific thread or the execution status and return value of a specific task.

① The main thread can obtain the status and return value of a specific thread (or task).

② When a thread finishes execution, the main thread can be notified immediately.

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

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

multi-process

Compared with multithreading, multiprocessing has independent Memory Space and avoids the impact of the Global Interpreter Lock (GIL), so it is more suitable for CPU-intensive tasks. The multiprocessing module is implemented by spawning new operating system processes. On Windows machines, this operation is relatively expensive; on Linux, processes are implemented in the kernel in the same way as threads, so their overhead is limited by the Python interpreter running in each process.

Multiprocessing in Python is implemented via the `multiprocessing` package, which is similar to `threading.Thread` in multithreading; you can use the `multiprocessing.Process` object to create a process object. The methods of this process object are similar to those of a thread object, including `start ()`, `run ()`, `join ()` and other methods. The implementation of multiprocessing in Python, similar to the aforementioned multithreading implementation, can also be done via class inheritance.

5 1 2 3 4 9
The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 59 Python Parallelism and Concurrency - Parallelism & Concurrency and Threads & Processes
347622978