doumu4916 2017-06-17 06:33
浏览 51

切片并行

I want to make it run parallel based on number of thread. But the result was not as i expected. I dont know how to make it efficient and fast.

I ended up with this code.

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "runtime"
    "strconv"
    "strings"
    "sync"
    "time"
)

func main() {
    start := time.Now()
    target := os.Args[1]
    thread, _ := strconv.Atoi(os.Args[3])
    file, err := ioutil.ReadFile(os.Args[2])
    if err != nil {
        fmt.Println("Error: Please double check if the file " + os.Args[2] + " is exist!")
        os.Exit(0)
    }
    wordlist := strings.Split(string(file), "
")

    var wg sync.WaitGroup
    runtime.GOMAXPROCS(runtime.NumCPU())
    jobs := make(chan string)
    for i := 0; i < thread; i++ {
        wg.Add(1)
        defer wg.Done()
        for _, word := range wordlist {
            go func(word string) {
                jobs <- word
            }(word)
        }
    }

    go func() {
        for job := range jobs {
            code := visit(target + job)
            fmt.Println(target + job + " - " + strconv.Itoa(code))
        }
    }()
    wg.Wait()

    elapsed := time.Since(start)
    fmt.Printf("Timer: %s
", elapsed)
}

func visit(url string) int {
    data, err := http.Get(url)
    if err != nil {
        panic(err)
    }
    return data.StatusCode
}

Any help would be appreciated. Thank you.

Update This is my current results :

$ go run test.go http://localhost/ word.txt 2
http://localhost/1 - 404
http://localhost/1 - 404
http://localhost/7 - 404
http://localhost/8 - 404
http://localhost/9 - 404
http://localhost/0 - 404
http://localhost/ - 200
http://localhost/3 - 404
http://localhost/2 - 404
http://localhost/4 - 404
http://localhost/6 - 404
http://localhost/2 - 404
http://localhost/3 - 404
http://localhost/4 - 404
http://localhost/5 - 404
http://localhost/9 - 404
http://localhost/7 - 404
http://localhost/8 - 404
http://localhost/0 - 404
http://localhost/5 - 404
http://localhost/ - 200
http://localhost/6 - 404
  • 写回答

2条回答 默认 最新

  • dtf54486 2017-06-17 06:46
    关注

    You are not using the waitgroup correctly. The defer's in main's for loop are never called since main never returns and as a result, wg.Wait() call never unblocks.

    Put defer calls in the goroutine sending the message:

    // ...
    for i := 0; i < thread; i++ {
        wg.Add(1)
    
        for _, word := range wordlist {
            go func(word string) {
                defer wg.Done()
                jobs <- word
            }(word)
        }
    }
    // ...
    

    Also close the jobs channel:

    // ...
    wg.Wait()
    close(jobs)
    // ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