parallel

package module
v0.0.0-...-ef08331 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2018 License: MIT Imports: 1 Imported by: 1

README

Parallel fn

  • Run functions in parallel.
  • Limit the number of goroutines running at the same time.

Installation

go get -u github.com/rafaeljesus/parallel-fn

Usage

Run

package main

import (
  	"errors"

  	"github.com/rafaeljesus/parallel-fn"
)

func main() {
	timeout := time.After(2 * time.Second)
        fn1 := func() error { return nil }
        fn2 := func() error { return errors.New("BOOM!") }

	for {
		select {
		case err := <-Run(fn1, fn2):
                	// catch errors
		case <-timeout:
      			// handle timeout
		}
	}
}

RunLimit

package main

import (
  	"errors"

  	"github.com/rafaeljesus/parallel-fn"
)

func main() {
	timeout := time.After(2 * time.Second)
  	fn1 := func() error { return nil }
  	fn2 := func() error { return errors.New("BOOM!") }
  	fn3 := func() error { nil }
  	fn4 := func() error { nil }

	for {
		select {
		case err := <-RunLimit(2, fn1, fn2, fn3, fn4):
      			// catch errors
		case <-timeout:
      			// handle timeout
		}
	}
}

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Badges

Build Status Go Report Card Go Doc


GitHub @rafaeljesus  ·  Medium @_jesus_rafael  ·  Twitter @_jesus_rafael

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(functions ...Func) chan error

Run calls the passed functions in a goroutine, returns a chan of errors.

func RunLimit

func RunLimit(concurrency int, functions ...Func) chan error

RunLimit calls the passed functions in a goroutine, limiting the number of goroutines running at the same time, returns a chan of errors.

Types

type Func

type Func func() error

Func is the function to run concurrently.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL