“线程池”对于Go语言是否相关?

huangapple go评论86阅读模式
英文:

Is 'thread pooling' relevant for Go?

问题

由于Go协程在内存需求和设置/销毁成本方面通常非常低,所以是否有必要实现一个线程(Go协程)工作池?在什么情况下您会考虑使用线程池而不是为每个请求“生成”一个Go协程?

英文:

Since it is generally very little overhead in terms of memory requirement and setup/tear down cost of a Go routine. Is it relevant even to implement a thread(go routine) worker pool? When would you consider using a thread pool instead of 'spawning' a go routine per request?

答案1

得分: 2

在Go语言中,生成和保持大量的goroutine是廉价的,但并非免费。

此外,你应该记住,goroutine本身可能很廉价,但在goroutine代码内部可能会分配大量内存。因此,你可能希望限制并发运行的goroutine数量。

你可以使用信号量来限制资源。另一种更符合Go语言习惯的方法是使用带有工作池的执行管道。这种模式在golang博客中有很好的描述。

英文:

Spawning and keeping lots of goroutines in golang is cheap but it's not free.

Also you should remember that goroutine themselves may be very cheap, but at the same time a lot of memory can be allocated inside of goroutine code. So you may want to limit number of concurrently running goroutines.

You may use semaphore to limit resources.
Another approach (more idiomatic for go) is to use executions pipelines with worker pools. This pattern is very well described in golang blog.

答案2

得分: 1

是的,这是相关的。db/sql 使用连接池来管理与数据库的连接,因为建立新连接需要时间。

英文:

Yes, it's relevant. db/sql uses pool of connections to database, because of establishing new connection take time.

huangapple
  • 本文由 发表于 2017年2月5日 09:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/42047678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定