Created
February 23, 2023 13:31
-
-
Save torarvid/359859e2274d55709a4cf5ae515d90c8 to your computer and use it in GitHub Desktop.
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
import asyncio | |
import traceback | |
from asyncio import TaskGroup | |
from typing import Awaitable, Callable | |
async def raiser(typ: type[Exception]): | |
raise typ(f"{typ.__name__} error") | |
async def taskgroup(): | |
async with TaskGroup() as g: | |
g.create_task(raiser(ValueError)) | |
g.create_task(raiser(TypeError)) | |
g.create_task(raiser(ValueError)) | |
async def gather(): | |
await asyncio.gather(raiser(ValueError), raiser(TypeError)) | |
async def main(coro: Callable[[], Awaitable[None]]): | |
try: | |
await coro() | |
except TypeError as e: | |
print("A TypeError", e) | |
traceback.print_exc() | |
except ValueError as e: | |
print("A ValueError", e) | |
traceback.print_exc() | |
except Exception as e: | |
print("Exception", e) | |
traceback.print_exc() | |
async def main_star(coro: Callable[[], Awaitable[None]]): | |
try: | |
await coro() | |
except* TypeError as e: | |
print("A TypeError", e) | |
traceback.print_exc() | |
except* ValueError as e: | |
print("A ValueError", e) | |
traceback.print_exc() | |
except* Exception as e: | |
print("Exception", e) | |
traceback.print_exc() | |
asyncio.run(main(taskgroup)) | |
print("\n\n\n") | |
asyncio.run(main(gather)) | |
print("\n\n\n") | |
asyncio.run(main_star(taskgroup)) | |
print("\n\n\n") | |
asyncio.run(main_star(gather)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment