What can you pass to an async spawn?

I'm trying to figure out what can be passed to an "async" task.


says "If a single piece of data must be accessible from more than one task concurrently, then it must be shared using synchronization primitives such as Arc" Just like regular threads. Rc isn't Send, although Arc is.


"Tasks spawned by tokio::spawn must implement Send"

This makes sense if you have a multi-threaded async system, because there is real parallelism.

My use case is that I have a huge, interlinked set of data structures with lots of Rc links. It's a client for a virtual world, and the state of all the visible objects is in memory. Updates come in from a queue fed by UDP messages, and some updates require additional HTTP requests to fetch more data. I want to run those HTTP requests in parallel. But I only want one thread at a time updating the world state.

I don't want to have to put Struct tokio::sync::Mutex all over. I'd get deadlocks from locking locks in the wrong order. What I was looking for is less restrictive single-thread async, where Rc can be passed to an async task and there's no need to lock. Is that available?

If you are using Rc, then those values can only be created and accessed by one thread. What I suggest is have a thread in charge of updating the world state, and send it messages through a channel. Or use Arc ( or both ).

If you set up your Tokio runtime with a LocalSet, then you can spawn non-send tasks.

Please note that even if you were ok with using a mutex, you still would not be able to put an Rc inside it.


Probably the way to go. Async doesn't help here.

I belive you are looking for spawn_local in tokio::task - Rust

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.