1
A
回答
1
A channel提供了一个可用于协调的并发同步机制。您可以使用一个与time.Ticker
协调的定期分派给定数量的函数调用。
// A PeriodicResource is a channel that is rebuffered periodically.
type PeriodicResource <-chan bool
// The NewPeriodicResourcePool provides a buffered channel that is filled after the
// given duration. The size of the channel is given as count. This provides
// a way of limiting an function to count times per duration.
func NewPeriodicResource(count int, reset time.Duration) PeriodicResource {
ticker := time.NewTicker(reset)
c := make(chan bool, count)
go func() {
for {
// Await the periodic timer
<-ticker.C
// Fill the buffer
for i := len(c); i < count; i++ {
c <- true
}
}
}()
return c
}
单程序例程会等待每个ticker事件并尝试将缓冲通道填充到最大容量。如果消费者没有消耗缓冲区,则任何连续的滴答只会补充它。您可以使用该频道同步执行最多ñ次/ 持续时间。例如,我可能想每秒钟拨打doSomething()
不超过五次。
r := NewPeriodicResource(5, time.Second)
for {
// Attempt to deque from the PeriodicResource
<-r
// Each call is synchronously drawing from the periodic resource
doSomething()
}
当然,相同的信道可被用于每第二调用go doSomething()
这将风扇出至多过程。
相关问题
- 1. Glassfish:限制用户每秒/每分钟的请求数量
- 2. 限制用户,使每秒
- 3. 如何计算在JSP中ping主机的次数?
- 4. 如何衡量“每秒查询次数”?
- 5. 如何限制每秒卷曲数php
- 6. Azure函数 - Ping限制?
- 7. 如何限制使用Sungrid的主机上的作业数量?
- 8. 从bash脚本ping未知主机ping
- 9. 限制主机上已退出容器的数量(1.8.3)
- 10. 在无限循环中每60秒重置一次变量值
- 11. Ejabberd发送和接收的数据包数量限制为61每秒
- 12. mininet:动态创建的主机不能ping其他主机,但主机可以ping交换机
- 13. Ping每秒钟和Tkinter按钮
- 14. c#限制每次迭代
- 15. 不能ping Hadoop主机名
- 16. 如何在GET/PHP5中限制每秒连接服务器的数量?
- 17. 这里API请求每秒限制
- 18. PHP - 抗洪 - 如何限制每秒
- 19. 编码/设计一个通用的线程安全的限制(即限制X()的每秒Ÿ多次执行)
- 20. 每隔n秒复制一次
- 21. AudioUnit每30秒录制一次毛刺
- 22. CPU的限制,每天一次
- 23. Ansible:来自远程主机的Ping
- 24. ssh配置主机限制
- 25. 每次/每天随机数
- 26. 脚本来ping从ping一台单一的主机名
- 27. 每秒增加数量?
- 28. Ansible包含每个主机的变量
- 29. 如何限制mod_proxy_connect的主机?
- 30. Tkinter标签+1每秒的Incerasing数量
请发布一些代码示例 –
请参阅:我如何在Golang中每秒执行多次命令:http://stackoverflow.com/questions/39385883/how-do-i-execute-commands-many-many -times每秒合golang – 2016-10-01 07:35:47