haifeng / gop

Easy way to process jobs in parallel in golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

License:MIT License


Languages

Language:Go 100.0%