Python并发编程之多线程与多进程
Python并发编程多线程与多进程一、并发与并行并发多个任务在重叠时间段内交替执行。并行多个任务在同一时刻真正同时运行。Python中多线程受GIL限制适合IO密集型多进程适合CPU密集型。二、threading基础import threading, timedef download(url):print(f下载 {url})time.sleep(2)print(f{url} 完成)threads [threading.Thread(targetdownload, args(furl-{i},)) for i in range(5)]for t in threads: t.start()for t in threads: t.join()三、线程同步import threadingclass Account:def __init__(self, balance):self.balance balanceself.lock threading.Lock()def withdraw(self, amount):with self.lock:if self.balance amount:self.balance - amountreturn Truereturn False其他同步工具- RLock可重入锁- Semaphore信号量限制并发数- Event事件通知- Condition条件变量四、线程池from concurrent.futures import ThreadPoolExecutor, as_completeddef fetch(url):return requests.get(url).status_codeurls [http://example.com] * 10with ThreadPoolExecutor(max_workers5) as pool:results list(pool.map(fetch, urls))五、multiprocessingfrom multiprocessing import Process, Queue, Pooldef cpu_work(n):return sum(i*i for i in range(n))with Pool(processes4) as pool:results pool.map(cpu_work, [1000000] * 8)进程间通信使用 Queue、Pipe、SharedMemory。六、进程池from concurrent.futures import ProcessPoolExecutordef is_prime(n):if n 2: return Falsefor i in range(2, int(n**0.5)1):if n % i 0: return Falsereturn Truewith ProcessPoolExecutor() as pool:results pool.map(is_prime, range(100000, 101000))七、选择建议任务类型 推荐方案 原因IO密集型 多线程/asyncio GIL在IO时释放CPU密集型 多进程 真正并行混合型 多进程线程池 各自处理对应任务总结IO密集型用线程CPU密集型用进程concurrent.futures 提供统一的高层接口。