Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync/async interoperable channels

When you wish to send a sequence of things across threads (in a thread-blocking way), you have e.g. crossbeam_channel. When you wish to send a sequence of things across futures (in a non-thread-blocking, future-blocking way), you have e.g. tokio::sync::mpsc.

What would be something that enables me to send from a blocking thread, and receive from an asynchronous context ? (Btw, I could imagine needing the opposite at some point.)

I need the channel to be bounded, thread-blocking when sending and future-blocking when receiving.

I am looking for something somewhat performant, like an equivalent of what is done in crossbeam_channel, but waking up the future instead of the thread, with the ability to buffer some messages to avoid blocking as much as possible. The answer given here for the multiple messages scenario looks a bit like a patch-up to that regard.

like image 812
Ten Avatar asked Aug 11 '20 17:08

Ten


1 Answers

The channels provided by Tokio have gained the feature to do this since this question was asked. You can simply call the blocking_send and blocking_recv methods on the channel when you are in non-async code:

let (mut tx, mut rx) = tokio::sync::mpsc::channel(10);

std::thread::spawn(move || {
    // send a value, blocking synchronously
    // this allows async channels to be used in non-async contexts
    tx.blocking_send("testing").unwrap();
});

// receive a value, blocking asynchronously
assert_eq!(rx.recv().await.unwrap(), "testing");
like image 75
Alice Ryhl Avatar answered Oct 15 '22 17:10

Alice Ryhl