Python multi-threading and multi-process: practical case analysis, master the application skills of concurrent programming

WBOY
Release: 2024-02-25 09:13:36
forward
451 people have browsed it

Python 多线程与多进程:实战案例剖析,掌握并发编程的应用技巧

1. python Multi-threading and multi-process overview

1. Multi-threading

MultipleThreads is a concurrent programming technology that allows a program to perform multiple tasks at the same time. In Python, you can use the threading module to create and manage threads. Threads share the program's memory space, so data can be shared easily. However, there are also some challenges in multi-threaded programming, such as thread safety issues and dead lock issues. 2. Multi-process Multiple processes is a

concurrency

programming technology that allows one program to perform multiple tasks at the same time. In Python, you can use the multiprocessing module to create and manage processes. The process is independent of the program's memory space, so there are no thread safety issues. However, multi-process programming also presents some challenges, such as inter-process communication and synchronization issues. 2. Practical Case 1: Using multi-threads to accelerate image processing

Requirements: There is a folder containing 1000 images, and each image needs to be processed, including resizing, sharpening, and adding watermarks.

Solution:

You can use multi-threading to speed up image processing. Specific steps are as follows:

First, save the list of image files in a list.

Then, use the threading module to create a thread pool

.
  1. Next, assign image processing tasks to threads in the thread pool.
  2. Finally, wait for all threads to complete their tasks and then save the processed image to the file.
  3. By using multi-threading, the image processing time can be greatly shortened.
  4. import threading
    import os
    from PIL import Image, ImageFilter
    
    def process_image(image_path):
    image = Image.open(image_path)
    image = image.resize((256, 256))
    image = image.filter(ImageFilter.SHARPEN)
    image.save(image_path)
    
    def main():
    # 获取图像文件列表
    image_files = os.listdir("images")
    
    # 创建线程池
    pool = ThreadPool(4)
    
    # 将图像处理任务分配给线程池
    for image_file in image_files:
    pool.submit(process_image, ("images/" + image_file, ))
    
    # 等待所有线程完成任务
    pool.close()
    pool.join()
    
    if __name__ == "__main__":
    main()
    Copy after login
  5. 3. Practical Case 2: Using multiple processes to accelerate scientific computing
Requirements:

A scientific calculation needs to be performed, involving a large number of floating point calculations.

Solution:

Multiple processes can be used to speed up scientific computing. Specific steps are as follows:

First, decompose the scientific computing task into multiple subtasks.

Then, use the multiprocessing module to create a process pool.

Next, assign subtasks to processes in the process pool.
  1. Finally, wait for all processes to complete their tasks, and then summarize the results of the subtasks.
  2. By using multiple processes, the time for scientific calculations can be greatly shortened.
  3. import multiprocessing
    import math
    
    def compute_pi(n):
    result = 0
    for i in range(n):
    result += 4 * (-1)**i / (2*i + 1)
    return result
    
    def main():
    # 分解科学计算任务
    n = 100000000
    num_processes = 4
    sub_tasks = [n // num_processes] * num_processes
    
    # 创建进程池
    pool = multiprocessing.Pool(num_processes)
    
    # 将子任务分配给进程池
    results = pool.map(compute_pi, sub_tasks)
    
    # 等待所有进程完成任务
    pool.close()
    pool.join()
    
    # 汇总子任务的结果
    pi = sum(results)
    
    print("π =", pi)
    
    if __name__ == "__main__":
    main()
    Copy after login
  4. IV.
  5. Summary

Through the above two practical cases, we have seen the powerful power of multi-threading and multi-process in concurrent programming. In actual

development

, we can choose appropriate concurrent programming technology according to specific needs.

The above is the detailed content of Python multi-threading and multi-process: practical case analysis, master the application skills of concurrent programming. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template