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:41:18 -0400 |
commit | 3853951b304c34a97ddb39204c5b47d944f2498a (patch) | |
tree | 275df194b8ada8349fa2131bb4442e742556f531 | |
parent | 609e01d550f62253c56c34fc5a49d01f1d49e6e2 (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 a4e8f70b..4267caf1 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): |