parallel

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2020 License: MIT Imports: 3 Imported by: 0

README

parallel

Concurrency Made Easy

Go Report Card   Test Coverage Issue Count Code Climate GoDoc
parallel executes independent tasks in parallel by using Golang fanout fanin concurrency pattern

Installation

go get -u github.com/JigneshSatam/parallel

Usage

Example code snippet: https://play.golang.org/p/uHBw49pwFwt

Step 1: Import the package
import (
	"github.com/JigneshSatam/parallel"
)
Step 2: Create Execute() interface{} method for user defined type
  • Let employee be a user-defined type
  • Suppose fetching employee details is an independent task and needs to be  done be in parallel
  • Create a method Execute() for employee type
  • Signature of Execute method Execute() interface{}
  • Add the code to fetch the employee details in this Execute() method
// employee -> Let `employee` be a user-defined type
type employee struct {
	name string
}

// Execute() -> Create `Execute() interface{}` method for employee type
func (e employee) Execute() interface{} {
	return employeeDetails(e)
}

func employeeDetails(e employee) string {
	return "Employee details Name: " + e.name
}
Step 3: Call parallel.Run() method
  • Call parallel.Run by passing list of employees who's details to be fetched.
func Example() {
	employees := []employee{employee{"foo"}, employee{"bar"}, employee{"baz"}}

	// Call `parallel.Run()` to start parallel execution
	outputChannel := parallel.Run(employees)

	for op := range outputChannel {
		// Cast `interface{}` to desired output type
		output := op.(string)
		fmt.Println(output)
	}
}

// Unordered output:
// "Employee details Name: bar"
// "Employee details Name: foo"
// "Employee details Name: baz"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

Overview

Package parallel executes independent tasks in parallel by using Golang fanout fanin concurrency pattern.

Concurrency Made Easy

Example
package main

import (
	"fmt"

	"github.com/JigneshSatam/parallel"
)

// customType -> Let `customType` be a user-defined type
type customType int

// Execute -> Create `Execute() interface{}` method for customType
func (c customType) Execute() interface{} {
	return c * c
}

func main() {
	tasks := []customType{1, 2, 3, 4, 5}

	// Call `parallel.Run()` to start parallel execution
	outputChannel := parallel.Run(tasks)

	for op := range outputChannel {
		// Cast interface{} to desired output type
		output := op.(customType)
		fmt.Println(output)
	}

}
Output:

1
4
9
16
25

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run added in v0.2.0

func Run(tasks ...interface{}) <-chan interface{}

Run starts the parallel execution of the tasks provided.

It casts user-defined type tasks into `Executor interface` to builds executors.

To cast the task in an `Executor interface` the user-defined type `task` needs to have method with `Execute() interface{}` signature.

Types

type Executor

type Executor interface {
	Execute() interface{}
}

Executor is an interface which has to have execute function which will be implicitly invoked to execute the concurrent tasks.

Jump to

Keyboard shortcuts

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