I need to run a function that takes non send value but the function has to run in a non blocking way. Result of the function is not important(if there is a way to gather it's result it's a plus).
#[tokio::main]
async fn main() {
let non_send_value = tokio::spawn(task_that_returns_non_send_value()).await;
// this has to be nonblocking and result is not that important
function_that_uses_non_send_value(non_send_value);
}
You may ask why you use tokio main macro or why did you spawn first task with tokio spawn because I'm using a workflow that is similar to this and I can not modify it probably. This is a minimum example. I have limited permission to change things.
I think you mean the Future of the async function is non-Send, this is usually related, but the same as the arguments of the function being non-Send.
in order to spawn a task that is non-Send in tokio, you need to use LocalSet:
#[tokio::main]
async fn main() {
let local = LocalSet::new();
// Spawn a non-Send future on the local set.
// alternative is use `block_on()`
let handle = local.spawn_local(async_fn_that_is_non_send());
// continue doing other tasks
sleep(Duration::from_millis(100)).await;
// need `run_until()` to drive the localset, since it is not scheduled on the worker thread
// or `await` the local set directly
local.await;
// can `await` the spawned task's result later
let value = handle.await;
}