-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go back to using nest_asyncio for run_sync utility
- Loading branch information
Showing
3 changed files
with
71 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
# Note: copied from https://github.com/ipyflow/ipyflow/blob/8e4bc5cb8d4231b9b69f4f9dce867b8101164ac5/core/ipyflow/kernel/patched_nest_asyncio.py | ||
import asyncio | ||
import sys | ||
|
||
|
||
def apply(loop=None): | ||
import nest_asyncio | ||
|
||
# ref: https://github.com/erdewit/nest_asyncio/issues/14 | ||
nest_asyncio._patch_task = _patched_patch_task | ||
|
||
# ref: https://github.com/erdewit/nest_asyncio | ||
nest_asyncio.apply(loop=loop) | ||
|
||
|
||
def _patched_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): | ||
|
||
def enter_task(loop, task): | ||
curr_tasks[loop] = task | ||
|
||
def leave_task(loop, task): | ||
curr_tasks.pop(loop, None) | ||
|
||
asyncio.tasks._enter_task = enter_task | ||
asyncio.tasks._leave_task = leave_task | ||
curr_tasks = asyncio.tasks._current_tasks | ||
else: | ||
curr_tasks = Task._current_tasks | ||
try: | ||
step_orig = Task._Task__step | ||
Task._Task__step = step | ||
except AttributeError: | ||
try: | ||
step_orig = Task.__step | ||
Task.__step = step | ||
except AttributeError: | ||
step_orig = Task._step | ||
Task._step = step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters