Last active
June 2, 2025 13:30
-
-
Save Albus/29749425ca58e8137690e49c5b211f4d to your computer and use it in GitHub Desktop.
async typer example
This file contains hidden or 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
# /// script | |
# requires-python = "~=3.13" | |
# dependencies = [ | |
# "anyio", | |
# "asyncer", | |
# "typer", | |
# "uvloop", | |
# ] | |
# /// | |
from asyncio import set_event_loop_policy, CancelledError, shield | |
from functools import partial | |
from anyio import sleep | |
from asyncer import runnify, create_task_group | |
from uvloop import EventLoopPolicy | |
set_event_loop_policy(EventLoopPolicy()) | |
@partial(runnify, backend_options=dict(debug=__debug__)) | |
async def main(): | |
# noinspection PyUnresolvedReferences | |
from typer import echo | |
async def test_sleep(sec: int): | |
nonlocal echo | |
try: | |
await shield(sleep(sec)) | |
except CancelledError: | |
echo = partial(echo, err= True) | |
finally: | |
echo(f"test_sleep {sec}") | |
async with create_task_group() as tg: | |
tg.soonify(test_sleep)(6) | |
tg.soonify(test_sleep)(5) | |
tg.soonify(test_sleep)(2) | |
tg.soonify(test_sleep)(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.