在Python中,設計并發編程模式需要考慮多種因素,如線程安全、進程間通信、任務調度等。以下是一些常見的并發編程設計模式:
多線程模式是最常見的并發編程模式之一,適用于I/O密集型任務。
import threading
def worker():
print(f"Thread {threading.current_thread().name} is working")
threads = []
for i in range(5):
t = threading.Thread(target=worker, name=f"Thread-{i}")
threads.append(t)
t.start()
for t in threads:
t.join()
多進程模式適用于CPU密集型任務,因為Python的全局解釋器鎖(GIL)限制了多線程的并行計算能力。
import multiprocessing
def worker():
print(f"Process {multiprocessing.current_process().name} is working")
processes = []
for i in range(5):
p = multiprocessing.Process(target=worker, name=f"Process-{i}")
processes.append(p)
p.start()
for p in processes:
p.join()
異步編程模式適用于I/O密集型任務,可以提高程序的并發性能。Python的asyncio
庫是實現異步編程的常用工具。
import asyncio
async def worker():
print(f"Task {asyncio.current_task().name} is working")
await asyncio.sleep(1)
async def main():
tasks = []
for i in range(5):
task = asyncio.create_task(worker(), name=f"Task-{i}")
tasks.append(task)
await asyncio.gather(*tasks)
asyncio.run(main())
線程池模式可以有效地管理線程資源,避免頻繁創建和銷毀線程的開銷。Python的concurrent.futures.ThreadPoolExecutor
提供了線程池的實現。
from concurrent.futures import ThreadPoolExecutor
def worker():
print(f"Thread {threading.current_thread().name} is working")
with ThreadPoolExecutor(max_workers=5) as executor:
for i in range(5):
executor.submit(worker, f"Task-{i}")
進程池模式可以有效地管理進程資源,避免頻繁創建和銷毀進程的開銷。Python的concurrent.futures.ProcessPoolExecutor
提供了進程池的實現。
from concurrent.futures import ProcessPoolExecutor
def worker():
print(f"Process {multiprocessing.current_process().name} is working")
with ProcessPoolExecutor(max_workers=5) as executor:
for i in range(5):
executor.submit(worker, f"Task-{i}")
任務隊列模式適用于生產者-消費者模型,可以有效地解耦生產者和消費者。Python的queue
模塊提供了任務隊列的實現。
import threading
import queue
def worker(q):
while True:
item = q.get()
if item is None:
break
print(f"Worker is processing {item}")
q.task_done()
q = queue.Queue()
for i in range(5):
q.put(i)
threads = []
for i in range(5):
t = threading.Thread(target=worker, args=(q,), name=f"Worker-{i}")
threads.append(t)
t.start()
q.join()
for _ in threads:
q.put(None)
for t in threads:
t.join()
事件驅動模式適用于需要響應特定事件的場景。Python的threading
模塊提供了事件對象的支持。
import threading
def worker(event):
print(f"Worker is waiting for event")
event.wait()
print("Worker has received event")
event = threading.Event()
t = threading.Thread(target=worker, args=(event,), name="Worker")
t.start()
print("Main thread is setting event")
event.set()
t.join()
管道通信模式適用于進程間通信,Python的multiprocessing
模塊提供了管道的實現。
import multiprocessing
def sender(conn):
conn.send(["Hello", "from", "sender"])
conn.close()
def receiver(conn):
msg = conn.recv()
print("Received:", msg)
conn.close()
parent_conn, child_conn = multiprocessing.Pipe()
t1 = multiprocessing.Process(target=sender, args=(child_conn,), name="Sender")
t2 = multiprocessing.Process(target=receiver, args=(parent_conn,), name="Receiver")
t1.start()
t2.start()
t1.join()
t2.join()
Python提供了多種并發編程模式,選擇哪種模式取決于具體的應用場景和需求。多線程適用于I/O密集型任務,多進程適用于CPU密集型任務,異步編程適用于I/O密集型任務,線程池和進程池可以有效地管理資源,任務隊列適用于生產者-消費者模型,事件驅動模式適用于響應特定事件的場景,管道通信適用于進程間通信。