3 ms·
This is essentially what I assumed and what I believe what ralusek assumes to be the case. This does not change our question. What would be the syntax for ho
by jayd16 7y ago
This is essentially what I assumed and what I believe what
ralusek assumes to be the case. This does not change our question.
What would be the syntax for how you would spawn a future, add it to the current Executor, cooperatively yield execution in the parent such that progress could be made on the child, but also return execution to the parent if the child yields but does not complete?
In C# I believe you could simply call
Task.Run(Action);
This will schedule the action to the current executor. If you yield execution through await that task will (have a chance to) run. The crux of the question is that the fact that you do not need to await that specific task, you just need to cooperatively yield execution such that the executor is freed up.
var shortTask = Task.Run(Task.Delay(100).Wait);
var longTask = Task.Run(Task.Delay(500).Wait);
await Task.Delay(1000);
await shortTask;
await longTask;
In C# this will take ~1000 milliseconds as the child futures yield execution back to the parent such that it can start its own yielding task.
- roblabla 7y agoWith async-std, you can just do let future = async_std::task::spawn(timer_future()); And then do more work. The timer_future will run cooperatively, and the future returned by spawn is merely a “join handle”. But this is a feature of the executor, not something that’s part of the core rust async/await. With tokio, you’d have to spawn a separate future and use channels to get the return value.
- iknowstuff 7y agoIt's the same. :) use std::time::Duration; use async_std::task; let shortTask = task::spawn(task::sleep(Duration::from_secs(1))); let longTask = task::spawn(task::sleep(Duration::from_secs(2))); task::sleep(Duration::from_secs(5)).await; shortTask.await; longTask.await; or simply: futures.join!( task::sleep(Duration::from_secs(1), task::sleep(Duration::from_secs(2), task::sleep(Duration::from_secs(5) ).await;