Pool context option & stop with timeout

This commit is contained in:
Alejandro Durante
2022-01-02 09:38:32 -03:00
parent 9d7bd8fe08
commit 6c719078e5
9 changed files with 243 additions and 60 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ module github.com/alitto/pond/examples/dynamic_size
go 1.17
require (
github.com/alitto/pond v1.5.1
github.com/alitto/pond v1.6.1
)
replace github.com/alitto/pond => ../../
+1 -1
View File
@@ -3,7 +3,7 @@ module github.com/alitto/pond/examples/fixed_size
go 1.17
require (
github.com/alitto/pond v1.5.1
github.com/alitto/pond v1.6.1
)
replace github.com/alitto/pond => ../../
+7
View File
@@ -0,0 +1,7 @@
module github.com/alitto/pond/examples/pool_context
go 1.17
require github.com/alitto/pond v1.6.1
replace github.com/alitto/pond => ../../
+35
View File
@@ -0,0 +1,35 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/alitto/pond"
)
// Pressing Ctrl+C while this program is running will cause the program to terminate gracefully.
// Tasks being processed will continue until they finish, but queued tasks are cancelled.
func main() {
// Create a context that will be cancelled when the user presses Ctrl+C (process receives termination signal).
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// Create a pool and pass the context to it.
pool := pond.New(1, 1000, pond.Context(ctx))
defer pool.StopAndWait()
// Submit several long runnning tasks
var count int = 100
for i := 0; i < count; i++ {
n := i
pool.Submit(func() {
fmt.Printf("Task #%d started\n", n)
time.Sleep(1 * time.Second)
fmt.Printf("Task #%d finished\n", n)
})
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ module github.com/alitto/pond/examples/fixed_size
go 1.17
require (
github.com/alitto/pond v1.5.1
github.com/alitto/pond v1.6.1
github.com/prometheus/client_golang v1.9.0
)
+1 -1
View File
@@ -3,7 +3,7 @@ module github.com/alitto/pond/examples/task_group
go 1.17
require (
github.com/alitto/pond v1.5.1
github.com/alitto/pond v1.6.1
)
replace github.com/alitto/pond => ../../