From f53fbef809e6b75076e03595efa865db04dbeaeb Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 16 Aug 2021 16:21:17 +0100 Subject: [PATCH] Decide current_task method to use at import time Saves 1 hasattr call per usage of Local.__getattr__ --- asgiref/sync.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/asgiref/sync.py b/asgiref/sync.py index 0d9464fd..8e555cf8 100644 --- a/asgiref/sync.py +++ b/asgiref/sync.py @@ -15,8 +15,11 @@ if sys.version_info >= (3, 7): import contextvars + + current_task = asyncio.current_task else: contextvars = None + current_task = asyncio.Task.current_task def _restore_context(context): @@ -498,12 +501,7 @@ def get_current_task(): Returns None if there is no task. """ try: - if hasattr(asyncio, "current_task"): - # Python 3.7 and up - return asyncio.current_task() - else: - # Python 3.6 - return asyncio.Task.current_task() + return current_task() except RuntimeError: return None