What is the best practise to communicate with a sync (blocking) function from async function?

I 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.

Use an async channel. Most async channel implementations have blocking send and receive operations, and if there isn't one, you can just block_on() the async channel send in your blocking code.

2 Likes