Python并发编程之多线程与多进程
2026/5/28 23:59:20 网站建设 项目流程

Python并发编程:多线程与多进程

一、并发与并行

并发:多个任务在重叠时间段内交替执行。
并行:多个任务在同一时刻真正同时运行。

Python中多线程受GIL限制适合IO密集型,多进程适合CPU密集型。


二、threading基础

import threading, time

def download(url):
print(f"下载 {url}")
time.sleep(2)
print(f"{url} 完成")

threads = [threading.Thread(target=download, args=(f"url-{i}",)) for i in range(5)]
for t in threads: t.start()
for t in threads: t.join()


三、线程同步

import threading

class Account:
def __init__(self, balance):
self.balance = balance
self.lock = threading.Lock()

def withdraw(self, amount):
with self.lock:
if self.balance >= amount:
self.balance -= amount
return True
return False

其他同步工具:
- RLock:可重入锁
- Semaphore:信号量,限制并发数
- Event:事件通知
- Condition:条件变量


四、线程池

from concurrent.futures import ThreadPoolExecutor, as_completed

def fetch(url):
return requests.get(url).status_code

urls = ['http://example.com'] * 10
with ThreadPoolExecutor(max_workers=5) as pool:
results = list(pool.map(fetch, urls))


五、multiprocessing

from multiprocessing import Process, Queue, Pool

def cpu_work(n):
return sum(i*i for i in range(n))

with Pool(processes=4) as pool:
results = pool.map(cpu_work, [1000000] * 8)

进程间通信使用 Queue、Pipe、SharedMemory。


六、进程池

from concurrent.futures import ProcessPoolExecutor

def is_prime(n):
if n < 2: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0: return False
return True

with ProcessPoolExecutor() as pool:
results = pool.map(is_prime, range(100000, 101000))


七、选择建议

任务类型 推荐方案 原因
IO密集型 多线程/asyncio GIL在IO时释放
CPU密集型 多进程 真正并行
混合型 多进程+线程池 各自处理对应任务

总结:IO密集型用线程,CPU密集型用进程,concurrent.futures 提供统一的高层接口。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询