溫馨提示×

Python并發(fā)編程如何設(shè)計(jì)模式

小樊
81
2024-11-02 18:21:42
欄目: 編程語言

在Python中,設(shè)計(jì)并發(fā)編程模式需要考慮多種因素,如線程安全、進(jìn)程間通信、任務(wù)調(diào)度等。以下是一些常見的并發(fā)編程設(shè)計(jì)模式:

1. 多線程模式

多線程模式是最常見的并發(fā)編程模式之一,適用于I/O密集型任務(wù)。

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()

2. 多進(jìn)程模式

多進(jìn)程模式適用于CPU密集型任務(wù),因?yàn)镻ython的全局解釋器鎖(GIL)限制了多線程的并行計(jì)算能力。

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()

3. 異步編程模式

異步編程模式適用于I/O密集型任務(wù),可以提高程序的并發(fā)性能。Python的asyncio庫是實(shí)現(xiàn)異步編程的常用工具。

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())

4. 線程池模式

線程池模式可以有效地管理線程資源,避免頻繁創(chuàng)建和銷毀線程的開銷。Python的concurrent.futures.ThreadPoolExecutor提供了線程池的實(shí)現(xiàn)。

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}")

5. 進(jìn)程池模式

進(jìn)程池模式可以有效地管理進(jìn)程資源,避免頻繁創(chuàng)建和銷毀進(jìn)程的開銷。Python的concurrent.futures.ProcessPoolExecutor提供了進(jìn)程池的實(shí)現(xiàn)。

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}")

6. 任務(wù)隊(duì)列模式

任務(wù)隊(duì)列模式適用于生產(chǎn)者-消費(fèi)者模型,可以有效地解耦生產(chǎn)者和消費(fèi)者。Python的queue模塊提供了任務(wù)隊(duì)列的實(shí)現(xiàn)。

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()

7. 事件驅(qū)動模式

事件驅(qū)動模式適用于需要響應(yīng)特定事件的場景。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()

8. 管道通信模式

管道通信模式適用于進(jìn)程間通信,Python的multiprocessing模塊提供了管道的實(shí)現(xiàn)。

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()

總結(jié)

Python提供了多種并發(fā)編程模式,選擇哪種模式取決于具體的應(yīng)用場景和需求。多線程適用于I/O密集型任務(wù),多進(jìn)程適用于CPU密集型任務(wù),異步編程適用于I/O密集型任務(wù),線程池和進(jìn)程池可以有效地管理資源,任務(wù)隊(duì)列適用于生產(chǎn)者-消費(fèi)者模型,事件驅(qū)動模式適用于響應(yīng)特定事件的場景,管道通信適用于進(jìn)程間通信。

0