From b73e4148c398246a2d9159b75b561187812caae2 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Sat, 16 Mar 2024 07:53:58 +0000 Subject: [PATCH] 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) --- sqlite_s3_query.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/sqlite_s3_query.py b/sqlite_s3_query.py index 009a098..4da69ef 100644 --- a/sqlite_s3_query.py +++ b/sqlite_s3_query.py @@ -71,22 +71,17 @@ def sqlite_s3_query_multi(url, get_credentials=lambda now: ( body_hash = sha256(b'').hexdigest() scheme, netloc, path, _, _ = urlsplit(url) - # We could use contextvars, but they aren't introduced until Python 3.7 - pending_exceptions = {} - pending_exception_lock = threading.Lock() + local = threading.local() + local.pending_exception = None def set_pending_exception(exception): - thread_id = threading.get_ident() - with pending_exception_lock: - pending_exceptions[thread_id] = exception + local.pending_exception = exception def raise_any_pending_exception(): - thread_id = threading.get_ident() - with pending_exception_lock: - try: - raise pending_exceptions.pop(thread_id) - except KeyError: - pass + to_raise = local.pending_exception + if to_raise is not None: + local.pending_exception = None + raise to_raise def run(func, *args): res = func(*args)