Skip to content

Alternative supervision logic / custom nurseries #569

Description

@miracle2k

I saw this topic mentioned a couple times in various tickets. Working with trio over the last couple of weeks, there were two specific kinds of behaviour that I felt I wanted to have.

One was a kind of "main task" mode. If the task ends, without an exception, I want the nursery to shutdown, rather than wait until other tasks are complete. This is a basically just a protection against "this task should not really end without an exception, but due to some bug in does". It's a pretty minor thing, so I won't talk about it more.

The more useful behaviour is a kind of "daemon" mode. A background task that will not block the nursery from exiting. I had of cases where I needed this; it was always about trying to process events concurrently with task.

My intention was to implement this in a custom nursery, and then ask here about what trio could do to make this kind of think easier or nicer. But I think the result turned out to be pretty nice and usable already, so maybe no action is required.

The code is here: https://gist.github.com/miracle2k/8499df40a7b650198bbbc3038a6fb292

And I am using it like this:

async def wait_for_stop_signal(self):    
    await self.stop_event.wait()
    raise StopSignalReceived()

async with open_special_nursery() as nursery:
    nursery.start_soon(wait_for_stop_signal, daemon=True)
    nursery.start_soon(func)

Hopefully this is useful for some.

I know there was some talk about Erlang-style supervision trees; I am not sure what the current plan is regarding custom supervision logic, but from this experiment, it seems to me that working on top of the existing nursery API is pretty flexible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions