Golang laden Sie mehrere Dateien parallel mit goroutines
Ist es möglich, zum herunterladen und speichern von Dateien in parallel mit goroutines?
Unten ist mein code, mit dem herunterladen von Dateien aus meiner dropbox:
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
)
const app_key string = "<app_key>"
const app_secret string = "<app_secret>"
var code string
type TokenResponse struct {
AccessToken string `json:"access_token"`
}
type File struct {
Path string
}
type FileListResponse struct {
FileList []File `json:"contents"`
}
func download_file(file File, token TokenResponse) {
download_file := fmt.Sprintf("https://api-content.dropbox.com/1/files/dropbox/%s?access_token=%s", file.Path, token.AccessToken)
resp, _ := http.Get(download_file)
defer resp.Body.Close()
filename := filepath.Base(file.Path)
out, err := os.Create(filename)
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, resp.Body)
}
func main() {
authorize_url := fmt.Sprintf("https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=%s", app_key)
//Get code
fmt.Printf("1. Go to: %s\n", authorize_url)
fmt.Println("2. Click 'Allow' (you might have to log in first)")
fmt.Println("3. Copy the authorization code.")
fmt.Printf("Enter the authorization code here: ")
fmt.Scanf("%s", &code)
//End get code
//Get access token
data := url.Values{}
data.Add("code", code)
data.Add("grant_type", "authorization_code")
data.Add("client_id", app_key)
data.Add("client_secret", app_secret)
resp, _ := http.PostForm("https://api.dropbox.com/1/oauth2/token", data)
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
var tr TokenResponse
json.Unmarshal(contents, &tr)
//End get access token
//Get file list
file_list_url := fmt.Sprintf("https://api.dropbox.com/1/metadata/dropbox/Camera Uploads?access_token=%s", tr.AccessToken)
resp2, _ := http.Get(file_list_url)
defer resp2.Body.Close()
contents2, _ := ioutil.ReadAll(resp2.Body)
var flr FileListResponse
json.Unmarshal(contents2, &flr)
//End get file list
for i, file := range flr.FileList {
download_file(file, tr)
if i >= 2 {
break
}
}
}
Es funktioniert nicht, wenn ich das Präfix der download_file Funktion mit der go
Befehl.
go download_file(file, tr)
Du musst angemeldet sein, um einen Kommentar abzugeben.
Weil deine Haupt-goroutine ist beendet. Benötigen Sie ein WaitGroup zu warten, bis alle die goroutines beenden. Zum Beispiel,
download_file
(vorwg.Done()
? Wird diewg.Done()
nennen noch passieren?wg.Done()
wenn Sie fertig sind. Wenn man das nicht nennen, es wird nicht aufgerufen werden. Wenn Sie sicherstellen möchten, es heißt sogar, wenn Sie aus der Funktion zurückzugeben früh, verwenden Siedefer
wie würden Sie für andere Arten von "catch" - Verhalten.Könnte dieses Projekt helfen, Sie und andere, die suchen, in der Verwirklichung die Parallelität Gehen:
Idee ist, erstellen Sie einen cluster, Gehen Sie zum ausführen von parallelen jobs. https://github.com/waqar-alamgir/mini-go-cluster
Quelle können optimiert werden, um die download-Dateien gleichzeitig.