trio icon indicating copy to clipboard operation
trio copied to clipboard

Enhance trio.Event() to pass user data

Open mic006 opened this issue 4 years ago • 2 comments

The trio.Event() is currently only a synchronization point. If some data has to be passed from the setter to the waiter, it has to be done outside.

In my project, I have done some encapsulation to pass a boolean along with the event.

But I have the feeling the trio.Event() itself could be enhanced to pass any Python object from setter to waiter, in a simple way. Here is what should be added in trio.Event() to pass user data:

    def __init__(self):
        self._user_data = None

    async def wait(self):
         # current wait() processing
         return self._user_data

    def set(self, user_data=None) -> None:
        self._user_data = user_data
        # current set() processing

The cost is one additional member in event. It would be fully backward compatible.

mic006 avatar Aug 19 '20 15:08 mic006

hello, can channels help you achieve this?

kontsaki avatar Dec 23 '20 02:12 kontsaki

You can write a combination event-plus-value object easily enough. There are multiple implementations of ValueEvent out there.

The problems are elsewhere: what do you do when the task that is supposed to eventually set the event gets cancelled instead? what if it raises an exception? do you need the value to be readable by multiple waiters or must it not be? You can build any and all of these with the building blocks Trio provides but the usage conditions are divergent enough that so far nobody has come up with an object that's universally useful enough to get included. Thus Trio doesn't provide any.

smurfix avatar Dec 23 '20 09:12 smurfix