Skip to content

Commit

Permalink
refactor: use threading.local for httpx exceptions
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
michalc committed Mar 16, 2024
1 parent 178d09b commit b73e414
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions sqlite_s3_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b73e414

Please sign in to comment.