go - Go 通道发送/接收的延迟大?(PProf 追踪)
问题描述
我正在使用 PProf 分析与生产者和消费者的数据管道的跟踪。我不确定我应该如何阅读这个,但从调度程序延迟配置文件来看,chanrecv
并且chansend
占我总运行时间的相当一部分。
我觉得很难相信,所以这是否建议进行通道发送/接收需要那么长时间,或者这只是指落后的生产者会导致chanrecv
阻塞。以及如何使这更有效?
示例代码:
func consume(in chan User) {
out := make(chan UserDetail)
go func() {
for user := range in {
if userDetail, err := getUserDetails(user); err == nil {
out <- userDetail
}
}
close(out)
}()
return out
}
func produce() {
out := make(chan User)
go func() {
// could be issuing a paginated api call to get users list
for user := range users {
out <- user
}
close(out)
}()
return out
}
func main() {
userChan := produce() // get all users
userDetailChannels := []chan User
// fanout to 8 workers
for workerIndex := 0; workerIndex < 8; workerIndex++ {
userDetailChannels = append(userDetailChannels, consume(userChan))
}
}
解决方案
推荐阅读
- c# - 如何编组 LLVMSharp 函数
- ios - 将 Unity 项目与现有 Xcode swift 项目结合时的问题
- visual-studio - “/”应用程序中的服务器错误。连接数据库
- node.js - 如何检索 Azure 存储中的每 n 行?
- python - 根据掩码从3d数组中提取切片的pythonic方法
- ios - 在If语句中根据UIPickerView的选中行改变标签
- javascript - 始终显示数据值图表栏和饼图.js
- ios - 改变 UIImageView 的变换
- node.js - 带有 Node.js 和 Postgresql 的 Travis CI:不存在错误关系
- php - 复制mysql数据并隐藏在其他地方的最佳做法是什么