Quantcast
Channel: Why does my go code hang whilst using goroutine and channels? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Why does my go code hang whilst using goroutine and channels?

$
0
0

So I am wanting to make mutiple http request to an api endpoint I need to do give or take 15 thousand now I am wanting to make these requests as fast as possible but whilst also not running into any os issue as when I run it with just creating a new goroutine for each request I run into the socket:too many files open error. So after some research I came up with using a buffered channel to stack jobs that need to be ran and below is the code I got.

My issue is that it adds the 1000 jobs to be ran to the channel but does not actually run them it just hangs and does nothing until I terminate the program. Why is this?

package mainimport ("fmt""sync")func main() {    jobsChannel := make(chan string, 1000)    workers := 10    var wg sync.WaitGroup    usernameAsPointer := getCommandArgs()    username := *usernameAsPointer    usernameID := getID(username)    listOfAccounts := readFile()    fmt.Printf("Loaded %d accounts!\n", len(listOfAccounts))    // Fill up our jobs channel    for _, account := range listOfAccounts {        fmt.Println("Adding Jobs to the channel")        fmt.Println(account)        wg.Add(1)        jobsChannel <- account    }    // Create a worker pool so we dont just kill the system we're running this on    for i := 0; i < workers; i++ {        fmt.Println("We're running our jobs.")        go func() {            for accountInfo := range jobsChannel {                followUser(usernameID, accountInfo)                wg.Done()            }        }()    }    wg.Wait()}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images