-
Notifications
You must be signed in to change notification settings - Fork 6
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
variant of wait() that guarantees that predicate matches when it returns #19
Comments
It isn't possible to implement, based on the way coroutines and the Trio scheduler work. Given Note that the AsyncValue API always returns the value that triggered the predicate. Many use cases rely on this, so that they don't miss fast-moving values. E.g.: val = await foo.wait_value(lambda x: x > 10)
handle_high_foo(val) # guaranteed to be > 10 |
I'd argue that this is an uphill battle, because in general code tends to need async calls for something, and then your invariant is violated. If you can share more details on your use case, I may be able to suggest something. |
Hi @belm0!
From my reading of https://trio.readthedocs.io/en/stable/reference-core.html#checkpoints, I understand that the Trio scheduler only gets to reschedule tasks during checkpoints. Checkpoints happen in async functions that are built into Trio, e.g., So it's my understanding that in this:
we might get paused between "a" and "b", but Trio will never have the opportunity to interrupt us between printing "b" and "c". Please let me know if I'm misunderstanding how async/Trio works, I'm relatively new to async & Trio. :) |
That's correct. I see, so the wait function is checking the value, and either returning directly to the caller or going back to sleep. Along the lines of my other comment about code tending to need async calls, I guess in practice I've learned not to rely on that property. For example, events are often layered or aggregated, say by passing several wait_value() calls to The use case is a little odd: we're saying that as long as time is frozen, you'd want to act on the event. But if you advance time by even 1 ms (i.e. let another task run), the value may be changed, and then you don't want to act on it. At least in our application (soft real-time robot) I don't think that's a common pattern-- but I'd like to keep an eye out for it over some time, and think about the use case. |
Would there be interest in adding a variant of
wait
that only returns if the predicate is stillTrue
? I find that I sometimes need to write stuff like:Maybe something like:
The text was updated successfully, but these errors were encountered: