Python 进程的创建 - multiprocessing

1. Introduction

Python multiprocessing is a module that allows multiple processes to run parallelly and independently of each other. It provides a way to take full advantage of modern multi-core processors. In this article, we will explore how to create and manage processes using the multiprocessing module in Python.

2. Creating Processes

To create a new process, we need to import the "multiprocessing" module and use the "Process" class. Here's an example:

import multiprocessing

def worker():

print("Worker process")

if __name__ == "__main__":

p = multiprocessing.Process(target=worker)

p.start()

p.join()

print("Main process")

In the above code, we define a simple function called "worker" which will be executed by the child process. Inside the main block, we create a new process using the "Process" class and pass the "worker" function as the target. Then, we start the process using the "start" method and wait for it to finish using the "join" method. Finally, we print "Main process" to indicate that the main process is still running after the child process completes.

Here, we use the condition if __name__ == "__main__": to ensure that the child processes are created only when the script is run as the main script and not as an imported module.

2.1 Passing Arguments to Processes

We can also pass arguments to the target function of the process. To do this, we can pass the arguments as a tuple using the "args" parameter of the "Process" class. Here's an example:

import multiprocessing

def worker(name):

print(f"Worker process: Hello, {name}!")

if __name__ == "__main__":

p = multiprocessing.Process(target=worker, args=("Alice",))

p.start()

p.join()

print("Main process")

In the above code, we pass the name "Alice" as an argument to the "worker" function by including it in a tuple and passing it to the "args" parameter of the "Process" class.

3. Synchronization between Processes

When working with multiple processes, we often need to synchronize their operations. The multiprocessing module provides several synchronization primitives, such as locks, semaphores, and events, to facilitate this.

3.1 Locks

A lock is a synchronization primitive that allows only one process to access a shared resource at a time. It provides a way to prevent multiple processes from interfering with each other. Here's an example:

import multiprocessing

def worker(lock):

lock.acquire()

try:

print("Worker process: Acquired lock")

# Perform operations on the shared resource

print("Worker process: Released lock")

finally:

lock.release()

if __name__ == "__main__":

lock = multiprocessing.Lock()

p1 = multiprocessing.Process(target=worker, args=(lock,))

p2 = multiprocessing.Process(target=worker, args=(lock,))

p1.start()

p2.start()

p1.join()

p2.join()

print("Main process")

In the above code, we create a lock object using the "Lock" class and pass it as an argument to the "worker" function. Inside the "worker" function, we acquire the lock using the "acquire" method before performing operations on the shared resource and release it using the "release" method afterwards.

4. Conclusion

The multiprocessing module in Python provides a convenient way to create and manage multiple processes. It allows us to take advantage of the full power of modern processors and perform tasks in parallel. By using synchronization primitives such as locks, we can ensure that the processes work together without interference. Understanding how to create and synchronize processes using the multiprocessing module is crucial for developing efficient and scalable Python applications.

免责声明:本文来自互联网,本站所有信息(包括但不限于文字、视频、音频、数据及图表),不保证该信息的准确性、真实性、完整性、有效性、及时性、原创性等,版权归属于原作者,如无意侵犯媒体或个人知识产权,请来电或致函告之,本站将在第一时间处理。猿码集站发布此文目的在于促进信息交流,此文观点与本站立场无关,不承担任何责任。

后端开发标签