并发,但每个任务只执行一次。

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

Concurrency but only do each task once

问题

func main() {
	// 在这里使用 switch 语句调用 grabusernames()
}

func grabusernames() {
	f, err := os.OpenFile("longlist.txt", os.O_RDONLY, os.ModePerm)
	if err != nil {
		log.Fatalf("打开文件错误:%v", err)
		return
	}
	defer f.Close()

	rd := bufio.NewReader(f)
	for {
		line, err := rd.ReadString('\n')
		line2 := strings.TrimSpace(line)
		if err != nil {
			if err == io.EOF {
				break
			}

			log.Fatalf("读取文件行错误:%v", err)
			return
		}
		go tellonym(line2) // 并发执行 tellonym() 函数
	}
}

func tellonym(line2 string) {
	threads := 10

	swg := sizedwaitgroup.New(threads)
	for i := 0; i < 1; i++ { // 将循环次数改为 1
		swg.Add()
		go func(i int) {
			defer swg.Done()
			var client http.Client
			resp, err := client.Get("https://tellonym.me/" + line2)
			if err != nil {
				log.Fatal(err)
			}
			defer resp.Body.Close()
			//fmt.Println("Response code: ", resp.StatusCode)
			if resp.StatusCode == 404 {
				fmt.Println("用户名" + line2 + "未被使用")
			} else if resp.StatusCode == 200 {
				fmt.Println("用户名" + line2 + "已被使用")
			} else {
				fmt.Println("其他情况,响应代码:", resp.StatusCode)
			}

		}(i)
	}
}

上述代码的问题在于它会重复检查相同的用户名1,000次。我希望它只检查 longlist.txt 中的每个用户名一次,但同时进行并发操作(因为列表很长,我希望速度快)。

当前输出:

用户名 causenot 未被使用
用户名 causenot 未被使用
用户名 causenot 未被使用
用户名 causenot 未被使用

期望输出:

