Application skills: Four types of multi-threaded applications in Python programming, how many do you know?

In Python, multithreading is a way to achieve concurrency. Multithreading allows a program to perform multiple tasks at the same time, thereby improving the efficiency and execution speed of the program.

This article will introduce all the ways of multithreading in Python, including using the threading module, using the concurrent.futures module, using the multiprocessing module, and using the asyncio module.

1. Use threading module

The threading module in Python provides basic support for multi-threaded programming. Use this module to create and manage threads to achieve concurrent execution. The following is a sample code that uses the threading module to implement multi-threading:

import threading
def worker():
    print('Worker thread started')
    # do some work here
    print('Worker thread finished')
if __name__ == '__main__':
    print('Main thread started')
    # create a new thread
    t = threading.Thread(target=worker)
    # start the new thread
    t.start()
    print('Main thread finished')

In the above code, we first define a worker function, which will be executed in a new thread.

Then, a new thread t is created in the main thread and the worker function is targeted by this thread.

Finally, start the new thread by calling the start method. Running the above code, the output results are as follows:

Main thread started
Worker thread started
Main thread finished
Worker thread finished

As can be seen from the above output, the program first executes the code in the main thread, then creates a new thread and executes the worker function in the new thread.

The main thread and the new thread are executed in parallel, so the execution speed of the program is improved.

2. Use concurrent.futures module

The concurrent.futures module is a new module in Python 3, which provides implementations of thread pools and process pools. Parallel execution can be more easily achieved using this module.

The following is sample code for implementing multi-threading using the concurrent.futures module:

import concurrent.futures
def worker():
    print('Worker thread started')
    # do some work here
    print('Worker thread finished')
if __name__ == '__main__':
    print('Main thread started')
    # create a thread pool
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        #submit worker function to the pool
        future = executor.submit(worker)
        print('Main thread finished')

In the above code, we first define a worker function, which will be executed in a new thread.

Then, a thread pool executor is created in the main thread and the maximum number of threads is set to 2. Next, submit the worker function to the thread pool by calling the submit method.

Finally, we output a message indicating that the main thread has completed execution. Running the above code, the output results are as follows:

Main thread started
Main thread finished
Worker thread started
Worker thread finished

As can be seen from the above output, the program first executes the code in the main thread, and then executes the worker function through the thread pool. The thread pool automatically manages the creation and destruction of threads, making the program more efficient.

3. Use multiprocessing module

The multiprocessing module in Python provides support for multi-process programming. Use this module to execute tasks in different processes to achieve concurrent execution.

The following is sample code for implementing multi-threading using the multiprocessing module:

import multiprocessing
def worker():
    print('Worker process started')
    # do some work here
    print('Worker process finished')
if __name__ == '__main__':
    print('Main process started')
    # create a new process
    p = multiprocessing.Process(target=worker)
    # start the new process
    p.start()
    print('Main process finished')

In the above code, we first define a worker function, which will be executed in a new process. Then, a new process p is created in the main process and the worker function is targeted by the process.

Finally, start the new process by calling the start method. Running the above code, the output results are as follows:

Main process started
Main process finished
Worker process started
Worker process finished

As can be seen from the above output, the program first executes the code in the main process, then creates a new process, and executes the worker function in the new process.

The main process and the new process are executed in parallel, so the execution speed of the program is improved.

4. Using asyncio module

The asyncio module in Python provides support for asynchronous programming. Use this module to implement coroutines to achieve concurrent execution in a single thread.

The following is sample code for implementing multithreading using the asyncio module:

import asyncio
async def worker():
    print('Worker task started')
    # do some work here
    print('Worker task finished')
if __name__ == '__main__':
    print('Main task started')
    # create a new event loop
    loop = asyncio.get_event_loop()
    # run the worker coroutine
    loop.run_until_complete(worker())
    # close the event loop
    loop.close()
    print('Main task finished')

In the above code, we first define an asynchronous function worker, which will be executed in a coroutine.

Then, a new event loop loop is created in the main task and the worker coroutine is run by calling the run_until_complete method.

Finally, we close the event loop. Running the above code, the output is as follows:

Main task started
Worker task started
Worker task finished
Main task finished

As can be seen from the above output, the program first executes the code in the main task, and then executes the worker coroutine through the event loop.

Coroutines are executed in a single thread, so the execution speed of the program is improved.

5. Summary

This article introduces all the ways to multithread in Python, including using the threading module, using the concurrent.futures module, using the multiprocessing module, and using the asyncio module.

Different methods are suitable for different scenarios, and you can choose the most appropriate method according to your needs.

Multi-threaded programming can improve the efficiency and execution speed of the program, but you need to pay attention to thread safety and the use of locks.

Finally:

Python learning materials

If you want to learn Python to help you automate your office, or are preparing to learn Python or are currently learning it, you should be able to use the following and get it if you need it.

① Python learning roadmap for all directions, knowing what to learn in each direction
② More than 100 Python course videos, covering essential basics, crawlers and data analysis
③ More than 100 Python practical cases, learning is no longer just theory
④ Huawei’s exclusive Python comic tutorial, you can also learn it on your mobile phone
⑤Real Python interview questions from Internet companies over the years, very convenient for review

There are ways to get it at the end of the article

1. Learning routes in all directions of Python

The Python all-direction route is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

2. Python course video

When we watch videos and learn, we can’t just move our eyes and brain but not our hands. The more scientific learning method is to use them after understanding. At this time, hands-on projects are very suitable.

3. Python practical cases

Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.

Four Python Comics Tutorial

Use easy-to-understand comics to teach you to learn Python, making it easier for you to remember and not boring.

5. Internet company interview questions

We must learn Python to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.


This complete version of Python learning materials has been uploaded to CSDN. If friends need it, you can also scan the official QR code of csdn below or click on the WeChat card at the bottom of the homepage and article to get the method. [Guaranteed 100% free]

syntaxbug.com © 2021 All Rights Reserved.