by valenterry · 3y ago · 0 comments
Agree with the grandparent. Async/await is unnecessary complexity in all but the most performance-oriented languages (like Rust).
Very curious to see how Java Loom will pan out in practice.
It is about doing things in parallel. There are many ways on how to achieve that, I am just nit picking on performance.
The vast majority of projects don't use CGo, and of those that do, the majority doesn't call CGo in a tight loop, making the performance impact mostly irrelevant.
So it's really coming back to the performance-orientedness of a language.
.NET is the opposite - it was designed from the get-go from cross-language and cross-runtime interop. Back when Java folk still had to struggle with JNI to use anything native, .NET 1.0 had P/Invoke and COM Interop. And now that it has async/await, it's all mapped down to a C-compatible ABI for interop purposes, so you can easily write async code in C# that calls into async code written in C++ that calls back into C# etc.
assert P;
foo(); // blocking call
assert Q;
and assert P;
await foo(); // await on an async call
assert Q;
?There may be some (e.g. thread identity is preserved in the former but possibly not in the latter, depending on implementation, and that could be expressed with proper assertions), but they only highlight how the differences are merely technical and certainly not "inherent".
The question is rather: is it possible to remove the await and just automatically rewrite every program to use await where necessary?
And the answer is no, because with only sync functions you have the guarantee that if your thread crashes, every code immediately stops executing. Not so with async functions. Once they are run there is a chance that your own thread crashes but some other thread keeps executing more code, at least for some time, potentially executing side effects.
It cannot be automatically decided if this is okay or not. Or to turn it around: the behaviour of a program that automatically awaits async functions (which formerly where sync) can be different.
There's obviously a difference between launching an operation to run concurrently with the spawning code and launching an operation and awaiting its completion, but that difference does not require the introduction of an async/await feature, and is perfectly expressible with nothing but threads and synchronous code. In particular, there is no need for two separate syntactic domains between async and sync subroutines. Indeed, in Erlang, Go, and Java, all the same behaviour of async/await is achieved with no such need, and no need for different kinds of APIs.
Of course there is. The former constraints the type (or return-type) of the function it is used in, the latter does not.
> but that difference does not require the introduction of an async/await feature
I don't remember saying anything about async/await in my original post though.
Wrong. Languages that definitely got it right:
Erlang/Elixir (high level), especially elixir's Task.async
Zig (low level)
Very close runner-up: Go
Task.async/1 (or Task.async/3) at a high level just spawns another BEAM process links it to the currently running process, sets up a process monitor, and then runs. Task.await/2 just pulls from the mailbox of the current process for a returned message.
There is a semantic difference between just running a line of code or running ith with Task.async right? So then there is a distinct concept of sync vs async.
vs-threading should be part of the .NET standard library but somehow isn't.
I knew Go can do it, so it doesn't "always fail"
What if you are reading the file and somewhere else in the code there is some logging happening. Should the logging flow stop until reading the file is done? Should it not? If not, what should happen if the "thing" (e.g. thread) that executes the code to read the file crashes?
Go does not treat sync and async the same. It specifically has syntax to deal with async, e.g. go routines.