Skip to main content

block_on

Function block_on 

Source
pub fn block_on<F>(fut: F) -> Result<<F as Future>::Output, AsyncError>
where F: Future + Send + 'static, <F as Future>::Output: Send,
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_place panics when there are no other worker threads.
  • Any other runtime flavor (multi-thread, etc.): uses block_in_place + spawn for efficient bridging.