Skip to content

Commit

Permalink
util: store thread assigned id in thread-local storage
Browse files Browse the repository at this point in the history
Fixes #2002
  • Loading branch information
rkojedzinszky authored and bitprophet committed Mar 18, 2022
1 parent d25e5f3 commit a02c165
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions paramiko/util.py
Expand Up @@ -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):
Expand Down

0 comments on commit a02c165

Please sign in to comment.