[compat/asyncio] Use asyncio.all_tasks

This commit is contained in:
pukkandan 2022-04-26 05:45:18 +05:30
parent 9196cbfe8b
commit 059bc4db19
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
4 changed files with 11 additions and 20 deletions

View File

@ -66,7 +66,7 @@ offlinetest: codetest
# XXX: This is hard to maintain # XXX: This is hard to maintain
CODE_FOLDERS = yt_dlp yt_dlp/downloader yt_dlp/extractor yt_dlp/postprocessor yt_dlp/compat \ CODE_FOLDERS = yt_dlp yt_dlp/downloader yt_dlp/extractor yt_dlp/postprocessor yt_dlp/compat \
yt_dlp/compat/asyncio yt_dlp/extractor/anvato_token_generator yt_dlp/extractor/anvato_token_generator
yt-dlp: yt_dlp/*.py yt_dlp/*/*.py yt-dlp: yt_dlp/*.py yt_dlp/*/*.py
mkdir -p zip mkdir -p zip
for d in $(CODE_FOLDERS) ; do \ for d in $(CODE_FOLDERS) ; do \

View File

@ -2,8 +2,7 @@
from asyncio import * # noqa: F403 from asyncio import * # noqa: F403
from . import tasks # noqa: F401 from .compat_utils import passthrough_module
from ..compat_utils import passthrough_module
passthrough_module(__name__, 'asyncio') passthrough_module(__name__, 'asyncio')
del passthrough_module del passthrough_module
@ -18,3 +17,8 @@ def run(coro):
loop = new_event_loop() loop = new_event_loop()
set_event_loop(loop) set_event_loop(loop)
loop.run_until_complete(coro) loop.run_until_complete(coro)
try:
all_tasks # >= 3.7
except NameError:
all_tasks = Task.all_tasks

View File

@ -1,13 +0,0 @@
# flake8: noqa: F405
from asyncio.tasks import * # noqa: F403
from ..compat_utils import passthrough_module
passthrough_module(__name__, 'asyncio.tasks')
del passthrough_module
try: # >= 3.7
all_tasks
except NameError:
all_tasks = Task.all_tasks

View File

@ -5221,7 +5221,7 @@ class WebSocketsWrapper():
pool = None pool = None
def __init__(self, url, headers=None, connect=True): def __init__(self, url, headers=None, connect=True):
self.loop = asyncio.events.new_event_loop() self.loop = asyncio.new_event_loop()
# XXX: "loop" is deprecated # XXX: "loop" is deprecated
self.conn = websockets.connect( self.conn = websockets.connect(
url, extra_headers=headers, ping_interval=None, url, extra_headers=headers, ping_interval=None,
@ -5252,7 +5252,7 @@ def __exit__(self, type, value, traceback):
# for contributors: If there's any new library using asyncio needs to be run in non-async, move these function out of this class # for contributors: If there's any new library using asyncio needs to be run in non-async, move these function out of this class
@staticmethod @staticmethod
def run_with_loop(main, loop): def run_with_loop(main, loop):
if not asyncio.coroutines.iscoroutine(main): if not asyncio.iscoroutine(main):
raise ValueError(f'a coroutine was expected, got {main!r}') raise ValueError(f'a coroutine was expected, got {main!r}')
try: try:
@ -5264,7 +5264,7 @@ def run_with_loop(main, loop):
@staticmethod @staticmethod
def _cancel_all_tasks(loop): def _cancel_all_tasks(loop):
to_cancel = asyncio.tasks.all_tasks(loop) to_cancel = asyncio.all_tasks(loop)
if not to_cancel: if not to_cancel:
return return
@ -5274,7 +5274,7 @@ def _cancel_all_tasks(loop):
# XXX: "loop" is removed in python 3.10+ # XXX: "loop" is removed in python 3.10+
loop.run_until_complete( loop.run_until_complete(
asyncio.tasks.gather(*to_cancel, loop=loop, return_exceptions=True)) asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))
for task in to_cancel: for task in to_cancel:
if task.cancelled(): if task.cancelled():