From dcabf0eeae027132c1e197c1ed046c1991805355 Mon Sep 17 00:00:00 2001 From: germaniuss Date: Fri, 19 Jan 2024 15:35:21 +0100 Subject: [PATCH] Move main_event_loop and main_event_loop_pid initialization from AsyncToSync.__init__ to AsyncToSync.__call__ --- asgiref/sync.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/asgiref/sync.py b/asgiref/sync.py index 5406b7d3..c044fcc9 100644 --- a/asgiref/sync.py +++ b/asgiref/sync.py @@ -153,7 +153,12 @@ def __init__( self.__self__ = self.awaitable.__self__ # type: ignore[union-attr] except AttributeError: pass - if force_new_loop: + self.force_new_loop = force_new_loop + + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: + __traceback_hide__ = True # noqa: F841 + + if self.force_new_loop: # They have asked that we always run in a new sub-loop. self.main_event_loop = None else: @@ -173,10 +178,7 @@ def __init__( ) else: self.main_event_loop = None - - def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: - __traceback_hide__ = True # noqa: F841 - + # You can't call AsyncToSync from a thread with a running event loop try: event_loop = asyncio.get_running_loop()