Junior — Senior
Simultaneous downloading of content from multiple web resources
livecode
Task condition
It is required to write a function ParallelDownload that will process incoming URLs in parallel, using a specified number of workers, and return a map with the download results for each site.
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
type SiteContent struct {
Title string
Date time.Time
Content string
}
// Function to simulate a long-running task, downloading site content
func DownloadSiteContent(ctx context.Context, url string) SiteContent {
// Generate a random wait time from 5 to 10 seconds
rand.Seed(time.Now().UnixNano())
sleepTime := time.Duration(rand.Intn(6)+5) * time.Second
// Wait for the random duration
time.Sleep(sleepTime)
// Return simulated site content
return SiteContent{
Title: "Site Title " + url,
Date: time.Now(),
Content: "Site Content " + url,
}
}
// Function for parallel downloading of site content
func ParallelDownload(ctx context.Context, urls <-chan string, numWorkers int) map[string]SiteContent {
// TODO
return nil
}
func main() {
urls := make(chan string)
// Launch function for parallel content download
go func() {
urls <- "http://example.com"
urls <- "http://example.org"
urls <- "http://example.net"
close(urls)
}()
// Launch parallel download with maximum number of workers
result := ParallelDownload(context.Background(), urls, 3)
// Output results
for url, content := range result {
fmt.Printf("Site: %s\nTitle: %s\nDate: %s\nContent: %s\n\n",
url, content.Title, content.Date, content.Content)
}
}