Thread is a natural OS object it’s have enough. Threads manipulations are expensive operations. They require switch to kernel return back, save and restore stack and so on. Many servers used threads but it’s unreal to keep a lot of threads and do not go out of resources. Also there’s a special task to synchronize them.
So new concept emerged - coroutine or coprogram. They could be imagined as parts of execution path between synchronization points: input-output, send-receive so on. They are very light and could be better orchestrated
So “threads but better”.
Answer from Eugene Lisitsky on Stack OverflowDescribed in short my Python program has 290 Threads, in every Thread a while Loop sends a request for 20kb of data every 3 seconds. If the data changed in the last 3 seconds (If-Modified-Since is not always accurate unfortunatly) the Thread sends a request for a 550kb response. This response gets analyzed, that means basic arithmetics, accessing a large dict and sending another request to localhost for <1kb. This is very heavy on my CPU, so I thought of migrating to Golang.
Do You think Golang is the right choice? I'm a bit concerned, because time is important and the Golang Garbage Collector could waste some. I have absolutley no experience with Golang and university level C++(so mainly theoretical). Would Java be a better fit?
python 3.x - Goroutines vs asyncio tasks + thread pool for CPU-bound calls - Stack Overflow
Asyncio Coroutines Faster Than Threads!?
Should I migrate my Multithreading Python program to Golang or other languages
Asyncio vs threading
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.