Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AnyIO #101

Closed
wants to merge 3 commits into from
Closed

Conversation

davidbrochart
Copy link
Contributor

@agronholm I know that you're working on the 5.0 branch, and you have probably been thinking about lots of things that I'm not even aware of, but I've been playing with this PR and the following example code, and it seems to work pretty well.
In #97 you seem to have taken another direction by treating tasks as resources, which is not what I do here, so I'm wondering what I am missing.

from anyio import run, sleep
from asphalt.core import ContainerComponent, Component, context_teardown, run_application

async def task0():
    print("task0 start")
    await sleep(2)
    print("task0 end")

async def task1():
    print("task1 start")
    await sleep(1)
    # 1 / 0
    await sleep(1)
    print("task1 end")

class Component1(Component):
    @context_teardown
    async def start(self, ctx):
        self.task_group.start_soon(task1)
        yield
        print("teardown1")

class Component0(ContainerComponent):
    @context_teardown
    async def start(self, ctx):
        self.add_component("component1", Component1)
        self.task_group.start_soon(task0)
        await sleep(1)
        await super().start(ctx)
        yield
        print("teardown0")

async def main():
    component0 = Component0()
    await run_application(component0, start_timeout=3)

run(main)

@davidbrochart davidbrochart marked this pull request as draft January 17, 2024 22:06
@agronholm
Copy link
Member

Well, having the test suite pass would be a start :)
But seriously, I will take a look at this tomorrow or the day after. I'm pretty sure it's not very simple to move to structured concurrency or I would've done it long ago already.

@davidbrochart
Copy link
Contributor Author

I'm pretty sure it's not very simple to move to structured concurrency or I would've done it long ago already.

I know, that's why I suspect I'm missing something 😄
I will work on tests if you think this PR is worth it.

@agronholm
Copy link
Member

agronholm commented Jan 17, 2024

Meanwhile you can describe for me how you see the startup and shutdown sequences of nested components working with this approach.

@davidbrochart
Copy link
Contributor Author

👍 will do but later, it's late here.

@davidbrochart
Copy link
Contributor Author

The application starts with a top-level task group, that is passed to the root component, and that is used to start it with a timeout. Each ContainerComponent creates a new task group where it starts its child components, and this task group is also passed to the child components.
In the example above, the root component Component0 starts a task task0 using its component-level task group, and adds Component1 which starts task1 with its (nested) component-level task group. If no exception happens, the application runs until all tasks are done, then each component's teardown runs. If an exception happens (like in task1 by uncommenting 1 / 0), the application stops after running the teardowns, and the exception is finally raised.

@davidbrochart davidbrochart force-pushed the anyio branch 3 times, most recently from f7ec4a0 to 427d0b4 Compare January 18, 2024 15:43
@coveralls
Copy link

coveralls commented Jan 19, 2024

Coverage Status

coverage: 96.39% (-1.0%) from 97.393%
when pulling e08eb00 on davidbrochart:anyio
into bac9743 on asphalt-framework:master.

@agronholm
Copy link
Member

I'll take a closer look at this on the weekend. I really, really want structured concurrency in Asphalt ASAP.

@davidbrochart
Copy link
Contributor Author

Closing in favor of #102.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants