pub fn block_on<F>(fut: F) -> Result<<F as Future>::Output, AsyncError>Expand description
Blocks on the provided future and returns the result.
This function is used to call async functions from sync code.
Due to limitations of tokio runtime, we cannot use tokio::runtime::Runtime::block_on
if we are already inside a tokio runtime. This function is a workaround for that limitation.
Handles three scenarios:
- No active runtime: creates a temporary current-thread runtime and drives the future directly.
- Current-thread runtime: spawns a dedicated OS thread with its own independent runtime,
since
block_in_placepanics when there are no other worker threads. - Any other runtime flavor (multi-thread, etc.): uses
block_in_place+ spawn for efficient bridging.