Skip to content

Commit b73e414

Browse files
committed
refactor: use threading.local for httpx exceptions
I wasn't really aware of threading.local before. This is a bit simpler, has no (obvious) locking, and so I suspect maybe a touch more performant (although I've not measured it)
1 parent 178d09b commit b73e414

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

sqlite_s3_query.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,17 @@ def sqlite_s3_query_multi(url, get_credentials=lambda now: (
7171
body_hash = sha256(b'').hexdigest()
7272
scheme, netloc, path, _, _ = urlsplit(url)
7373

74-
# We could use contextvars, but they aren't introduced until Python 3.7
75-
pending_exceptions = {}
76-
pending_exception_lock = threading.Lock()
74+
local = threading.local()
75+
local.pending_exception = None
7776

7877
def set_pending_exception(exception):
79-
thread_id = threading.get_ident()
80-
with pending_exception_lock:
81-
pending_exceptions[thread_id] = exception
78+
local.pending_exception = exception
8279

8380
def raise_any_pending_exception():
84-
thread_id = threading.get_ident()
85-
with pending_exception_lock:
86-
try:
87-
raise pending_exceptions.pop(thread_id)
88-
except KeyError:
89-
pass
81+
to_raise = local.pending_exception
82+
if to_raise is not None:
83+
local.pending_exception = None
84+
raise to_raise
9085

9186
def run(func, *args):
9287
res = func(*args)

0 commit comments

Comments
 (0)