Spawning async methods – Am I doing it right?

Okay, then I will not feel bad using it :slight_smile:

I do remember your posts here. :woozy_face: For now, simply requiring Unpin works fine, except it's a bit verbose.

Yeah thanks. The next subsection also explains how to make the methods non-threadsafe by using #[async_trait(?Send)]. I had to actually use this because I need my async method to hold an RwLockReadGuard and that is !Send. However, I ran into the problems that you already pointed out:

The only way out was to use tokio::task::spawn_local, which – in turn – required me to create a tokio::task::LocalSet in my program's main function.

So async works, but I lost multithreading support.

Is it somehow possible to hold an RwLockReadGuard or MutexGuard in an async function or method that gets spawn'ed? :thinking: … Nevermind, while writing this, I just found the answer: Yes. Tokio provides several Send'able synchronization locks/guards in its tokio::sync module. :smiley: That should allow me to make all futures Send again despite using locks/guards. Will try that tomorrow, but wanted to share this, in case any other people have problems with locks and async functions.

tokio::sync::MutexGuard
A handle to a held Mutex. The guard can be held across any .await point as it is Send.

Thanks for helping and encouraging me to keep on!