Fix asyncio deprecation asyncio.Task.all_tasks

This commit is contained in:
coffeebenzene
2020-12-02 17:57:28 +00:00
parent 4b254fe5ed
commit ed6231d3aa
2 changed files with 18 additions and 5 deletions

View File

@@ -29,6 +29,13 @@ from urllib.parse import urlunparse
if sys.version_info[:2] < (3, 3):
raise ValueError("Python < 3.3 not supported: %s" % sys.version)
# For compatibility with python versions 3.6 or earlier.
# asyncio.Task.all_tasks() is fully moved to asyncio.all_tasks() starting with 3.9. Also applies to current_task.
try:
asyncio.all_tasks
except AttributeError as e:
asyncio.all_tasks = asyncio.Task.all_tasks
asyncio.current_task = asyncio.Task.current_task
from dateutil.parser import parse as parse_date
from jinja2 import Environment, FileSystemLoader, PrefixLoader, ChoiceLoader
@@ -2817,9 +2824,7 @@ class JupyterHub(Application):
async def shutdown_cancel_tasks(self, sig):
"""Cancel all other tasks of the event loop and initiate cleanup"""
self.log.critical("Received signal %s, initiating shutdown...", sig.name)
tasks = [
t for t in asyncio.Task.all_tasks() if t is not asyncio.Task.current_task()
]
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
if tasks:
self.log.debug("Cancelling pending tasks")
@@ -2832,7 +2837,7 @@ class JupyterHub(Application):
except StopAsyncIteration as e:
self.log.error("Caught StopAsyncIteration Exception", exc_info=True)
tasks = [t for t in asyncio.Task.all_tasks()]
tasks = [t for t in asyncio.all_tasks()]
for t in tasks:
self.log.debug("Task status: %s", t)
await self.cleanup()

View File

@@ -28,6 +28,14 @@ from tornado.httpclient import HTTPError
from tornado.log import app_log
from tornado.platform.asyncio import to_asyncio_future
# For compatibility with python versions 3.6 or earlier.
# asyncio.Task.all_tasks() is fully moved to asyncio.all_tasks() starting with 3.9. Also applies to current_task.
try:
asyncio.all_tasks
except AttributeError as e:
asyncio.all_tasks = asyncio.Task.all_tasks
asyncio.current_task = asyncio.Task.current_task
def random_port():
"""Get a single random port."""
@@ -474,7 +482,7 @@ def print_stacks(file=sys.stderr):
# also show asyncio tasks, if any
# this will increase over time as we transition from tornado
# coroutines to native `async def`
tasks = asyncio.Task.all_tasks()
tasks = asyncio.all_tasks()
if tasks:
print("AsyncIO tasks: %i" % len(tasks))
for task in tasks: