func main() {
pool := worker.NewPool(2)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
works := worker.GenerateSquares(ctx, 500*time.Millisecond)
results := pool.Run(ctx, works)
fmt.Println("1)", <-results)
fmt.Println("2)", <-results)
// Give the time to generate and do more works that won't be received.
time.Sleep(2 * time.Second)
fmt.Println("cancel context!")
cancel()
// Try to drain a result channel before the cancel broadcast will reach the runners.
for result := range results {
fmt.Println("*)", result)
}
// Give the time all runners to print about closing.
time.Sleep(1 * time.Second)
}
ilya2049/go-async
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published