Search code examples
rustrust-tokiosingle-threaded

Does Tokio have run_until_stalled equivalent?


I have a project where I have a main game loop. I also have an async runtime and run it on each frame until all of its futures are stalled. I do this by using futures::LocalPool::run_until_stalled. Now I am looking into using Tokio for the async runtime, because some dependencies require it. But I cannot find a similar functionality in Tokio, that is to manually drive the runtime instead of it doing its own thing.

Does anyone know if Tokio has this functionality with its single threaded runtime?


Solution

  • There isn't a direct analog.

    futures_executor is a very basic executor that does not handle I/O. Because of this, futures can only be "woken up" from events caused by other futures (or elsewhere in the code). In this environment, run_until_stalled makes sense - if all futures are inactive, they can't wake each other up, and they will never progress.

    However, Tokio handles I/O and other external events - an inactive future will become active if, for example an underlying socket receives more data or an OS signal is triggered. All tasks becoming inactive is not a stalled state, since more data may come in and activate one of them at any time. In this environment, run_until_stalled does not make much sense.

    For the context of a game loop, consider running the event loop on the main thread, the tokio runtime in its own thread, and using channels or other synchronization mechanisms to pass data back and forth.