- Distributed Computing with Go
- V.N. Nikhil Anurag
- 257字
- 2021-06-24 18:36:12
Goroutines aren't predictable
In this chapter, we started by looking at how Go enables us to write code that is concurrent and, to an extent, parallel. Then we followed up with a discussion on how Go schedules goroutines over machines and processors. It is possible that we might be able to reason how the goroutines are going to be distributed over machines and processors, which in turn might let us write non-standard or hacky Go code.
Consider the code from the Parallelism section, where we tried to simulate listening to music while writing a few emails. Here is the output of the code for quick reference:
Done writing mail #3. 19:32:57 Listening... 19:32:57 Listening... 19:32:57 Done writing mail #1. 19:32:57 Listening... 19:32:57 Listening... 19:32:57 Done writing mail #2. 19:32:57
We can now easily infer that there were at least two Ps, and one of them was being used by the goroutine printing Listening..., while the other P was handling the goroutines related to writing emails.
This is all well and good, however consider the case where GOMAXPROCS is set to 1 or the system has low hardware capabilities which might result in fewer machines. It is possible that this might lead to the goroutine printing Listening... run forever and never giving control to the other goroutines. In reality, the Go compiler should detect this case and accordingly plan the scheduling of goroutines. However, it would be better to plan our code so that we do not have to rely on Go's scheduler and its current implementation.
- pcDuino開發實戰
- Windows Server 2012 Hyper-V:Deploying the Hyper-V Enterprise Server Virtualization Platform
- 阿里云數字新基建系列:云原生操作系統Kubernetes
- vSphere Virtual Machine Management
- 精通Linux內核開發
- 開源安全運維平臺OSSIM疑難解析:入門篇
- Windows Phone應用程序開發
- Docker+Kubernetes應用開發與快速上云
- Linux自動化運維:Shell與Ansible(微課版)
- Mobile First Design with HTML5 and CSS3
- 深入淺出Node.js
- Windows 8實戰從入門到精通(超值版)
- 應急指揮信息系統設計
- 完美應用Ubuntu(第2版)
- Linux內核修煉之道