Python multithreading is a concurrent programming method that improves program performance and responsiveness by using multiple threads to perform multiple tasks at the same time. In this article, we will introduce multi-threaded programming in Python, including how to create threads, thread synchronization and thread pools, etc.
Create thread
To create a thread, you can use Python's built-in threading
module. This module provides Thread
classes to easily create and manage threads. Here's a simple example:
import threading
def worker():
print('Working...')
t = threading.Thread(target=worker)
t.start()
The above code creates a worker
function named and passes it as a target to Thread
the class. Then, call start
the method to start the thread. This thread will execute worker
the function and output Working...
the message.
Thread synchronization
In multi-threaded programming, thread synchronization is a very important concept. If multiple threads access shared resources simultaneously, data inconsistencies or race conditions may result. Python provides some thread synchronization mechanisms, such as locks and condition variables, to help solve these problems.
Lock
A lock is a thread synchronization mechanism that ensures that only one thread can access a shared resource. Modules in Python threading
provide Lock
classes that can be used to implement locks. Here is an example of using a lock:
import threading
counter = 0
lock = threading.Lock()
def worker():
global counter
with lock:
for i in range(100000):
counter += 1
threads = []
for i in range(10):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
for t in threads:
t.join()
print(counter)
The above code creates 10 threads and uses a lock to ensure that only one thread can access counter
the variable. Each thread increments the counter 100000 times. Finally, the value of the counter is output.
condition variable
A condition variable is a thread synchronization mechanism that allows a thread to wait until a specific condition is met. Modules in Python threading
provide Condition
classes that can be used to implement condition variables. Here is an example using condition variables:
import threading
items = []
condition = threading.Condition()
def consumer():
with condition:
while not items:
condition.wait()
items.pop(0)
def producer():
with condition:
items.append('item')
condition.notify()
threads = []
for i in range(10):
t = threading.Thread(target=consumer)
threads.append(t)
t.start()
for i in range(10):
t = threading.Thread(target=producer)
threads.append(t)
t.start()
for t in threads:
t.join()
The above code creates 10 consumer threads and 10 producer threads. Each producer thread adds a string to items
the list and each consumer thread removes the first element from the list. If the list is empty, the consumer thread will wait until an element is available. Condition variables are used to synchronize consumer and producer threads.
Thread Pool
Thread pool is a mechanism for managing and reusing threads, which can reduce the overhead of thread creation and destruction. Modules in Python concurrent.futures
provide ThreadPoolExecutor
classes to easily create and manage thread pools. Here is an example using a thread pool:
import concurrent.futures
def worker(index):
print(f'Working on task {index}...')
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for i in range(10):
executor.submit(worker, i)
The above code creates a thread pool that can run up to 5 threads at the same time. Then, use submit
the method to submit 10 tasks to the thread pool. The thread pool will automatically allocate and manage threads to run up to 5 tasks simultaneously.
Summarize
In this article, we introduce multi-threaded programming in Python, including how to create threads, thread synchronization and thread pools, etc. Multi-threaded programming can improve the performance and responsiveness of the program, but you need to pay attention to issues such as thread synchronization and resource competition. Python provides some thread synchronization mechanisms and thread pools to help us write concurrent programs more easily.