trio
trio copied to clipboard
Enhance trio.Event() to pass user data
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.
hello, can channels help you achieve this?
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.