Skip to content

Commit

Permalink
Decide current_task method to use at import time
Browse files Browse the repository at this point in the history
Saves 1 hasattr call per usage of Local.__getattr__
  • Loading branch information
kezabelle committed Aug 16, 2021
1 parent 4841c13 commit f53fbef
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions asgiref/sync.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit f53fbef

Please sign in to comment.