Executes tasks in parallel with MemoizableFuture
support.
$ go get -u github.com/esoytekin/executor
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
}
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)
}
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])
}
}