3 ms·
To me, async is just "cooperative multitasking" with a quick paintjob It is, and not only to you. It is a way to save a call stack until a runloop calls it bac
by wruza 3y ago
To me, async is just "cooperative multitasking" with a quick paintjob
It is, and not only to you. It is a way to save a call stack until a runloop calls it back.
But what I can’t agree with is parallels with OS. Coop MT is only problematic in OS MT. When it’s your code there’s no unknown bad actor, and having multiple cooperative (mostly waiting) processes without scheduling them on a thread pool is a useful concept regardless of threads availability.
E.g. when you have to wait on multiple sources, the options you have are:
- serialize
- perform few non-blocking calls and wait for any/all of them to complete
- schedule them as tasks on a thread pool and wait for their completion
Async can do all three, it’s orthogonal. I’d say that just awaiting on PMT task completion is much more convenient that setting up locking primitives. Same for NB polling. Promise is just an abstraction and all it does is waiting for an event to fire on a current thread’s runloop while retaining the comfort of a lexical scope, all with a couple of keywords.
- hayley-patton 3y agoBut I'm my own worst enemy, and blocking the event loop is unpleasant even when I do it myself. Once I had to pass compute-intensive tasks (hashing some data, which took long enough to matter) to a thread pool, to not hurt latency for other tasks in the event loop. > I’d say that just awaiting on PMT task completion is much more convenient that setting up locking primitives. Don't use the primitives then - write e.g. a parbegin/parallel-map atop thread primitives, and use that.
- amelius 3y agoAsync is great for dealing with I/O but it forgets that the CPU is a resource too.
- Spivak 3y agoWhich is why you queue up CPU intensive tasks to threads where you can time slice, and IO intensive tasks to async/fibers whatever you wanna call them. It's hard to make this completely seamless. Power to Java for getting the closest.