Skip to content

joy717/poolasync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card

pool-async

pool-async is a tool for goroutines with a pool.

Features

  • limit goroutine numbers for avoid to use out of resources like memory, fd etc.
  • can return first error when using DoWithError() then Wait() will return the first non-nil error
  • you can get all errors if you want. just call GetErrors()

Quick start

package main

import "github.com/joy717/poolasync"
import "fmt"

func main() {
  pa := poolasync.NewDefaultPoolAsync()
  pa.DoWitError(func() error {
    fmt.Println("goroutine 1")
    return nil
  }).DoWitError(func() error {
    fmt.Println("goroutine 2")
    return fmt.Errorf("goroutine 2 err")
  })
  
  // NOTICE: should call pa.Wait() always to complete the jobs.
  // because we use channel, Wait() is the reciver function.
  if err := pa.Wait(); err != nil {
    fmt.Println("the 1st non-nil err: ", err)
  }
}

If you like this tool, star it and share it. Thx.

Note

You can choose Golang offical repo errgroup.Group now. errgroup repo add limit for concurrency since 2022. https://cs.opensource.google/go/x/sync/+/0976fa681c295de5355f7a4d968b56cb9da8a76b

but you can still choose poolasync. poolasync can get all errors, the errgroup can only get the first non-nil error.

About

poolasync is a tool for goroutines with a pool.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages