英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论