- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 golang 中有一个程序可以计算 SHA1 并打印以两个零开头的 SHA1。我想使用 goroutines 和 channel 。我的问题是,如果我不知道它会产生多少结果,我不知道如何优雅地退出 select 子句。
很多教程都提前知道了,计数器命中就退出。其他人建议使用 WaitGroups,但我不想这样做:我想在主线程出现在 channel 中时立即在主线程中打印结果。有人建议在 goroutines 完成时关闭 channel ,但我想在异步完成后关闭它,所以我不知道如何。
请帮助我实现我的要求:
package main
import (
"crypto/sha1"
"fmt"
"time"
"runtime"
"math/rand"
)
type Hash struct {
message string
hash [sha1.Size]byte
}
var counter int = 0
var max int = 100000
var channel = make(chan Hash)
var source = rand.NewSource(time.Now().UnixNano())
var generator = rand.New(source)
func main() {
nCPU := runtime.NumCPU()
runtime.GOMAXPROCS(nCPU)
fmt.Println("Number of CPUs: ", nCPU)
start := time.Now()
for i := 0 ; i < max ; i++ {
go func(j int) {
count(j)
}(i)
}
// close channel here? I can't because asynchronous producers work now
for {
select {
// how to stop receiving if there are no producers left?
case hash := <- channel:
fmt.Printf("Hash is %v\n ", hash)
}
}
fmt.Printf("Count of %v sha1 took %v\n", max, time.Since(start))
}
func count(i int) {
random := fmt.Sprintf("This is a test %v", generator.Int())
hash := sha1.Sum([]byte(random))
if (hash[0] == 0 && hash[1] == 0) {
channel <- Hash{random, hash}
}
}
最佳答案
首先:如果您不知道您的计算何时结束,您甚至如何建模?确保您确切知道您的程序何时以及在何种情况下终止。如果你完成了,你就会知道如何用代码编写它。
您基本上是在处理生产者-消费者问题。一个标准案例。我会建模这样 ( on play ):
func producer(max int, out chan<- Hash, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < max; i++ {
random := fmt.Sprintf("This is a test %v", rand.Int())
hash := sha1.Sum([]byte(random))
if hash[0] == 0 && hash[1] == 0 {
out <- Hash{random, hash}
}
}
close(out)
}
显然,您是在暴力破解哈希,因此当循环结束时就到达了终点。我们可以在这里关闭 channel 并向其他 goroutines 发出信号,告诉它们没有什么可以监听了。
func consumer(max int, in <-chan Hash, wg *sync.WaitGroup) {
defer wg.Done()
for {
hash, ok := <-in
if !ok {
break
}
fmt.Printf("Hash is %v\n ", hash)
}
}
消费者从 in
channel 获取所有传入消息并检查它是否已关闭(ok
)。如果它关闭,我们就完成了。否则打印接收到的哈希值。
为了开始这一切,我们可以这样写:
wg := &sync.WaitGroup{}
c := make(chan Hash)
wg.Add(1)
go producer(max, c, wg)
wg.Add(1)
go consumer(max, c, wg)
wg.Wait()
WaitGroup
的目的是等待生成的 goroutines 完成,由在 goroutines 中调用 wg.Done
。
另请注意,您使用的 Rand
对于并发访问而言并不安全。使用初始化的全局在 math/rand
中。示例:
rand.Seed(time.Now().UnixNano())
rand.Int()
关于go - 如何在golang中优雅地跳出select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21783333/
这个问题刚刚出现:如何打破 if 语句?我有一个很长的 if 语句,但有一种情况我可以尽早摆脱它。 在循环中我可以这样做: while (something ) { last if $some
我有一个 iframe,它显示带有可点击链接的 flash 横幅广告。无论如何强制链接在父窗口中打开,而不将 JS 放在打开或更改 flash 文件的页面上? 例如,我可以对 iFrame 做些什么来
JSHint 告诉我不要在 if 语句中使用标签(就 jshint 而言,标签似乎只用于循环)。 forminjection: if (options.addToForm !== false) {
我正在动态地将文本字段(最多 32 个)添加到我的页面中,我需要遍历所有这些文本字段并检查,如果它们全部为空,我将显示一条警告消息!我不想使用 document.getElementsByTagNam
我正在编写一个程序,其中我使用 if 语句来检查某些条件;如果为真,我会增加一个计数器。问题是,一旦语句为真,变量要么无限递增,要么随机递增。 如果条件满足但我一直在尝试使用一些子句来打破这个语句 我
有一个similar question关于 C++ 中的这个问题,但我在这里使用 JavaScript。我基本上和另一篇文章中的 OP 处于相同的情况。 var input = prompt(); w
我一直在研究 Issue 14在 PascalScript 脚本引擎上,使用 Goto 命令跳出 Case block 会产生编译器错误,即使这是完全有效(如果丑陋)的 Object Pascal 代
我正在使用 VBA 的 While...Wend 循环。 Dim count as Integer While True count=count+1 If count = 10 The
我在这个网站上看到过类似的问题,但我无法让它工作。我正在尝试通过 Javascript 检测输入框中的重复条目 - 但我希望在输入重复项时循环中断。我有那部分工作,但循环继续运行,它创建了一个用户无法
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个问题,我可能需要因为某些值(value)而跳出整个 promise 链,或者根据一个值(value)采取两条路径。 你怎么做最好? 这是我想跳出整个链条的第一个场景。我只想给他们留言。
因此,对于类作业,我必须接受用户输入并分配我的沙鼠对象每天可以使用的“食物”量。在这种情况下,我已经从用户那里获取了每天的最大食物量,如果用户尝试输入超过每日最大值的值,我需要向用户提供错误消息。 如
我正在尝试制作一个导航栏,它看起来像这样 https://imgur.com/a/wf7Pe 是有链接的那个,忽略白色的div。 问题是,如果调整页面大小,文本会超出 div,位于其下方。 像这样:
我正在尝试使用 C 退出 while 循环,但使用中断、返回或更改 while 条件不起作用。 基本上,我想知道为什么在最后一个 else if 中,当我声明 back = true 时,我的代码一直
我正在寻找关键字或干净的方式来不使用选择语句退出finally block 。考虑以下示例: private bool AtteptToDoSomething() { try {
我有一个 python 脚本,它将解析 xml 文件中的序列号并将它们写入文本文件。下面代码的问题是,它正在进行无限循环。如果我在记录到文件后的某个位置添加一条break语句,它只会写入一个序列号。如
我在类 LinkRepository 中有一个方法,我正在检查 vector 数组 Datalinks 中的重复条目,它是该类的一个成员。我遍历数组中的所有元素以检查新条目 Datalink* dat
是否可以在每个循环中跳出下划线……? _.each(obj, function(v,i){ if(i > 2){ break // <~ does not work } // so
我想优化依赖于 filter() 的函数。在某些情况下,我想在它们达到特定元素时打破它们。 (例如,我可能有一个包含不同元素的数组。或者,我只想实现一种 findFirst 功能。)在这种情况下,函数
我是一名优秀的程序员,十分优秀!