当前位置: 代码网 > it编程>前端脚本>Python > Python GIL(全局解释器锁)的使用小结

Python GIL(全局解释器锁)的使用小结

2025年11月24日 Python 我要评论
我们通常所说的gil,指的是全局解释器锁(global interpreter lock),是计算机程序设计语言解释器用于同步线程的一种机制,它使得任何时刻仅有一个线程在执行。即使在多核处理器上,使用

我们通常所说的gil,指的是全局解释器锁(global interpreter lock),是计算机程序设计语言解释器用于同步线程的一种机制,它使得任何时刻仅有一个线程在执行。即使在多核处理器上,使用 gil 的解释器也只允许同一时间执行一个线程。

在python中,gil的存在主要是为了简化cpython解释器的实现,因为cpython的内存管理不是线程安全的。gil可以防止并发访问python对象,从而避免多个线程同时修改同一个对象导致的数据不一致问题。

但是,gil也导致了一个问题:在多核cpu上,使用多线程的python程序并不能真正地并行执行,而是通过交替执行来模拟并发。因此,对于cpu密集型的任务,使用多线程并不能提高性能,甚至可能因为线程切换的开销而降低性能。

然而,对于i/o密集型的任务(如网络请求、文件读写等),由于线程在等待i/o时会被阻塞,此时gil会被释放,从而允许其他线程运行,因此多线程在i/o密集型任务中仍然可以提升性能。

为了克服gil的限制,可以采用多进程(使用multiprocessing模块)来利用多核cpu,因为每个进程有自己独立的python解释器和内存空间,因此每个进程都有自己的gil,从而可以实现真正的并行。

什么是 gil?

gil(global interpreter lock) 是 cpython 解释器中的一个互斥锁,它确保在任何时刻只有一个线程在执行 python 字节码。这意味着即使在多核 cpu 上,cpython 也无法实现真正的并行线程执行。

gil 的工作原理

+-----------------------------------------------+
|              python 进程 (单个进程)            |
|                                               |
|  +-----------------------------------------+  |
|  |          全局解释器锁 (gil)              |  |
|  |                                         |  |
|  |      🔒 一把锁,控制 python 字节码执行    |  |
|  +-----------------------------------------+  |
|                    ↑                          |
|                    | (获取/释放)              |
|                    |                          |
|  +------------+  +------------+  +------------+|
|  |  线程 1    |  |  线程 2    |  |  线程 3    ||
|  |            |  |            |  |            ||
|  | python代码 |  | python代码 |  | python代码 ||
|  |  执行中    |  |  等待中    |  |  等待中    ||
|  +------------+  +------------+  +------------+|
|                                               |
+-----------------------------------------------+

关键点:

  • 🔒 gil 是进程级别的锁,不是线程级别的
  • 📍 同一时间只有一个线程能持有 gil 并执行 python 字节码
  • ⏳ 其他线程必须等待 gil 被释放
import threading
import time

def count_down(n):
    while n > 0:
        n -= 1

# 单线程执行
start = time.time()
count_down(100000000)
single_time = time.time() - start

# 多线程执行
start = time.time()
t1 = threading.thread(target=count_down, args=(50000000,))
t2 = threading.thread(target=count_down, args=(50000000,))
t1.start()
t2.start()
t1.join()
t2.join()
multi_time = time.time() - start

print(f"单线程执行时间: {single_time:.2f}秒")
print(f"双线程执行时间: {multi_time:.2f}秒")
# 你会发现多线程可能比单线程更慢!

为什么需要 gil?

1. 简化内存管理

python 使用引用计数进行内存管理:

import sys

a = []
print(sys.getrefcount(a))  # 查看对象的引用计数

b = a
print(sys.getrefcount(a))  # 引用计数增加

没有 gil 时,多个线程同时修改引用计数会导致竞争条件:

# 伪代码演示竞争条件
# 线程1: obj.ref_count += 1
# 线程2: obj.ref_count -= 1
# 如果没有同步机制,ref_count 可能出错

2. 保护内部数据结构

python 的很多内部数据结构(如 list、dict)不是线程安全的。

gil 的影响

cpu 密集型任务

import threading
import time

def cpu_intensive_task():
    result = 0
    for i in range(10**7):
        result += i * i
    return result

# 测试多线程性能
def test_multithreading():
    threads = []
    start_time = time.time()
    
    for _ in range(4):
        t = threading.thread(target=cpu_intensive_task)
        threads.append(t)
        t.start()
    
    for t in threads:
        t.join()
    
    print(f"多线程执行时间: {time.time() - start_time:.2f}秒")

# 对比多进程
import multiprocessing

def test_multiprocessing():
    processes = []
    start_time = time.time()
    
    for _ in range(4):
        p = multiprocessing.process(target=cpu_intensive_task)
        processes.append(p)
        p.start()
    
    for p in processes:
        p.join()
    
    print(f"多进程执行时间: {time.time() - start_time:.2f}秒")

# 运行测试
if __name__ == "__main__":
    test_multithreading()  # 可能比单线程还慢
    test_multiprocessing()  # 真正的并行,速度更快

i/o 密集型任务

import threading
import time
import requests

def download_site(url, session):
    with session.get(url) as response:
        print(f"read {len(response.content)} from {url}")

def download_all_sites(sites):
    with requests.session() as session:
        # 单线程
        start_time = time.time()
        for url in sites:
            download_site(url, session)
        print(f"单线程下载时间: {time.time() - start_time:.2f}秒")
        
        # 多线程
        start_time = time.time()
        threads = []
        for url in sites:
            thread = threading.thread(target=download_site, args=(url, session))
            thread.start()
            threads.append(thread)
        
        for thread in threads:
            thread.join()
        print(f"多线程下载时间: {time.time() - start_time:.2f}秒")

# i/o 密集型任务中,多线程有明显优势

如何绕过 gil 的限制

1. 使用多进程

from multiprocessing import pool, cpu_count
import math

def is_prime(n):
    if n < 2:
        return false
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return false
    return true

def find_primes_parallel(numbers):
    with pool(processes=cpu_count()) as pool:
        results = pool.map(is_prime, numbers)
    return results

numbers = range(1000000, 1010000)
primes = find_primes_parallel(numbers)
+------------------------+
|   主进程 (协调者)       |
+------------------------+
          |
    +-----+-----+
    |           |
    v           v
+-------+   +-------+
| 进程1 |   | 进程2 |
|       |   |       |
|  🔒gil |   |  🔒gil |  ← 每个进程有独立的gil
+-------+   +-------+

2. 使用 c 扩展

// primes.c
#include <python.h>

static pyobject* find_primes_c(pyobject* self, pyobject* args) {
    py_begin_allow_threads  // 释放 gil
    // 执行计算密集型任务
    py_end_allow_threads    // 重新获取 gil
    return py_buildvalue("i", result);
}
+-----------------------+
|   python 线程         |
+-----------------------+
          |
          v
+-----------------------+
|   释放 gil 的 c 扩展   | ← 在c代码中手动释放gil
+-----------------------+
          |
          v
+-----------------------+
|   并行计算 (无gil限制)  |
+-----------------------+

3. 使用其他 python 实现

  • jython: 基于 jvm,没有 gil
  • ironpython: 基于 .net,没有 gil
  • pypy: 有 gil,但性能更好
+----------------+----------------+----------------+
|   cpython      |   jython       |   ironpython   |
|   (有gil)       |   (无gil)      |   (无gil)      |
+----------------+----------------+----------------+

4. 使用异步编程

import asyncio
import aiohttp

async def download_site_async(session, url):
    async with session.get(url) as response:
        content = await response.read()
        print(f"read {len(content)} from {url}")

async def download_all_sites_async(sites):
    async with aiohttp.clientsession() as session:
        tasks = []
        for url in sites:
            task = asyncio.create_task(download_site_async(session, url))
            tasks.append(task)
        await asyncio.gather(*tasks)

# 运行异步任务
asyncio.run(download_all_sites_async(sites))

gil 的优缺点

优点

  • 简化 cpython 实现
  • 使单线程程序更快(无锁开销)
  • 更容易集成非线程安全的 c 扩展

缺点

  • 限制多核 cpu 的利用率
  • 对 cpu 密集型多线程程序不友好
  • 可能造成性能误解

实际开发建议

# 根据任务类型选择方案:

def choose_concurrency_method(task_type, data):
    if task_type == "cpu_intensive":
        # 使用多进程
        with multiprocessing.pool() as pool:
            return pool.map(process_data, data)
    
    elif task_type == "io_intensive":
        # 使用多线程或异步
        with threadpoolexecutor() as executor:
            return list(executor.map(process_data, data))
    
    elif task_type == "mixed":
        # 混合方案:进程池 + 线程池
        pass

未来展望

python 社区正在探索移除 gil 的方案:

  • nogil 分支:尝试移除 gil 的实验性版本
  • subinterpreters:通过多个解释器实例实现真正的并行

总结

gil 是 cpython 的历史遗留问题,它:

  • 主要影响 cpu 密集型多线程程序
  • 对 i/o 密集型任务影响较小
  • 可以通过多进程、c 扩展、异步编程等方式绕过

到此这篇关于python gil(全局解释器锁)的使用小结的文章就介绍到这了,更多相关python gil全局解释器锁内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com