You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否需始终使用asyncio.Lock保障Python虚拟线程的锁公平性?

Python虚拟线程下threading.Lock的公平性问题与解决方案

核心问题分析

  • threading.Lock是非公平锁:底层依赖操作系统互斥量实现,不保证等待线程/虚拟线程按到达顺序获取锁,存在线程饥饿风险——某些线程可能长期抢不到锁。
  • 虚拟线程(Python 3.10+)下该问题依然存在:虽然虚拟线程是用户态调度,但等待threading.Lock时会被挂起,锁的获取逻辑仍由底层非公平机制决定,无法保证任务的到达顺序。
  • asyncio.Lock不能替代:它是专为异步任务设计的锁,仅能在asyncio事件循环内使用,直接在线程(包括虚拟线程)中调用会阻塞整个线程,且不具备线程安全性,跨线程使用会引发问题。

解决方案:实现公平锁

可以基于threading.Condition和队列手动实现公平锁,确保等待的线程/虚拟线程严格按到达顺序获取锁:

import threading

class FairLock:
    def __init__(self):
        self._inner_lock = threading.Lock()
        self._condition = threading.Condition(self._inner_lock)
        self._waiting_queue = []
        self._current_owner = None

    def acquire(self):
        with self._inner_lock:
            thread_id = threading.get_ident()
            # 支持重入(可选,若不需要可删除此判断)
            if self._current_owner == thread_id:
                return
            self._waiting_queue.append(thread_id)
            # 等待直到自己处于队列头部且锁未被持有
            while self._current_owner is not None or self._waiting_queue[0] != thread_id:
                self._condition.wait()
            # 获取锁
            self._waiting_queue.pop(0)
            self._current_owner = thread_id

    def release(self):
        with self._inner_lock:
            if self._current_owner != threading.get_ident():
                raise RuntimeError("当前线程未持有该锁")
            self._current_owner = None
            if self._waiting_queue:
                self._condition.notify_all()

    # 支持上下文管理器语法
    def __enter__(self):
        self.acquire()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.release()

使用方式

替换原代码中的threading.Lock即可:

g_lock = FairLock()
def my_threaded_functionality():
    with g_lock:
        # ... 处理共享资源 ...

替代方案:基于队列的串行化处理

如果共享资源的访问逻辑可以封装为任务,也可以用queue.Queue实现天然的公平访问:

  1. 将每个请求的处理逻辑封装为任务函数,放入队列。
  2. 启动一个单独的线程(或虚拟线程),从队列中按顺序取出任务执行。
    这种方式无需手动处理锁,队列的FIFO特性天然保证了任务的到达顺序,从根源上避免饥饿问题。

内容的提问来源于stack exchange,提问作者SomethingSomething

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:08:30