From fda95957fae5e261ae43c430ee36dc2b97308e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Carvalho?= <1012794+RedRoserade@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:19:02 +0000 Subject: [PATCH] Update asyncio.py --- aioitertools/asyncio.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/aioitertools/asyncio.py b/aioitertools/asyncio.py index 8f81c57..31d0e93 100644 --- a/aioitertools/asyncio.py +++ b/aioitertools/asyncio.py @@ -8,6 +8,7 @@ Provisional library. Must be imported as `aioitertools.asyncio`. """ import asyncio +from contextlib import suppress import time from typing import ( Any, @@ -123,18 +124,19 @@ async def as_generated( tailer_count: int = 0 - async def tailer(iter: AsyncIterable[T]) -> None: + async def tailer(iterable: AsyncIterable[T]) -> None: nonlocal tailer_count try: - async for item in iter: + async for item in iterable: await queue.put({"value": item}) except asyncio.CancelledError: - if isinstance(iter, AsyncGenerator): # pragma:nocover - await iter.aclose() + if isinstance(iterable, AsyncGenerator): # pragma:nocover + with suppress(Exception): + await iterable.aclose() raise - except Exception as e: - await queue.put({"exception": e}) + except Exception as exc: + await queue.put({"exception": exc}) finally: tailer_count -= 1 @@ -162,20 +164,16 @@ async def as_generated( raise i["exception"] elif "done" in i: break - except (asyncio.CancelledError, GeneratorExit): pass - finally: for task in tasks: if not task.done(): task.cancel() for task in tasks: - try: + with suppress(asyncio.CancelledError): await task - except asyncio.CancelledError: - pass @deprecated_wait_param