Skip to content
Discussion options

You must be logged in to vote

Add tasks to TaskGroup

    async with asyncer.create_task_group() as task_group:
        result1 = task_group.soonify(do_work)(name="task 1", tm=1)
        _ = task_group.soonify(do_work)(name="task 2", tm=2)

and use

        while not result1.ready:  # Untill task 1 is done
            await anyio.sleep(0)  # Give event loop time to execute tasks

to execute them until first task is done.
Then cancel tasks

        task_group.cancel_scope.cancel()

Full code example:

import anyio
import asyncer


async def do_work(name: str, tm: int) -> str:
    await anyio.sleep(tm)
    print(f"Hello {name}")
    return f"Hello {name}"


async def main():
    async with asyncer.create_task_group() as task_g…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants