4 ms·
Thank you, so what is the syntax that is used in order to execute without awaiting? Do you create a thread for each?
by ralusek 7y ago
Thank you, so what is the syntax that is used in order to execute without awaiting? Do you create a thread for each?
- roblabla 7y agoThis is where things get tricky: Rust didn't standardize an event loop - it only standardized common traits that allow implementing one, and a way to communicate whether a computation needs more time or already has a result. If what you want to do is run multiple CPU-bound computation and have a central event loop awaiting the result, then yes, you'll need to spawn threads and use some kind of channel to transfer the state and result. If what you want is to run multiple IO-bound queries, then you'll want to use the facilities of the event loop of your choice (tokio, async-std, etc...) to register the intent that you're waiting for more data on a file-descriptor. The "proper" way to execute without awaiting it on the current future is usually to spawn another future on the event loop. The syntax to do that with tokio is use tokio; let my_future = some_future(); tokio::spawn(my_future);
- Rusky 7y agoYou do not create a thread for each- that would defeat most of the purpose of futures. Instead you call `Future::poll`, which runs a future until it blocks again, and provide it a way to signal when it is ready. That signal would be handed off to an event loop (which tracks things executing on other hardware like network or disk controllers) or another part of the program (which will be scheduled eventually).