Network Engineers Learn Python-33-A Brief Introduction to Multi-Threading Technology

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 threadingmodule. This module provides Threadclasses 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 workerfunction named and passes it as a target to Threadthe class. Then, call startthe method to start the thread. This thread will execute workerthe 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 threadingprovide Lockclasses 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 counterthe 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 threadingprovide Conditionclasses 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 itemsthe 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.futuresprovide ThreadPoolExecutorclasses 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 submitthe 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.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132623355