2016-11-18 57 views
0

我正在與我的城市的公交車頻率的API工作,但當我嘗試使HTTP獲取到許多網址時,我有點卡在線程上。 如果沒有併發性,程序需要16分鐘才能完成1500個URL調用來獲取HTTP狀態代碼,並且我試圖使用併發性,但在閱讀了很多帖子之後,我不明白goroutines是如何工作的...多線程去HTTP獲取

的想法是讓一個功能和變更請求的數量,喜歡這裏:

go getBusPostStatus(600, 800) 

但我是在完全stucked ...

下面是代碼:

package main 

import (
"fmt" 
"net/http" 
"strconv" 
"time" 
) 
var i int = 0 
var convStr string 
var message = make(chan string) 

/*func main(){ 
    for i = 0; i < 1500; i++ { 
     z = strconv.Itoa(i) 
     url := "http://www.urbanosdezaragoza.es/frm_esquemaparadatime.php?poste=" + z 
     resp, err := http.Get(url) 
     if err != nil { 
      fmt.Println("Houston, we've got problems") 
     }else{ 
      if resp.StatusCode == 200{ 
       fmt.Println("OK: El poste "+z+" existe") 
      }else{ 
       fmt.Println("WARN: El poste "+z+" NO existe") 
      } 
     } 
    } 
}*/ 

//Return 2 houndred posts 
func returnTH(c chan string){ 
for i = 0; i < 200; i++ { 
    convStr = strconv.Itoa(i) 
    url := "http://www.urbanosdezaragoza.es/frm_esquemaparadatime.php?poste=" + convStr 
    resp, err := http.Get(url) 
    if err != nil { 
     fmt.Println("Houston, we've got problems") 
    }else{ 
     if resp.StatusCode == 200{ 
      //fmt.Println("OK: El poste "+z+" existe") 
      c <- "OK: The bus post "+convStr+" exists" 
     }else{ 
      //fmt.Println("WARN: El poste "+z+" NO existe") 
      c <- "WARN: The bus post "+convStr+" does not exist" 
     } 
    } 
    } 
} 
func returnFH(z chan string){ 
    for i = 201; i < 400; i++ { 
    convStr = strconv.Itoa(i) 
    url := "http://www.urbanosdezaragoza.es/frm_esquemaparadatime.php?poste=" + convStr 
    resp, err := http.Get(url) 
    if err != nil { 
     fmt.Println("Houston, we've got problems") 
    }else{ 
     if resp.StatusCode == 200{ 
      //fmt.Println("OK: El poste "+z+" existe") 
      z <- "OK: The bus post "+convStr+" exists" 
     }else{ 
      //fmt.Println("WARN: El poste "+z+" NO existe") 
      z <- "WARN: The bus post "+convStr+" does not exist" 
     } 
    } 
    } 
} 

func threadPrint(c, z chan string){ 
    for { 
     threadOne := <- c 
     threadTwo := <- z 
     fmt.Println(threadOne) 
     fmt.Println(threadTwo) 
    } 
} 
func main(){ 
    start := time.Now() 
    var c chan string = make(chan string) 
    var z chan string = make(chan string) 
    //for i = 0; i < 1500; i++{ 
    go returnTH(c) 
    go returnFH(z) 
    go threadPrint(c,z) 
    /*go getBusPostStatus(400, 600) 
    go getBusPostStatus(600, 800) 
    go getBusPostStatus(800, 1000) 
    go getBusPostStatus(1000, 1200) 
    go getBusPostStatus(1200, 1400) 
    go getBusPostStatus(1400, 1500)*/ 
    //} 
    timeExec:= time.Since(start) 
    fmt.Println("Time to exec code = ", timeExec) 

    /*var input string 
    fmt.Scanln(&input) 
    fmt.Println("done")*/ 
} 

非常感謝提前!

+0

什麼是'getBusPostStatus'?你堅持什麼? – JimB

+0

這是一個「想法」,每200次調用一次線程就可以創建一個線程,但我不知道如何在線程中創建線程,這是沒有線程的「舊系統」。我被困在使用線程創建1500個URL的方式上,因爲第一個被評論的函數,即old/* main * /,需要16分鐘才能完成1500個響應......非常感謝您的幫助! – Brokes

+2

Go沒有線程,但是如果你的意思是goroutines,那麼你已經通過用'go'關鍵字調度函數來使用它們了。 – JimB

回答

0

以下是一個簡單的示例代碼,它使用goroutine和channel來同時請求100次並打印結果。希望這段代碼有幫助。

package main 

import (
    "fmt" 
    "math/rand" 
    "time" 
) 

func main() { 
    rep := 100 
    results := make(chan string) 

    // Use goroutine to send multiple time-consuming jobs to the channel. 
    for i := 0; i < rep; i++ { 
     go func(num int) { 
      results <- mockHTTPRequest(num) 
     }(i) 
    } 

    // Receive results from the channel and use them. 
    for i := 0; i < rep; i++ { 
     fmt.Println(<-results) 
    } 
} 

func mockHTTPRequest(num int) string { 
    timeDelay := rand.Intn(5000) 
    time.Sleep(time.Duration(timeDelay) * time.Millisecond) 
    if timeDelay%2 == 0 { 
     return fmt.Sprintf("OK: The bus post %v exists", num) 
    } 
    return fmt.Sprintf("WARN: The bus post %v does not exist", num) 
} 

您可以在https://play.golang.org/p/RR34roRIl4上運行此代碼。

+0

感謝您的幫助!那麼,這個想法是:你打電話給一個網址,例如:http://www.urbanosdezaragoza.es/frm_esquemaparadatime.php?poste=2,最後一個數字,在這種情況下,「2」是數字在這種情況下,HTTP將返回一個HTTP - > 200,因爲這個帖子存在...問題是一個接一個,緊密地從1500到1500的1500個電話...我不知道HOW讓Goorine一次打出3或4個電話,使用Go的「線程」,從0到1500 ...非常感謝您的幫助,bcoz我從昨天開始就完全陷入了僵局...... – Brokes