Skip to main content

block

Function block 

Source
pub async fn block<F, T>(f: F) -> Result<T>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,
Expand description

Run a blocking operation without stalling the async runtime.

Calling a blocking API directly from a handler occupies a runtime worker for its duration; enough concurrent calls and the server stops answering anything. This moves the work to tokio’s blocking pool, which is what that pool is for.

Reach for it around synchronous file I/O, a blocking database driver, or CPU-heavy work such as password hashing.

let sum = churust_core::block(|| (1..=1000).sum::<u64>()).await.unwrap();
assert_eq!(sum, 500_500);

A panic inside f becomes a 500 rather than taking down the worker, matching how a panicking handler is already treated.