Documentation ¶
Index ¶
- type ExecuteContext
- type Parallel
- func (p *Parallel[TARGET, RESULT]) Cancel()
- func (p *Parallel[TARGET, RESULT]) Execute(target TARGET)
- func (p *Parallel[TARGET, RESULT]) IsExecuting() bool
- func (p *Parallel[TARGET, RESULT]) ReadyExecute()
- func (p *Parallel[TARGET, RESULT]) SetMaxExecuteNum(num int)
- func (p *Parallel[TARGET, RESULT]) Wait()
- type Result
- type ResultContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExecuteContext ¶
ExecuteContext is the context provided to the executeHandler function. It contains the target to be processed, a shared value map, and a boolean indicating if the execution is running.
func (*ExecuteContext[TARGET, RESULT]) Cancel ¶
func (ctx *ExecuteContext[TARGET, RESULT]) Cancel()
Cancel cancels the execution.
func (*ExecuteContext[TARGET, RESULT]) SetValue ¶
func (ctx *ExecuteContext[TARGET, RESULT]) SetValue(key string, value any)
SetValue sets a shared value to the context.
func (*ExecuteContext[TARGET, RESULT]) Target ¶
func (ctx *ExecuteContext[TARGET, RESULT]) Target() TARGET
Target
type Parallel ¶
Parallel
func NewParallel ¶
func NewParallel[TARGET any, RESULT any](executeHandler func(ctx *ExecuteContext[TARGET, RESULT]) (*RESULT, error), resultHandler func(ctx *ResultContext[TARGET, RESULT])) *Parallel[TARGET, RESULT]
NewParallel creates a new parallel executor with custom executeHandler and resultHandler.
func (*Parallel[TARGET, RESULT]) Cancel ¶
func (p *Parallel[TARGET, RESULT]) Cancel()
Cancel cancels the execution. It switches the isExecuting flag to false.
func (*Parallel[TARGET, RESULT]) Execute ¶
func (p *Parallel[TARGET, RESULT]) Execute(target TARGET)
Execute adds a target to be executed by the parallel executor. If the execution is not running, the function will simply return without adding the target.
func (*Parallel[TARGET, RESULT]) IsExecuting ¶
IsExecuting returns a boolean indicating whether the executor is currently executing tasks. It uses the atomic Load method to safely check the isExecuting flag.
func (*Parallel[TARGET, RESULT]) ReadyExecute ¶
func (p *Parallel[TARGET, RESULT]) ReadyExecute()
ReadyExecute initializes the execution channels and starts the background goroutines. It will only be executed once. If it's called multiple times, the function will simply return after the first call.
func (*Parallel[TARGET, RESULT]) SetMaxExecuteNum ¶
SetMaxExecuteNum sets the maximum number of concurrent execute handlers that can run in parallel. If the provided number is less than 1, it defaults to the number of CPUs available on the system (runtime.NumCPU()). This function panics if the provided number is less than 0.
type ResultContext ¶
ResultContext is the context provided to the resultHandler function. It contains the processed target, the result, a shared value map, an error if one occurred, and a boolean indicating if the execution is running.
func (*ResultContext[TARGET, RESULT]) Cancel ¶
func (ctx *ResultContext[TARGET, RESULT]) Cancel()
Cancel cancels the execution. It switches the isExecuting flag to false.
func (*ResultContext[TARGET, RESULT]) Error ¶
func (ctx *ResultContext[TARGET, RESULT]) Error() error
Error returns the error that occurred during processing, if any.
func (*ResultContext[TARGET, RESULT]) Result ¶
func (ctx *ResultContext[TARGET, RESULT]) Result() RESULT
Result returns the result of processing the target.
func (*ResultContext[TARGET, RESULT]) Target ¶
func (ctx *ResultContext[TARGET, RESULT]) Target() TARGET
Target returns the processed target from the result context.
func (*ResultContext[TARGET, RESULT]) Value ¶
func (ctx *ResultContext[TARGET, RESULT]) Value(key string) any
Value returns the shared value associated with the provided key after execution. If the key is not found in the shared value map, it returns nil.