Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use threading.local for httpx exceptions #93

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading