search
HomeCommon ProblemThe difference between python thread pool and multi-threading

The difference between python thread pool and multi-threading: 1. Threads travel under the process; 2. Threads travel under the process; 3. A process can contain multiple threads; 4. Data is difficult to share between different processes ;5. Processes consume more computer resources than threads.

The difference between python thread pool and multi-threading

1. Threads and multi-threads

Process: When a program is executed, it can be called a process, which includes the running program and the memory and system resources used by the program. A process is composed of multiple threads.

Thread: A thread is an execution stream in the program. Each thread has its own private register, and the code area It is shared. Different threads can execute the same function.

Multiple threads: Multi-threading means that a program contains multiple execution streams, that is, a program can run multiple different threads at the same time to perform different tasks, allowing a single program to create multiple parallel execution threads to complete their respective tasks.

The biggest benefit of multi-threading: Improve CPU utilization (especially suitable for I/O-intensive programs, the speed improvement is particularly obvious)

The difference between processes and threads:

Be a Simple metaphor: process = train, thread = carriage

Threads travel under the process (a simple carriage cannot run)

A process can contain multiple threads (a train can have multiple Carriage)

It is difficult to share data between different processes (it is difficult for passengers on one train to change to another train, such as station transfer)

It is very difficult to share data between different threads in the same process Easy to share (it is easy to change from carriage A to carriage B)

Processes consume more computer resources than threads (using multiple trains consumes more resources than multiple carriages)

Inter-process They will not affect each other. If one thread hangs up, the entire process will hang up (one train will not affect another train, but if the middle carriage of a train catches fire, it will affect all carriages)

The process can be expanded to multiple machines, and the process is suitable for up to multiple cores (different trains can run on multiple tracks, and the carriages of the same train cannot run on different tracks)

The memory address used by the process can be locked , that is, when a thread uses some shared memory, other threads must wait for it to end before they can use this piece of memory. (For example, the bathroom on the train) - "Mutex Lock"

The memory address used by the process can limit the usage (for example, a restaurant on the train, only a maximum number of people are allowed to enter, if it is full, you need to wait at the door, Wait for someone to come out before entering)-"Semaphore"

2. Multi-threading threading class

The threading class is the most commonly used multi-threading module. The method of creating a thread is as follows:

threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, 
daemon=None)

group: The default is None.

target: The name of the function to be executed, remember not to include the parentheses of the function.

name: The name of the thread, the default is 'Thread-N' Form.

args: The parameter tuple of the callable object passed in the parameter target.

kwargs: The keyword argument dictionary of the callable object passed in the parameter target.

daemon: daemon mode attribute, the default is None.

Important methods of thread objects:

start(): Start the thread. It will make the run() method in an independent is called in the thread.

run(): This method represents thread activity.

join(timeout=None): Let the current caller thread wait until the thread ends.

daemon: Indicates whether the thread is a daemon thread, True or False.

Create a multi-threaded instance:

import random
import threading
import time
def awesome_function(name):
wait_time = random.randint(1, 10)
print('current thread name is :{0} and wait {1} s'.format(name, 
wait_time))
time.sleep(wait_time)
print('thread {} finished.'.format(name))
if __name__ == '__main__':
for i in range(0, 3):
t = threading.Thread(target=awesome_function, args=(i,))
t.start()

You can first look at the running results I recorded:

The above example starts 3 threads, and 3 threads execute tasks concurrently. The thread that completes the task first (the one with the shortest time.sleep) outputs the result first.

3. A better-used thread pool class ThreadPoolExecutor

Starting a new thread is very expensive because it involves interaction with the operating system. In this case, using a thread pool can greatly improve program performance, especially when a large number of programs need to be created with a short lifetime. When using threads, you should consider using a thread pool.

The thread pool creates a large number of idle threads when the system starts. As long as the program submits a function to the thread pool, the thread pool will start an idle thread for execution. It. When the execution of the function ends, the thread will not die, but will return to the thread pool again and become idle, waiting for the next function. At the same time, using the thread pool can effectively control the number of concurrent threads in the system. When the system contains a large number of concurrent threads, the system performance will drop sharply and even cause the Python interpreter to crash. The maximum number of threads parameter of the thread pool can control the number of concurrent threads in the system not to exceed this number.

The thread pool currently in mainstream use is ThreadPoolExecutor in the concurrent.futures module:

import random
import time
from concurrent.futures import ThreadPoolExecutor
def awesome_function(name):
wait_time = random.randint(1, 10)
print('current thread name is :{0} and wait {1} s'.format(name, 
wait_time))
time.sleep(wait_time)
print('thread {} finished.'.format(name))
if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=3) as t:
for i in range(0, 3):
t.submit(awesome_function, i)

The running results are recorded as follows:

Create a thread pool object with a maximum capacity of 3 and submit it through submit The executed function is put into the thread pool. If a thread in the thread pool (thread 2) When the execution is completed, the idle thread (thread 3) is put into the pool, and so on, until all threads are completed, the program ends.

The above is the detailed content of The difference between python thread pool and multi-threading. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
deepseek web version official entrancedeepseek web version official entranceMar 12, 2025 pm 01:42 PM

The domestic AI dark horse DeepSeek has risen strongly, shocking the global AI industry! This Chinese artificial intelligence company, which has only been established for a year and a half, has won wide praise from global users for its free and open source mockups, DeepSeek-V3 and DeepSeek-R1. DeepSeek-R1 is now fully launched, with performance comparable to the official version of OpenAIo1! You can experience its powerful functions on the web page, APP and API interface. Download method: Supports iOS and Android systems, users can download it through the app store; the web version has also been officially opened! DeepSeek web version official entrance: ht

In-depth search deepseek official website entranceIn-depth search deepseek official website entranceMar 12, 2025 pm 01:33 PM

At the beginning of 2025, domestic AI "deepseek" made a stunning debut! This free and open source AI model has a performance comparable to the official version of OpenAI's o1, and has been fully launched on the web side, APP and API, supporting multi-terminal use of iOS, Android and web versions. In-depth search of deepseek official website and usage guide: official website address: https://www.deepseek.com/Using steps for web version: Click the link above to enter deepseek official website. Click the "Start Conversation" button on the homepage. For the first use, you need to log in with your mobile phone verification code. After logging in, you can enter the dialogue interface. deepseek is powerful, can write code, read file, and create code

How to solve the problem of busy servers for deepseekHow to solve the problem of busy servers for deepseekMar 12, 2025 pm 01:39 PM

DeepSeek: How to deal with the popular AI that is congested with servers? As a hot AI in 2025, DeepSeek is free and open source and has a performance comparable to the official version of OpenAIo1, which shows its popularity. However, high concurrency also brings the problem of server busyness. This article will analyze the reasons and provide coping strategies. DeepSeek web version entrance: https://www.deepseek.com/DeepSeek server busy reason: High concurrent access: DeepSeek's free and powerful features attract a large number of users to use at the same time, resulting in excessive server load. Cyber ​​Attack: It is reported that DeepSeek has an impact on the US financial industry.

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools