Run a function with !send value and return immediately

Hi,

I need to run a function or future (I can make it both). The function takes non send value. I can not wait this function to finish or can't schedule it later. I have to return immediately.

tokio::spawn(async {
   if let Ok(non_send_value) = function_a().await {
      function_that_takes_non_send_value_and_takes_a_lot_of_time_to_finish(non_send_value)
   }
   
  return true; // need to return this immediately
})

Then you'll need some sort of shared queue or a channel.
You send the value via the sender in a non-blocking manner and the processing is done in a separate thread where the channel's receiver is polled.

Pseudo code:

std::thread::spawn(move || {
    loop {
        let value = receiver.recv();
        function_that_takes_non_send_value_and_takes_a_lot_of_time_to_finish(value);
    }
});

tokio::spawn(async {
   if let Ok(non_send_value) = function_a().await {
      sender.send(non_send_value);
   }
   
   return true; // need to return this immediately
})
2 Likes