Skip to content

Non-issue with coroutines that don't checkpoint #12

@davidbrochart

Description

@davidbrochart

Interestingly, the following code fails to run task_that_never_continues() with both asyncio or Trio (see Trio's documentation):

from anyio import create_task_group, run, sleep

async def do_nothing():
    pass

async def task_that_never_continues():
    print("some time for the other task to run...")
    await anyio.sleep(0.1)
    print("this part never runs")

async def task_that_never_lets_other_tasks_run():
    while True:
        await do_nothing()
        # await sleep(0)


async def main():
    async with create_task_group() as tg:
        tg.start_soon(task_that_never_continues)
        tg.start_soon(task_that_never_lets_other_tasks_run)

run(main)

But it is fine with tinyio:

import tinyio

def do_nothing():
    pass

def task_that_never_continues():
    print("some time for the other task to run...")
    yield tinyio.sleep(0.1)
    print("this part does run")

def task_that_never_lets_other_tasks_run():
    while True:
        yield do_nothing()


def main():
    yield [task_that_never_continues(), task_that_never_lets_other_tasks_run()]

loop = tinyio.Loop()
out = loop.run(main())

To me, not yielding back to the event loop when running an async function (even though it is secretly synchronous) is a big issue, since it should be a strong guarantee. So kudos to you @patrick-kidger for solving it in tinyio!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions