Python Multiprocessing Module: Process Creation, Synchronization, and Inter‑Process Communication
This article provides a comprehensive guide to Python's multiprocessing module, covering how to create and manage processes, use join for synchronization, configure daemon processes, and employ synchronization primitives such as Lock, Semaphore, Event, and Queue for safe inter‑process communication, with complete code examples.
The multiprocessing module in Python offers a high‑level interface for creating and controlling separate processes, enabling true parallel execution on multi‑core systems.
Creating a simple process :
<code># 创建一个进程
from multiprocessing import Process # 导入创建进程模块
def func():
print('我是一个进程')
if __name__ == '__main__':
p = Process(target=func) # 创建进程对象
p.start() # 启动进程
# 结果
# 我是一个进程
</code>Processes can receive arguments, and join() can be used to block the main process until a child finishes, turning asynchronous code into synchronous execution:
<code>from multiprocessing import Process
def func(arg):
print('我是一个%s' % arg)
if __name__ == '__main__':
p = Process(target=func, args=('name',))
p.start()
p.join() # 阻塞等待子进程结束
print('我是第一顺序执行者')
# 结果
# 我是一个name
# 我是第一顺序执行者
</code>On Windows, process creation must be guarded by if __name__ == '__main__': to avoid recursive spawning.
Daemon processes terminate automatically when the main program exits, unless join() is called to wait for them:
<code>import time
from multiprocessing import Process
def func():
while True:
time.sleep(1)
print('过了一秒')
if __name__ == '__main__':
p = Process(target=func)
p.daemon = True # 必须在 start 之前设置
p.start()
for i in range(5):
time.sleep(0.1)
print('*' * i)
</code>Synchronization primitives such as Lock , Semaphore , and Event ensure safe access to shared resources.
Lock example :
<code>from multiprocessing import Lock, Process
def func(k, lock):
lock.acquire()
print(k)
lock.release()
if __name__ == '__main__':
lock = Lock()
for i in range(10):
p = Process(target=func, args=(i, lock))
p.start()
</code>Semaphore example (limited concurrent access) :
<code>import time, random
from multiprocessing import Process, Semaphore
def toilet(i, sem):
sem.acquire()
print(f'{i}进厕所')
time.sleep(random.randint(1,5))
print(f'{i}出厕所')
sem.release()
if __name__ == '__main__':
sem = Semaphore(4) # 同时最多 4 个进程进入临界区
for i in range(10):
p = Process(target=toilet, args=(i, sem))
p.start()
</code>Event example (red‑green light simulation) :
<code>import time, random
from multiprocessing import Process, Event
def traffic_car(e):
while True:
if e.is_set():
time.sleep(3)
print('红灯亮')
e.clear()
else:
time.sleep(3)
print('绿灯亮')
e.set()
def car(i, e):
e.wait()
print(f'{i}车通过')
if __name__ == '__main__':
e = Event()
tra = Process(target=traffic_car, args=(e,))
tra.start()
for i in range(100):
if i % 6 == 0:
time.sleep(random.randint(1,3))
p = Process(target=car, args=(i, e))
p.start()
</code>Inter‑process communication can be achieved with Queue , which provides put() and get() methods. Queues can be used between parent‑child processes or among sibling processes.
<code># Simple queue usage
from multiprocessing import Queue
q = Queue()
q.put(1)
print(q.get()) # 输出 1
</code> <code># Parent‑child communication
from multiprocessing import Process, Queue
def q_put(q):
q.put('hello')
if __name__ == '__main__':
q = Queue()
p = Process(target=q_put, args=(q,))
p.start()
print(q.get()) # 输出 hello
</code>Overall, the multiprocessing module equips developers with the tools needed to launch multiple processes, synchronize their execution, protect shared data, and exchange information, making it essential for building robust concurrent Python applications.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.