Skip to content
/ synctools Public

additions to the go sync package, pool to limit goroutines concurrency

Notifications You must be signed in to change notification settings

gwik/synctools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

synctools provides a Pool structure that limits the concurrency of goroutines.

Pool wraps a sync.WaitGroup and uses a channel to limit the number of goroutines to be run in parallel.

Example :

package main

import (
  "fmt"
  "github.com/gwik/synctools"
  "net/http"
  "time"
)

func main() {
  var urls = []string{
    "http://www.golang.org/",
    "http://www.google.com/",
    "http://www.somestupidname.com/",
    "http://github.com/",
    "http://bitbucket.org/",
    "http://sigkill.tumblr.com/",
  }

  pool := synctools.NewPool(3)
  for _, url := range urls {
    // avoid for loop cache, see http://golang.org/doc/effective_go.html#channels
    url := url
    pool.Spawn(func() {
      fmt.Println("fetch", url)
      // Fetch the URL.
      http.Get(url)
      time.Sleep(3 * time.Second)
    })
  }
  // Wait for all HTTP fetches to complete.
  pool.Wait()
}

GoDoc

About

additions to the go sync package, pool to limit goroutines concurrency

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages