Home > Article > Backend Development > Python multi-threading and multi-process: practical case analysis, master the application skills of concurrent programming
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
concurrencyprogramming 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
.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()
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.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()
Through the above two practical cases, we have seen the powerful power of multi-threading and multi-process in concurrent programming. In actual
developmentThe 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!