pool

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: MIT Imports: 3 Imported by: 0

README

gopool

Go equivalent-ish of Python's multiprocessing.Pool.

Installation

go get github.com/DarkCeptor44/go-pool

Example

import (
    "fmt"

    "github.com/DarkCeptor44/go-pool"
)

func multiply(current int, total int, value pool.Value) pool.Result {
 n := value.Int() * 2

 return pool.Result{
  Old: value,
  New: pool.Value{Val: n},
 }
}

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

 // makes pool with 10 workers, the task function and the slice of ints
 p := pool.NewPool(10, multiply, pool.FromSlice[int](arr))
 results := p.Run()

 for _, result := range results {
    fmt.Printf("Original: %d, Multiplied: %d\n", result.Old.Int(), result.New.Int())
 }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(numWorkers int, task Task, values []Value) *Pool

Returns a new Pool struct

func (*Pool) Run

func (p *Pool) Run() []Result

Executes the task on the pool with amount of workers specified and returns a list of results

type Result

type Result struct {
	Old Value
	New Value
}

type Task

type Task func(index int, total int, value Value) Result

Pool task has to be this format

type Value

type Value struct {
	Val interface{}
}

func FromSlice

func FromSlice[K any](slice []K) []Value

Turns a generic slice into []Value

func (*Value) Float

func (v *Value) Float() float64

Turns Value into float64

func (*Value) Int

func (v *Value) Int() int

Turns Value into int

func (*Value) String

func (v *Value) String() string

Turns Value into string

Jump to

Keyboard shortcuts

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