-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Ability to advance the Runtime or LocalSet until all tasks are Pending? #2443
Comments
This is probably not the best way to go around that. Using Tokio in a game can be a decent way to handle network connections, but it should probably go on a different thread than the main game loop. |
There is currently no way to tick the runtime until all tasks are pending. Assuming you are using I/O, one problem is, how do you wake up your game loop when an I/O event has been received? |
I would find this feature very useful for simplified testing. I've spent considerable time trying alternative strategies to know when things are "done" but they've all proven unsatisfactory one way or another. With this you could:
Conventionally, for testing without real delays you would mock out all this async stuff and interrogate each system synchronously but I feel like there's a real opportunity here for testing the async system as-is. Being able to wait for an idle runtime is a blunt tool but I keep coming back to it as the easiest way to reach this particular nirvana. If this is not an appropriate thing for the real tokio Runtime (for good reasons I'm doubtless ignorant of), is there a way we could build such a thing ourselves out of the tools tokio gives us? |
@thombles @EkardNT Switch to smol. It includes two functions https://docs.rs/smol/1.2.4/smol/struct.LocalExecutor.html#method.try_tick https://docs.rs/smol/1.2.4/smol/struct.LocalExecutor.html#method.tick |
There's no need to switch executor just to use |
I don't think that
This suggests that It's also worth noting Tokio's Lines 459 to 477 in e309da0
To advance the Note that there is a limit on the number of tasks that the Line 217 in e309da0
Therefore, if you want to ensure that all tasks on the |
Tokio not being able to integrate with external event loops is a real show stopper :( One thing that would already help me a lot would be a |
Seconding this. Having a method like |
Also need this functionality to be able to embed Tokio into an external event loop. |
By using The only limitation with This has been useful for me when I want to run/test an async task one tick at a time and synchronized with the main thread. Feel free to review and suggest how the tokio runtime can be added to this |
That's awesome. Likewise I made something similar here. I am using an adapted single threaded futures executor from the You cannot use |
I am currently working on a patch set that will provide this functionality. |
I would like to use tokio in a game I am making to implement coroutines that can execute across multiple game ticks. The way I would like this to work is that each tick of the main game loop, I would tell tokio's runtime to process all woken tasks until all futures have returned Pending, aka there is no more work to do at the present time. Then I would proceed with the rest of the game's update logic for that loop iteration. On the next loop iteration, the game would advance tokio's runtime again in the same manner. This would keep happening over and over until the program exits.
This might only make sense to do with a LocalSet, given that the full Runtime could potentially have multiple threads. There are also possibilities for more advanced control knobs around the basic all_pending stop condition, such as advancing until
all_pending || num_tasks_processed > some_configurable_threshold
, or having a timeout, etc. These could be useful in the context of a game where you want to tightly control the runtime of a single frame.One possibility I've considered for achieving this using tokio's existing feature set is to call
LocalSet::run_until
with a future that simply awaits a yield_now call before completing.The idea is that the yield_now() call would put the future at the back of the queue, allowing any other pending tasks to execute first. The problem with this approach is that it only does one pass through tokio's list of tasks, which may not be enough. For example, if one of the tasks spawns another one, I'm not sure whether this solution would cause that new task to be polled during the current game tick.
The text was updated successfully, but these errors were encountered: