diff options
author | Richard Kojedzinszky <richard@kojedz.in> | 2022-03-13 08:47:30 +0100 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2022-03-18 16:58:38 -0400 |
commit | a02c1657dc79e5e25ed0424faa5db66ecda8c4a2 (patch) | |
tree | 3887834ec350086bb4443d0cfdff448d000cc071 | |
parent | d25e5f31490da2aee8b75d8a3aca338abc490f73 (diff) |
util: store thread assigned id in thread-local storage
Fixes #2002
-rw-r--r-- | paramiko/util.py | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/paramiko/util.py b/paramiko/util.py index 93970289..a344915c 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -225,24 +225,20 @@ def mod_inverse(x, m): return u2 -_g_thread_ids = {} +_g_thread_data = threading.local() _g_thread_counter = 0 _g_thread_lock = threading.Lock() def get_thread_id(): - global _g_thread_ids, _g_thread_counter, _g_thread_lock - tid = id(threading.currentThread()) + global _g_thread_data, _g_thread_counter, _g_thread_lock try: - return _g_thread_ids[tid] - except KeyError: - _g_thread_lock.acquire() - try: + return _g_thread_data.id + except AttributeError: + with _g_thread_lock: _g_thread_counter += 1 - ret = _g_thread_ids[tid] = _g_thread_counter - finally: - _g_thread_lock.release() - return ret + _g_thread_data.id = _g_thread_counter + return _g_thread_data.id def log_to_file(filename, level=DEBUG): |