-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taskgroup): improve task group functionality (#81)
- Loading branch information
Showing
12 changed files
with
352 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/alitto/pond/v2/examples/task_group_context | ||
|
||
go 1.22 | ||
|
||
require github.com/alitto/pond/v2 v2.0.0 | ||
|
||
replace github.com/alitto/pond/v2 => ../../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/alitto/pond/v2" | ||
) | ||
|
||
func main() { | ||
// Generate 1000 tasks that each take 1 second to complete | ||
tasks := generateTasks(1000, 1*time.Second) | ||
|
||
// Create a pool with a max concurrency of 10 | ||
pool := pond.NewPool(10) | ||
defer pool.StopAndWait() | ||
|
||
// Create a context with a timeout of 5 seconds | ||
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
// Create a group with the timeout context | ||
group := pool.NewGroupContext(timeout) | ||
|
||
// Submit all tasks to the group and wait for them to complete or the timeout to expire | ||
err := group.Submit(tasks...).Wait() | ||
|
||
if err != nil { | ||
fmt.Printf("Group completed with error: %v\n", err) | ||
} else { | ||
fmt.Println("Group completed successfully") | ||
} | ||
} | ||
|
||
func generateTasks(count int, duration time.Duration) []func() { | ||
|
||
tasks := make([]func(), count) | ||
|
||
for i := 0; i < count; i++ { | ||
i := i | ||
tasks[i] = func() { | ||
time.Sleep(duration) | ||
fmt.Printf("Task #%d finished\n", i) | ||
} | ||
} | ||
|
||
return tasks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.