You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I had question about when we need to Close(). I've added a small block to the example on the README.
// HERE IS NEW CODE!!// Let's say we throw an error here and return. wp is still processing the first 500 tasks.ifn==500 {
returnfmt.Errorf("n == 500")
}
Should we call defer wp.Close() immediately after we create it to avoid the workerpool continuing tasks it has in flight (unless people want the existing tasks to keep running)? Users may want to cancel all running tasks if they aren't able to submit or cancel all existing tasks if they throw an error else where. Both are valid, but I think it's worth calling out in the docs.
funcmain() {
wp:=workerpool.New(runtime.NumCPU())
fori, n:=0, int64(1_000_000_000_000_000_000); n<1_000_000_000_000_000_100; i, n=i+1, n+1 {
n:=n// https://golang.org/doc/faq#closures_and_goroutinesid:=fmt.Sprintf("task #%d", i)
// Use Submit to submit tasks for processing. Submit blocks when no// worker is available to pick up the task.err:=wp.Submit(id, func(_ context.Context) error {
fmt.Println("isprime", n)
ifIsPrime(n) {
fmt.Println(n, "is prime!")
}
returnnil
})
// Submit fails when the pool is closed (ErrClosed) or being drained// (ErrDrained). Check for the error when appropriate.iferr!=nil {
fmt.Fprintln(os.Stderr, err)
return
}
// HERE IS NEW CODE!!// Let's say we throw an error here and return. wp is still processing the first 500 tasks.ifn==500 {
returnfmt.Errorf("n == 500")
}
}
// Drain prevents submitting new tasks and blocks until all submitted tasks// complete.tasks, err:=wp.Drain()
iferr!=nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Iterating over the results is useful if non-nil errors can be expected.for_, task:=rangetasks {
// Err returns the error that the task returned after execution.iferr:=task.Err(); err!=nil {
fmt.Println("task", task, "failed:", err)
}
}
// Close should be called once the worker pool is no longer necessary.iferr:=wp.Close(); err!=nil {
fmt.Fprintln(os.Stderr, err)
}
}
The text was updated successfully, but these errors were encountered:
Should we call defer wp.Close() immediately after we create it to avoid the workerpool continuing tasks it has in flight (unless people want the existing tasks to keep running)?
Yes, you can defer it and still keep the Close() call in the example, as it is safe to call Close() twice on a workerpool (although it will return ErrClosed beyond the first time). To be honest I've never encountered this use-case where the main goroutine has an error to throw, most of the time its control flow is focused on managing the workerpool and tasks. Do you have a concrete use-case example out of curiosity?
Users may want to cancel all running tasks if they aren't able to submit
In this case they should not use this module, or ensure that the workerpool capacity is at least equal to the total number of tasks they want to run. From the README:
When the limit of concurrently running workers is reached, submitting a task blocks until a worker is able to pick it up. This behavior is intentional as it prevents from accumulating tasks which could grow unbounded.
cancel all existing tasks if they throw an error elsewhere
Yes, deferring wp.Close() is a valid solution. Maybe we should add it in the example cc @rolinh
Hi, I had question about when we need to
Close()
. I've added a small block to the example on the README.Should we call
defer wp.Close()
immediately after we create it to avoid the workerpool continuing tasks it has in flight (unless people want the existing tasks to keep running)? Users may want to cancel all running tasks if they aren't able to submit or cancel all existing tasks if they throw an error else where. Both are valid, but I think it's worth calling out in the docs.The text was updated successfully, but these errors were encountered: