4 ms·
There are two different questions here: 1. Should the code be async-aware? That is, should it be able to yield to other tasks? 2. Should the code launch backg
by Nullabillity 3y ago
There are two different questions here:
1. Should the code be async-aware? That is, should it be able to yield to other tasks?
2. Should the code launch background tasks?
1 is a cross-cutting concern, in a cooperative multitasking environment, anything that is unable to yield is, well, preventing other tasks from running.
In Go, 1 is always true, and implemented by the runtime itself. In Rust, this means using async I/O, like preferring tokio over std.
2 is what the advice is about. The Rust equivalent here would be tokio::spawn.
Writing async-independent Rust code requires you to tackle 1, maybe by writing a trait for all the I/O actions that you require from your executor, which could return blocking futures if you select to run synchronously.
That said, blocking also prevents you from using async for structured concurrency in the library implementation, which may or may not be a big deal for your use-case.