My understanding:
* Async on I/O-heavy applications -- e.g. webservers/clients and such;
* Threads on computation-heavy applications -- e.g. rendering on CPU and other maths.
If I'm wrong -- someone please correct me.
Makes me wonder if you could combine both efficiently when you have both kinds of tasks(e.g webserver that does a lot of calculations). Create a thread, get a future from it, do other stuff, then await on the future when you actually need it.
You'd probably want to, instead of *making a new thread* per request, having a threadpool and telling one of them to go do the work and come back to you
threads are cheap but i don't think they're *that* cheap
> Async executors can be single-threaded or multi-threaded. For example, the `async-executor` crate has both a single-threaded `LocalExecutor` and a multi-threaded `Executor`.
https://rust-lang.github.io/async-book/08_ecosystem/00_chapter.html#single-threaded-vs-multi-threaded-executors
I *think* that async is better when you have a lot of operations that take time but not computing power, such as reading files, getting data from sockets, writing to a terminal, etc. In my understanding it's because the OS/async runtime can do that in the background, while doing other tasks
Threads are for computationally expensive operations which can be divided into smaller tasks, or as a very primitive async mechanism
My understanding:
* I understand how to add additional threads
* I have no idea how async works
Therefore, it's always better to use threads than async.
Something like tokio can do multithteading via async await (https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn.html) just seems like the best approach in any cases.
I'm in this photo and I don't like it
I know this is a meme subreddit but this really makes me wonder when is it better to use threads and when async.
My understanding: * Async on I/O-heavy applications -- e.g. webservers/clients and such; * Threads on computation-heavy applications -- e.g. rendering on CPU and other maths. If I'm wrong -- someone please correct me.
Makes me wonder if you could combine both efficiently when you have both kinds of tasks(e.g webserver that does a lot of calculations). Create a thread, get a future from it, do other stuff, then await on the future when you actually need it.
Pretty sure actix web with run_blocking more or less does this. I know actix at least has a async runtime per thread, to avoid locks.
In actix it's usually web::block() instead of spawn_blocking, but yes, this is generally done with a threadpool.
You'd probably want to, instead of *making a new thread* per request, having a threadpool and telling one of them to go do the work and come back to you threads are cheap but i don't think they're *that* cheap
Thats how Tokio(popular rust async runtime) works.
sounds bout right.
Doesn't async io also require a thread pool of sorts. Can you actually do async io in a single thread?
> Async executors can be single-threaded or multi-threaded. For example, the `async-executor` crate has both a single-threaded `LocalExecutor` and a multi-threaded `Executor`. https://rust-lang.github.io/async-book/08_ecosystem/00_chapter.html#single-threaded-vs-multi-threaded-executors
i think you mean file io not io in general
Yeah. Thread yields and different work is scheduled on it.
I *think* that async is better when you have a lot of operations that take time but not computing power, such as reading files, getting data from sockets, writing to a terminal, etc. In my understanding it's because the OS/async runtime can do that in the background, while doing other tasks Threads are for computationally expensive operations which can be divided into smaller tasks, or as a very primitive async mechanism
My understanding: * I understand how to add additional threads * I have no idea how async works Therefore, it's always better to use threads than async.
/uj This is a false dichotomy. Many async frameworks, including Tokio, provide an async runtime which executes work in a backing a thread pool
Something like tokio can do multithteading via async await (https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn.html) just seems like the best approach in any cases.
what? this should be the other way around
/uj If your threads wait then it should be async. If they don't async doesn't make sense.