【问题标题】:Python: Thread safe dictionary with short lived keys, is this correct?Python:具有短寿命键的线程安全字典,这是正确的吗?
【发布时间】:2023-04-02 04:48:01
【问题描述】:

import threading
import weakref
_mainlock = threading.RLock()
_job_locks = weakref.WeakValueDictionary()
def do_thing(job_id):
    _mainlock.acquire() #Dictionary modification lock acquire
    _job_locks.setdefault(job_id, threading.RLock()) #Possibly modifies the dictionary
    _mainlock.release()
    _job_locks[job_id].acquire()
    try:
        one_time_init(job_id)
    finally:
        _job_locks[job_id].release()
    #On function return, the weakref.WeakValueDictionary should cause the key to evaporate

假设 do_thing() 在许多 id 编号可能相同也可能不同的线程上被多次调用(例如,4 次 ID 为 3,每次都有不同的 ID),这个线程安全吗? one_time_init() 是否会一次针对特定的作业 ID 运行多次? (PS:one_time_init 保存了每个 ID 已经运行一次的状态,所以如果它已经运行完成,调用它是一个空操作)


更新代码(感谢 THC4k):

import threading
import weakref
_mainlock = threading.RLock()
_job_locks = weakref.WeakValueDictionary()

def do_thing(job_id):
    with _mainlock:
        jl = _job_locks.setdefault(job_id, threading.RLock())
    with jl:
        one_time_init(job_id)

【问题讨论】:

    标签:
    python
    multithreading
    dictionary
    thread-safety
    weak-references