What is the best practise to communicate with a sync (blocking) function from async function?
⚓ Rust 📅 2025-10-05 👤 surdeus 👁️ 6I am using a FFI lib operating a hardware. And some of its structs are not even Send + Sync. So in my async program (tokio runtime), I use spawn_blocking to hold all its code and using channels to communicating with it from other async code.
Since the channels are used in sync code, they are the std::sync version. And in the async code, I do something like:
loop {
match rx.try_recv() {
Ok(result) => do_something(result).await,
Err(TryRecvError::Empty) => tokio::time::sleep(Duration::from_millis(1)).await,
e => e?,
}
}
Is there other better way to do this? Not a loop with interval.
2 posts - 2 participants
🏷️ Rust_feed