When you enter the world of Go Language, you must be attracted by the powerful Concurrency, which can be used to drop tasks into the background with the simplest keyword go, but how to control Concurrency efficiently is a must-learning for Go language. This article introduces three ways to get acquainted with Concurrency, which correspond to three different terms: WaitGroup, Channel, and Context
.
WaitGroup
Suppose you have two machines that need to upload the latest code at the same time, and the last restart step can be executed only after the two machines have finished uploading separately. It’s like splitting a job into several parts and doing it at the same time to reduce the time, but then you need to wait until all the jobs are done before you can execute the next step, so you need to use the WaitGroup function to do that. Here’s a simple example.
|
|
Channel
Another practical case is that we need to actively notify a Goroutine to stop. For example, when the app is started, it will run some monitors in the background, and when the whole app needs to stop, we need to send a Notification to the monitors in the background to stop it first, and then we need to use Channel to notify them. Here’s an example:
|
|
The above example shows that the background is controlled by a Goutine and a Channel. As you can imagine, when there are many Goroutines in the background, we need to declare multiple Channels to control them. Maybe there will be another Goroutine inside the Goroutine, and the developer will find that it is no longer possible to control multiple Goroutines by simply using a Channel. The solution is to use context
.
Context
You can imagine that today there is a background task A, and task A generates task B, and task B generates task C. That is, we can follow this pattern all the way through. Suppose we need to stop task A in the middle, and A has to tell B and C to stop together, then context is the fastest way.
|
|
You can think of context as a controller that can control an indeterminate number of goroutines at any time, and from top to bottom, you can stop the whole background service by cancel()
at any point in time after declaring `context. This side case will be used when the App needs to be restarted, all goroutines will be notified to stop first, and the App will be restarted only after it stops normally.
Conclusion
Choose different functions according to different contexts and situations, here is a summary
- WaitGroup: If you need to split a single Job into multiple sub-tasks and wait until all of them are completed before proceeding to the next step, this is the most suitable time to use WaitGroup.
- Channel+select: Channel can only be used in the case of a single Goroutine. If you want to manage multiple Goroutines, it is recommended to use context.
- Context: If you want to control all the Goroutines at once, I believe context is the most suitable, and it is also the most used by Go. Of course, there are other features of context, please refer to the official documentation for details.