Home >Backend Development >Python Tutorial >How to use the multiprocessing module for multi-process management in Python 2.x
How to use the multiprocessing module for multi-process management in Python 2.x
Introduction:
With the popularity of multi-core processors and the improvement of hardware performance, the use of multi-process parallel processing has become an improved procedure important means of efficiency. In Python 2.x, we can use the multiprocessing module to implement multi-process management. This article will introduce how to use the multiprocessing module for multi-process management.
from multiprocessing import Process def func(): # 子进程要执行的代码 print("This is a child process.") if __name__ == "__main__": # 创建子进程 p = Process(target=func) # 启动子进程 p.start() # 等待子进程结束 p.join() # 输出结果 print("This is the main process.")
In the above sample code, we first imported The Process class then defines a func function as the code to be executed by the child process. In the main function, we create a Process object p and specify the function to be executed as func through the target parameter. Then start the subprocess by calling the p.start() method, and then call the p.join() method to wait for the subprocess to end. Finally output the result.
from multiprocessing import Process def func(index): # 子进程要执行的代码 print("This is child process %d." % index) if __name__ == "__main__": # 创建多个子进程 processes = [] for i in range(5): p = Process(target=func, args=(i,)) processes.append(p) # 启动所有子进程 for p in processes: p.start() # 等待所有子进程结束 for p in processes: p.join() # 输出结果 print("This is the main process.")
In the above sample code, we use a loop to create 5 sub-processes, and the function func of each sub-process receives a The parameter index represents the number of the child process. When creating a child process, we pass the parameter index to the child process through the args parameter, so that each child process performs different tasks.
from multiprocessing import Process, Queue def producer(queue): # 生产者进程 for i in range(5): item = "item %d" % i queue.put(item) print("Produced", item) def consumer(queue): # 消费者进程 while True: item = queue.get() print("Consumed", item) if item == "item 4": break if __name__ == "__main__": # 创建Queue对象 queue = Queue() # 创建生产者进程和消费者进程 p1 = Process(target=producer, args=(queue,)) p2 = Process(target=consumer, args=(queue,)) # 启动子进程 p1.start() p2.start() # 等待子进程结束 p1.join() p2.join() # 输出结果 print("This is the main process.")
In the above sample code, we create a queue object through the Queue class for use between the producer process and the consumer Transfer data between processes. In the producer process, we use the put method to put data into the queue; in the consumer process, we use the get method to take data out of the queue. When the queue is empty, the consumer process will automatically block until there is data in the queue to be retrieved. In the sample code, the producer process puts 5 items into the queue, and then the consumer process takes the items from the queue and prints them. When the item taken out is "item 4", the consumer process ends.
Conclusion:
Using the multiprocessing module for multi-process management can effectively improve the execution efficiency of the program. Through the introduction of this article, readers can learn how to use the multiprocessing module to create sub-processes, create multiple sub-processes for parallel execution, and implement inter-process communication. Hope this article is helpful for multi-process programming in Python 2.x.
The above is the detailed content of How to use the multiprocessing module for multi-process management in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!