-
Notifications
You must be signed in to change notification settings - Fork 327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement goroutines state monitor? #85
Comments
Also, a job can be a struct with method |
Hi @PROger4ever! Thanks for the idea I'm not sure it would be possible to design this in a way that is general enough to be broadly useful and without adding significant surface area to the the API of the library. For example, what if you need to know something other than a string? Or what if you don't want to monitor ever second, but instead you want to print to stderr every time a SIGHUP is sent? I think this can be implemented more generally outside of the library in a way that doesn't significantly constrain a user's choice of how to monitor. The monitoring hooks can be passed in to the closure, then control is left in the hands of the user. func main() {
p := pool.New()
states := make([]atomic.Value, 10)
for i := 0; i < 10; i++ {
i := i
p.Go(func() {
states[i].Store("Doing work 1...")
time.Sleep(time.Second)
states[i].Store("Doing work 2...")
time.Sleep(time.Second)
})
}
monitorCtx, cancelMonitor := context.WithCancel(context.Background())
monitorPool := pool.New().WithContext(monitorCtx)
monitorPool.Go(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-time.After(100 * time.Millisecond):
for i := range states {
fmt.Fprintf(os.Stderr, "%d: %s\n", i, states[i].Load())
}
}
}
})
p.Wait()
cancelMonitor()
monitorPool.Wait()
} |
You can already pretty easily do this just by passing the struct's method to the type myTask struct {
val int
}
func (t myTask) Run() {
println(t.val)
}
func main() {
p := pool.New()
for i := 0; i < 10; i++ {
task := myTask{i}
p.Go(task.Run)
}
p.Wait()
} |
Sometimes there are cases, when goroutines states should be printed in console.
Similar functionality: hmdsefi/gowl
Usage proposal:
The text was updated successfully, but these errors were encountered: