I am a student and have some experience in languages that use an async/await approach for concurrency, and not really practiced that as extensively as Go's model.
What i have gathered from online resources is that an "async" function, can be called with the "await" keyword, to actually wait for the async function to complete. But isn't this basically a single threaded program as you have to wait for the async function to complete?
What is the async/await equivalent to Channels? How do you communicate between two concurrent functions?
Can anyone explain this to me, or guide me to some resources that can help me to understand this?
node.js - async-await vs goroutines + channel, which is better performence for handling many WebSocket connection? - Stack Overflow
python 3.x - Goroutines vs asyncio tasks + thread pool for CPU-bound calls - Stack Overflow
C# Task async/await vs Golang's goroutines
go - C# Task async/await vs Golang's goroutines - Stack Overflow
Videos
I think I know part of the answer. I tried to summarize my understanding of the differences, in order of importance, between asyncio tasks and goroutines:
1) Unlike under asyncio, one rarely needs to worry that their goroutine will block for too long. OTOH, memory sharing across goroutines is akin to memory sharing across threads rather than asyncio tasks since goroutine execution order guarantees are much weaker (even if the hardware has only a single core).
asyncio will only switch context on explicit await, yield and certain event loop methods, while Go runtime may switch on far more subtle triggers (such as certain function calls). So asyncio is perfectly cooperative, while goroutines are only mostly cooperative (and the roadmap suggests they will become even less cooperative over time).
A really tight loop (such as with numeric computation) could still block Go runtime (well, the thread it's running on). If it happens, it's going to have less of an impact than in python - unless it occurs in mutliple threads.
2) Goroutines are have off-the-shelf support for parallel computation, which would require a more sophisticated approach under asyncio.
Go runtime can run threads in parallel (if multiple cores are available), and so it's somewhat similar to running multiple asyncio event loops in a thread pool under a GIL-less python runtime, with a language-aware load balancer in front.
3) Go runtime will automatically handle blocking syscalls in a separate thread; this needs to be done explicitly under asyncio (e.g., using run_in_executor).
That said, in terms of memory cost, goroutines are very much like asyncio tasks rather than threads.
I suppose you could think of it working that way underneath, sure. It's not really accurate, but, close enough.
But there is a big difference: in Go you can write straight line code, and all the I/O blocking is handled for you automatically. You can call Read, then Write, then Read, in simple straight line code. With Python asyncio, as I understand it, you need to queue up a function to handle the reads, rather than just calling Read.
I have been learning both C# and Golang for a while and trying to compare the ways they support asynchronous programming.
What I know about goroutines is that the go runtime can schedule the goroutines to run on different physical/machine threads. If a goroutine is blocked (e.g., synchronous waiting for I/O), the go runtime can suspend the goroutine and free the physical threads to run other goroutines.
C# Task is similar to goroutine in the sense that it is also an abstraction on top of physical threads. However, it is considered a bad idea to do blocking I/O (e.g., do .Result) inside an async Task because "the entire thread will be blocked which might cause deadlocks (e.g., GUI app)". Can't the C# runtime do something similar to goroutines to suspend the blocked async Task and free the physical threads to run other async Tasks?
I have been struggling for this problem for a while and couldn't find public materials explaining this better. Maybe my understanding is not correct. Can someone please help me?
Hey guys,
I have been working on this simple async library. Request you to check it out. And provide your valuable feedback. You are welcome to join the project.
https://github.com/maniartech/async
Best Regards