

在深入技术细节前,必须明确两个基础概念的区别:
通俗比喻:
1.并发:单个服务员同时照看多桌客人,轮流服务
2.并行:多个服务员各自独立服务不同桌客人
理解Python并发编程必须掌握GIL(Global Interpreter Lock)概念:
python
# GIL本质:互斥锁,确保同一时刻仅有一个线程执行Python字节码import threading
def count_down():
global counter
while counter > 0:
counter -= 1
counter = 1000000
#创建两个线程
thread1 = threading.Thread(target=count_down)
thread2 = threading.Thread(target=count_down)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(f"最终计数: {counter}") # 结果可能不为0,GIL导致竞争条件
GIL影响分析:
1.保护内存管理,避免竞争条件
2.限制多线程的CPU并行能力
3.导致CPU密集型任务多线程性能不佳
适用场景: I/O密集型任务
python
import threadingimport timeimport requests
def download_site(url):
"""模拟I/O密集型操作"""
response = requests.get(url)
print(f"下载 {url}, 内容长度: {len(response.content)}")
def threading_demo():
urls = [
"https://www.python.org",
"https://www.google.com",
"https://www.github.com",
"https://www.stackoverflow.com"
]
start_time = time.time()
threads = []
for url in urls:
thread = threading.Thread(target=download_site, args=(url,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print(f"多线程总耗时: {time.time() - start_time:.2f}秒")
技术优势:
技术局限:
适用场景: CPU密集型任务
python
import multiprocessingimport timeimport math
def calculate_factorial(n):
"""模拟CPU密集型计算"""
result = math.factorial(n)
print(f"完成 {n} 的阶乘计算")
return result
def multiprocessing_demo():
numbers = [10000, 20000, 30000, 40000]
start_time = time.time()
with multiprocessing.Pool(processes=4) as pool:
results = pool.map(calculate_factorial, numbers)
print(f"多进程总耗时: {time.time() - start_time:.2f}秒")
return results
技术优势:
技术局限:
适用场景: 高并发I/O操作
python
import asyncioimport aiohttpimport time
async def async_download_site(session, url):
"""异步I/O操作"""
async with session.get(url) as response:
content = await response.read()
print(f"下载 {url}, 内容长度: {len(content)}")
async def async_main():
urls = [
"https://www.python.org",
"https://www.google.com",
"https://www.github.com",
"https://www.stackoverflow.com"
]
start_time = time.time()
async with aiohttp.ClientSession() as session:
tasks = []
for url in urls:
task = asyncio.create_task(async_download_site(session, url))
tasks.append(task)
await asyncio.gather(*tasks)
print(f"协程总耗时: {time.time() - start_time:.2f}秒")
# 执行协程
asyncio.run(async_main())
技术优势:
技术局限:
使用相同任务测试三种方案的性能表现:
python
import timeimport threadingimport multiprocessingimport asyncioimport aiohttp
def test_performance():
"""并发方案性能对比测试"""
urls = ["https://httpbin.org/delay/1"] * 10 # 10个延迟1秒的请求
1. 同步方式(性能基准)
start = time.time()
for url in urls:
requests.get(url)
sync_time = time.time() - start
2. 多线程方案
start = time.time()
threads = []
for url in urls:
t = threading.Thread(target=requests.get, args=(url,))
threads.append(t)
t.start()
for t in threads:
t.join()
thread_time = time.time() - start
3. 多进程方案
start = time.time()
with multiprocessing.Pool(10) as pool:
pool.map(requests.get, urls)
process_time = time.time() - start
4. 协程方案
async def async_test():
async with aiohttp.ClientSession() as session:
tasks = [session.get(url) for url in urls]
await asyncio.gather(*tasks)
start = time.time()
asyncio.run(async_test())
async_time = time.time() - start
print(f"同步执行: {sync_time:.2f}s")
print(f"多线程执行: {thread_time:.2f}s")
print(f"多进程执行: {process_time:.2f}s")
print(f"协程执行: {async_time:.2f}s")
并发规模评估
系统集成考量
实际项目常需组合多种技术方案:
python
import asyncioimport multiprocessingfrom concurrent.futures import ProcessPoolExecutor
def cpu_intensive_task(data):
"""CPU密集型任务处理"""
# 复杂计算逻辑
return result
async def main():
# I/O密集型部分使用协程
data = await fetch_data_async()
# CPU密集型部分使用多进程
with ProcessPoolExecutor() as executor:
loop = asyncio.get_event_loop()
results = await loop.run_in_executor(
executor, cpu_intensive_task, data
)
return results
python
# 错误示例
counter = 0
def unsafe_increment():
global counter
for _ in range(100000):
counter += 1 # 非原子操作,产生竞争条件
# 正确方案:使用锁机制from threading import Lock
lock = Lock()
def safe_increment():
global counter
for _ in range(100000):
with lock:
counter += 1
python
# 错误示例async def bad_async():
time.sleep(1) # 阻塞整个事件循环!
# 正确方案:使用异步等待async def good_async():
await asyncio.sleep(1) # 非阻塞操作
| 方案 | 适用场景 | 核心优势 | 主要局限 |
| 多线程 | I/O密集型,小规模并发 | 开销小,共享内存 | 受GIL限制 |
| 多进程 | CPU密集型计算 | 真正并行,绕过GIL | 开销大,通信复杂 |
| 协程 | 高并发I/O操作 | 性能极高,资源开销小 | 需要异步生态 |
选型建议:

一家致力于优质服务的软件公司
8年互联网行业经验1000+合作客户2000+上线项目60+服务地区

关注微信公众号
