Skip to content

Commit

Permalink
Reuse the existing CurrentThreadExecutor if there is one
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Feb 5, 2025
1 parent 7983c75 commit cd2f065
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
# Make a future for the return information
call_result: "Future[_R]" = Future()

# Make a CurrentThreadExecutor we'll use to idle in this thread - we
# need one for every sync frame, even if there's one above us in the
# same thread.
# Make a CurrentThreadExecutor we'll use to idle in this thread, unless
# we can reuse one above us in the same thread.
old_executor = getattr(self.executors, "current", None)
current_executor = CurrentThreadExecutor()
self.executors.current = current_executor
if old_executor is None:
current_executor = CurrentThreadExecutor()
self.executors.current = current_executor
else:
current_executor = old_executor

# Wrapping context in list so it can be reassigned from within
# `main_wrap`.
Expand Down

0 comments on commit cd2f065

Please sign in to comment.