Python中真正的并行可通过multiprocessing实现多进程计算,利用独立解释器绕过GIL;在I/O密集场景下,threading因GIL释放可并发执行;高I/O任务推荐asyncio协程提升效率;性能关键代码可用Cython释放GIL实现多线程并行。

Python中由于全局解释器锁(GIL)的存在,传统的多线程在CPU密集型任务中并不能真正实现并行执行。也就是说,多个线程在同一时刻只能有一个在执行Python字节码,因此无法充分利用多核CPU。但这并不意味着Python不能实现真正的“多线程”并行——关键在于如何绕过GIL的限制。
要实现真正的并行计算,推荐使用multiprocessing模块。它通过创建多个进程来绕过GIL,每个进程拥有独立的Python解释器和内存空间,从而实现多核并行。
示例:import multiprocessing <p>def cpu_task(n): return sum(i * i for i in range(n))</p><p>if <strong>name</strong> == '<strong>main</strong>': with multiprocessing.Pool() as pool: results = pool.map(cpu_task, [100000] * 4) print(results)
这种方式适用于计算密集型任务,如数据处理、数学运算等。
某些操作(如I/O、正则、NumPy计算)会释放GIL。在这种情况下,使用threading也能获得一定并发效果。
立即学习“Python免费学习笔记(深入)”;
适用场景包括:例如:
import threading
import requests
<p>def fetch_url(url):
response = requests.get(url)
print(f"Status: {response.status_code}")</p><p>threads = [
threading.Thread(target=fetch<em>url, args=("<a href="https://www.php.cn/link/5f69e19efaba426d62faeab93c308f5c">https://www.php.cn/link/5f69e19efaba426d62faeab93c308f5c</a>",))
for </em> in range(5)
]</p><p>for t in threads:
t.start()
for t in threads:
t.join()
虽然仍是“多线程”,但由于I/O期间GIL被释放,因此能实现并发,提升整体效率。
对于高并发I/O任务,推荐使用asyncio + async/await,它通过事件循环在一个线程内高效调度成千上万个协程。
示例:import asyncio import aiohttp <p>async def fetch(session, url): async with session.get(url) as response: return response.status</p><p>async def main(): async with aiohttp.ClientSession() as session: tasks = [fetch(session, "<a href="https://www.php.cn/link/5f69e19efaba426d62faeab93c308f5c">https://www.php.cn/link/5f69e19efaba426d62faeab93c308f5c</a>") for _ in range(5)] results = await asyncio.gather(*tasks) print(results)</p><p>asyncio.run(main())
这种方式不依赖多线程,但能实现极高I/O并发,且资源消耗更低。
如果你有性能关键代码,可以用Cython编写,并在其中释放GIL,从而允许其他线程并行执行。
示例(Cython):# cython: boundscheck=False, wraparound=False
def cpu_heavy(double[:,:] arr) nogil:
cdef int i, j
cdef double total = 0.0
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
total += arr[i, j] * arr[i, j]
return total
在调用这段代码时,可以启用多线程并真正并行执行。
基本上就这些。Python中“真正多线程”的实现不是靠threading本身,而是通过多进程、异步、系统调用或底层扩展来突破GIL限制。选择哪种方式取决于你的任务类型:计算密集用multiprocessing,I/O密集用asyncio或带阻塞释放的threading。不复杂但容易忽略的是区分清楚任务性质再选方案。
以上就是python真正实现多线程要怎么做?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号