M
M
Maxim Vlasov2018-10-14 04:12:02
go
Maxim Vlasov, 2018-10-14 04:12:02

How to control the number of goroutines in Go?

Good day! You need to make some kind of web spider on it. The mechanism is simple in theory:
There is a certain number of links. Each link runs on its own "thread". When links of a certain type are found, it should be added to the general queue. The number of streams is limited by settings. When the work on the page is completed, the thread is closed and a new thread from the queue should take its place. I have no idea how to implement this. What is the mechanism for controlling the total number of threads?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Olohtonov, 2018-10-14
@tr0yka

The classic idea (still from C) is to do it with the help of channels, this is how it should be arranged:
you start a channel that will play the role of a semaphore (mutex with a counter), write as many bytes there as you want to run the maximum number of coroutines, before starting each from them you subtract one byte from the channel, at the end you write it there. In this simple way, you get an upper limit on the number of simultaneously running coroutines.
Here is a sample code:
From this book, I recommend reading it, by the way:
5bc32f687decc787834192.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question