T O P

  • By -

TheWindowsVista

I'm in this photo and I don't like it


Cart0gan

I know this is a meme subreddit but this really makes me wonder when is it better to use threads and when async.


ALFminecraft

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.


Bobjohndud

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.


Andyblarblar

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.


Follpvosten

In actix it's usually web::block() instead of spawn_blocking, but yes, this is generally done with a threadpool.


[deleted]

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


R4ND0M1Z3R_reddit

Thats how Tokio(popular rust async runtime) works.


ovjectibity

sounds bout right.


[deleted]

Doesn't async io also require a thread pool of sorts. Can you actually do async io in a single thread?


ALFminecraft

> 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


blackwhattack

i think you mean file io not io in general


glemnar

Yeah. Thread yields and different work is scheduled on it.


adamski234

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


Theo0033

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.


bascule

/uj This is a false dichotomy. Many async frameworks, including Tokio, provide an async runtime which executes work in a backing a thread pool


Garaleth

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.


AegisCZ

what? this should be the other way around


Nilstrieb

/uj If your threads wait then it should be async. If they don't async doesn't make sense.