-
Is it possible for a task function to know the state of the pool and exit early if Can the function safely call |
Beta Was this translation helpful? Give feedback.
Answered by
alitto
May 9, 2023
Replies: 1 comment
-
Hey! A few things to note here:
That said, if we focus on the last scenario (task is running while the pool is stopped) then you have a few alternatives to detect if the pool was stopped.
// Create a context with a cancel function. We will use the cancel function to stop the pool instead of directly calling Stop() or StopAndWait()
ctx, cancel := context.WithCancel()
// Create a pool and pass the context to it.
pool := pond.New(1, 1000, pond.Context(ctx))
// Submit a task
pool.Submit(func() {
select {
case <-time.After(500 * time.Millisecond): // Here we simulate the task's main processing
fmt.Println("task function completed")
case <-ctx.Done(): // If we receive from this channel, it means the pool was stopped.
fmt.Println("pool was stopped")
}
})
// Stop the pool
cancel() Does it make sense? |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
romdr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey!
A few things to note here:
Submit()
method will return a non-nill error.Stop()
then it won't be executed. If you callStopAndWait()
then it will be executed. More info here.That said, if we focus on the last scenario (task is running while the pool is stopped) then you have a …