Skip to content

haifeng/gop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gop

Easy way to process jobs in parallel in golang

  • goroutine
  • return results
  • support timeout

Example usage:

package main

import (
  "fmt"
  "strings"
  "time"

  "github.com/haifeng/gop"
)

type MyWorker struct {
  str string
}

func (mw *MyWorker) Perform() interface{} {
  start := time.Now()
  time.Sleep(2 * time.Second)
  r := strings.ToUpper(mw.str)
  fmt.Printf("Last %s\n", time.Now().Sub(start))
  return r
}

func main() {
  workers := []gop.Worker{
    &MyWorker{"first"},
    &MyWorker{"second"},
    &MyWorker{"third"},
  }

  start := time.Now()

  g := gop.New(5 * time.Second)

  results := g.Run(workers)
  for _, result := range results {
    r := result.(string)
    fmt.Println(r)
  }

  fmt.Printf("Done, got %d results\n", len(results))
  fmt.Printf("Total: %s\n", time.Now().Sub(start))
}

More examples.

About

Easy way to process jobs in parallel in golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages