diff --git a/asgiref/local.py b/asgiref/local.py index 4a17052f..956292cf 100644 --- a/asgiref/local.py +++ b/asgiref/local.py @@ -31,6 +31,7 @@ class Local: """ CLEANUP_INTERVAL = 60 # seconds + launch_map_classes = None # cache imported (AsyncToSync, SyncToAsync) on first access. def __init__(self, thread_critical: bool = False) -> None: self._thread_critical = thread_critical @@ -48,7 +49,12 @@ def _get_context_id(self): Get the ID we should use for looking up variables """ # Prevent a circular reference - from .sync import AsyncToSync, SyncToAsync + # Once imported the first time, hold a reference to them on the class. + if not Local.launch_map_classes: + from .sync import AsyncToSync, SyncToAsync + Local.launch_map_classes = (AsyncToSync, SyncToAsync) + else: + AsyncToSync, SyncToAsync = Local.launch_map_classes # First, pull the current task if we can context_id = SyncToAsync.get_current_task()