-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
async
yield
function
#3359
Comments
Doesn't the usual Rust async yield work? struct Yield {
yielded: Option<bool>,
}
impl Yield {
fn new() -> Self {
Self {
yielded: Some(false),
}
}
}
impl Future for Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let yielded = self.yielded.unwrap();
if yielded {
self.yielded = None;
Poll::Ready(())
} else {
self.yielded = Some(true);
cx.waker().wake_by_ref();
Poll::Pending
}
}
} Also it seems that the future you are spawning here is really blocking. Instead of putting a timer here I believe you should just yield in-between every call to |
@daxpedda unfortunately that yield doesn't work at all for me. It compiles, but my async task seems to get stuck. |
Ah, apologies, my Will play around with it myself and see if it works. |
Alright, this doesn't seem to work at all. I'm guessing the |
So after some testing basically my findings are that you can yield, but not how you want it. Basically you yield to the executor. So you can multiplex multiple futures by yielding, but you can't yield back to the "native" runtime because it's not part of the executor. Obviously web workers would be the perfect solution here, unfortunately the ecosystem around that is not exactly in a good state. I can't come up with a better solution then yours otherwise. |
Thanks @daxpedda, that at least clears things up a little bit for me! |
I actually found two viable solutions for this in the meantime:
|
I discovered even more in the meantine:
Both could do what you want and much more, really interesting developments. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I have a task where I need to decode a bunch of messages from a stream. Every 10ms I want to yield to my UI task (running on
requestAnimationFrame
). My code is something like this:I spawn this with
wasm_bindgen_futures::spawn_local
The problem is the
yield_
function. I've currently defined it as so:this feels very hacky, for obvious reasons.
Any suggestions for how to improve this?
The text was updated successfully, but these errors were encountered: