diff --git a/asgiref/sync.py b/asgiref/sync.py index f263a432..c38a16eb 100644 --- a/asgiref/sync.py +++ b/asgiref/sync.py @@ -519,6 +519,16 @@ def thread_handler(self, loop, exc_info, task_context, func, *args, **kwargs): else: return func(*args, **kwargs) + @staticmethod + def get_current_task() -> Optional["asyncio.Task[Any]"]: + """ + Implementation of asyncio.current_task() + that returns None if there is no task. + """ + try: + return asyncio.current_task() + except RuntimeError: + return None @overload def async_to_sync(