Skip to content

esoytekin/executor

Repository files navigation

Parallel Task Executor for GO CI status

Executes tasks in parallel with MemoizableFuture support.

Install

$ go get -u github.com/esoytekin/executor

Introduction

Executes tasks in parallel with MemoizableFuture support. tasks should implement executor.Task interface

type Task[V any] interface {
	// Exec godoc
	Exec() (V, error)

	// Hash godoc
	// is used for memoization
	// generate it using input parameters
	Hash(hashStr func(string) int) []int
}

executor.NewFutureTask

accepts executor.Task as parameter and returns new futureTask

package main


type TaskImpl struct {
	input int
}

func (t *TaskImpl) Exec() ( int, error ) {
	time.Sleep(3 * time.Second)
	return t.input * t.input, nil
}

func (t *TaskImpl) Hash(f func(string) int) []int {
	return []int{t.input}
}

func main(){

    taskItem := &TaskImpl{1}
    ft := executor.NewFutureTask[int](taskItem)
    result,_ := ft.Get() // blocks execute operation
    fmt.Println("result", result)
}

Usage Example

package main

import (
	"fmt"
	"testing"
	"time"

	"gotest.tools/assert"
    	"github.com/esoytekin/executor"
)

type TaskImpl struct {
	input int
}

func (t *TaskImpl) Exec() ( int, error ) {
	time.Sleep(3 * time.Second)
	return t.input * t.input
}

func (t *TaskImpl) Hash(f func(string) int) []int {
	return []int{t.input}
}

func TestExecutor(t *testing.T) {

	expected := []int{1, 4, 4, 9, 16, 25, 36, 49, 64, 81, 4}

	e := executor.NewTaskExecutor[int](100)

	e.Progress(func(p int) {
		fmt.Println("progress", p)
	})

	results, _:= e.ExecuteTask(&TaskImpl{1}, &TaskImpl{2}, &TaskImpl{2}, &TaskImpl{3}, &TaskImpl{4}, &TaskImpl{5}, &TaskImpl{6}, &TaskImpl{7}, &TaskImpl{8}, &TaskImpl{9}, &TaskImpl{2})

	assert.Equal(t, len(expected), len(results))

	for x := range results {
		assert.Equal(t, expected[x], results[x])
	}

}

About

Parallel task executor for GO

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages