Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Patch task for Python>=3.7 again, reverts #57, fixes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
erdewit committed Dec 1, 2021
1 parent da7b523 commit adfc2f9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions nest_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ def _check_running(self):

def _patch_task():
"""Patch the Task's step and enter/leave methods to make it reentrant."""

def step(task, exc=None):
curr_task = curr_tasks.get(task._loop)
try:
step_orig(task, exc)
finally:
if curr_task is None:
curr_tasks.pop(task._loop, None)
else:
curr_tasks[task._loop] = curr_task

Task = asyncio.Task
if sys.version_info >= (3, 7, 0):

Expand All @@ -188,18 +199,9 @@ def leave_task(loop, task):
asyncio.tasks._enter_task = enter_task
asyncio.tasks._leave_task = leave_task
curr_tasks = asyncio.tasks._current_tasks
step_orig = Task._Task__step
Task._Task__step = step
else:

def step(task, exc=None):
curr_task = curr_tasks.get(task._loop)
try:
step_orig(task, exc)
finally:
if curr_task is None:
curr_tasks.pop(task._loop, None)
else:
curr_tasks[task._loop] = curr_task

curr_tasks = Task._current_tasks
step_orig = Task._step
Task._step = step
Expand Down

0 comments on commit adfc2f9

Please sign in to comment.