Skip to content

Instantly share code, notes, and snippets.

@Albus
Last active June 2, 2025 13:30
Show Gist options
  • Save Albus/29749425ca58e8137690e49c5b211f4d to your computer and use it in GitHub Desktop.
Save Albus/29749425ca58e8137690e49c5b211f4d to your computer and use it in GitHub Desktop.
async typer example
# /// 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)
@Albus
Copy link
Author

Albus commented Jun 2, 2025

uvx --python 3.13 --with asyncer,uvloop typer main.py run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment