From 6ba6881879480da2d115cd9b8d75bf599ad8814b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ni=20Gauffier?= Date: Thu, 21 Mar 2024 16:46:40 +0100 Subject: [PATCH] Revert removing of SyncToAsync.get_current_task --- asgiref/sync.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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(