用户名 causenot 未被使用
用户名 billybob 已被使用
用户名 something 已被使用
用户名 stacker 已被使用
英文:
func main() {
	//switch statement here that runs grabusernames()
}
func grabusernames() {
	f, err := os.OpenFile(&quot;longlist.txt&quot;, os.O_RDONLY, os.ModePerm)
	if err != nil {
		log.Fatalf(&quot;open file error: %v&quot;, err)
		return
	}
	defer f.Close()

	rd := bufio.NewReader(f)
	for {
		line, err := rd.ReadString(&#39;\n&#39;)
		line2 := strings.TrimSpace(line)
		if err != nil {
			if err == io.EOF {
				break
			}

			log.Fatalf(&quot;read file line error: %v&quot;, err)
			return
		}
		tellonym(line2)
	}
}
func tellonym(line2 string) {
	threads := 10

	swg := sizedwaitgroup.New(threads)
	for i := 0; i &lt; 1000; i++ {
		swg.Add()
		go func(i int) {
			defer swg.Done()
			var client http.Client
			resp, err := client.Get(&quot;https://tellonym.me/&quot; + line2)
			if err != nil {
				log.Fatal(err)
			}
			defer resp.Body.Close()
			//fmt.Println(&quot;Response code: &quot;, resp.StatusCode)
			if resp.StatusCode == 404 {
				fmt.Println(&quot;Username&quot; + line2 + &quot;not taken&quot;)
			} else if resp.StatusCode == 200 {
				fmt.Println(&quot;username &quot; + line2 + &quot; taken&quot;)
			} else {
				fmt.Println(&quot;Something else, response code: &quot;, resp.StatusCode)
			}

		}(i)
	}

The issue with the code above is that it checks the same username 1,000 times
I'd like it to check each username in the longlist.txt once, but I want to concurrently do it ( it's a long list and I'd like it to be fast

Current output:

Username causenot taken
Username causenot taken
Username causenot taken
Username causenot taken

Desired output:

Username causenot taken
Username billybob taken
Username something taken
Username stacker taken

答案1

得分: 2

你需要在**tellonym(line2)**函数中使用goroutines。在你的for循环中,你使用了相同的用户名1,000次。

func main() {
    // 在这里加入switch语句来运行grabusernames()
}
func grabusernames() {
    f, err := os.OpenFile("longlist.txt", os.O_RDONLY, os.ModePerm)
    if err != nil {
        log.Fatalf("打开文件错误:%v", err)
        return
    }
    defer f.Close()

    rd := bufio.NewReader(f)
    for {
        line, err := rd.ReadString('\n')
        line2 := strings.TrimSpace(line)
        if err != nil {
            if err == io.EOF {
                break
            }

            log.Fatalf("读取文件行错误:%v", err)
            return
        }
        go tellonym(line2) // 在这里使用goroutines
    }
}

另外,请注意以下细节:

如果你从io.Reader中读取数据,请将其视为从流中读取。它是单一的输入源,由于其特性,你无法“并行读取”它——在底层,你获取一个字节,等待下一个字节,再获取一个字节,依此类推。将其分词成单词是在缓冲区中进行的。

其次,我希望你不要将goroutines视为“万能解决方案”,以“添加goroutines后一切都会加速”的方式使用。如果Go语言提供了如此简单的并发方式,并不意味着你应该在任何地方都使用它。

最后,如果你确实需要将大型文件并行分割成单词,并且认为分割部分将成为瓶颈(不了解你的情况,但我真的怀疑)——那么你必须发明自己的算法,并使用“os”包来Seek()/Read()文件的各个部分,每个部分由自己的goroutine处理,并以某种方式跟踪哪些部分已经被处理过。

英文:

You have to use goroutines in tellonym(line2) function. In your for loop you are using same username with 1,000 times.

func main() {
    //switch statement here that runs grabusernames()
}
func grabusernames() {
    f, err := os.OpenFile(&quot;longlist.txt&quot;, os.O_RDONLY, os.ModePerm)
    if err != nil {
        log.Fatalf(&quot;open file error: %v&quot;, err)
        return
    }
    defer f.Close()

    rd := bufio.NewReader(f)
    for {
        line, err := rd.ReadString(&#39;\n&#39;)
        line2 := strings.TrimSpace(line)
        if err != nil {
            if err == io.EOF {
                break
            }

            log.Fatalf(&quot;read file line error: %v&quot;, err)
            return
        }
        go tellonym(line2) // use go routines in here
    }
}



Also take care about this details:

if you're reading from io.Reader consider it as reading from the stream. It's the single input source, which you can't 'read in parallel' because of it's nature - under the hood, you're getting byte, waiting for another one, getting one more and so on. Tokenizing it in words comes later, in buffer.

Second, I hope you're not trying to use goroutines as a 'silver bullet' in a 'let's add gouroutines and everything will just speed up' manner. If Go gives you such an easy way to use concurrency, it doesn't mean you should use it everywhere.

And finally, if you really need to split huge file into words in parallel and you think that splitting part will be the bottleneck (don't know your case, but I really doubt that) - then you have to invent your own algorithm and use 'os' package to Seek()/Read() parts of the file, each processed by it's own gouroutine and track somehow which parts were already processed.

答案2

得分: 0

请尝试这个代码:

func grabusernames() {
f, err := os.OpenFile("longlist.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatalf("打开文件错误:%v", err)
return
}
defer f.Close()
rd := bufio.NewReader(f)
ch := make(chan struct{}, 10)
var sem sync.WaitGroup
for {
line, err := rd.ReadString('\n')
line2 := strings.TrimSpace(line)
if err != nil {
if err == io.EOF {
break
}
log.Fatalf("读取文件行错误:%v", err)
return
}
ch <- struct{}{}
sem.Add(1)
go tellonym(line2, ch, &sem)
}
sem.Wait()
}
func tellonym(line2 string, ch chan struct{}, sem *sync.WaitGroup) {
defer func() {
sem.Done()
<-ch
}()
var client http.Client
resp, err := client.Get("https://tellonym.me/" + line2)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
//fmt.Println("Response code: ", resp.StatusCode)
if resp.StatusCode == 404 {
fmt.Println("用户名" + line2 + "未被使用")
} else if resp.StatusCode == 200 {
fmt.Println("用户名" + line2 + "已被使用")
} else {
fmt.Println("其他情况,响应代码:", resp.StatusCode)
}
}
英文:

Try this

func grabusernames() {
f, err := os.OpenFile(&quot;longlist.txt&quot;, os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatalf(&quot;open file error: %v&quot;, err)
return
}
defer f.Close()
rd := bufio.NewReader(f)
ch := make(chan struct{}, 10)
var sem sync.WaitGroup
for {
line, err := rd.ReadString(&#39;\n&#39;)
line2 := strings.TrimSpace(line)
if err != nil {
if err == io.EOF {
break
}
log.Fatalf(&quot;read file line error: %v&quot;, err)
return
}
ch &lt;- struct{}{}
sem.Add(1)
go tellonym(line2, ch, &amp;sem)
}
sem.Wait()
}
func tellonym(line2 string, ch chan struct{}, sem *sync.WaitGroup) {
defer func() {
sem.Done()
&lt;-ch
}()
var client http.Client
resp, err := client.Get(&quot;https://tellonym.me/&quot; + line2)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
//fmt.Println(&quot;Response code: &quot;, resp.StatusCode)
if resp.StatusCode == 404 {
fmt.Println(&quot;Username&quot; + line2 + &quot;not taken&quot;)
} else if resp.StatusCode == 200 {
fmt.Println(&quot;username &quot; + line2 + &quot; taken&quot;)
} else {
fmt.Println(&quot;Something else, response code: &quot;, resp.StatusCode)
}
}
</details>

huangapple
  • 本文由 发表于 2022年3月9日 19:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/71409016.html
匿名

发表评论

匿名网友

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

确定