cmitsakis / workerpool-go Goto Github PK
View Code? Open in Web Editor NEWauto-scaling worker pool (work queue) in Go, using generics
License: Apache License 2.0
auto-scaling worker pool (work queue) in Go, using generics
License: Apache License 2.0
If I take the example from the documentation but I add WithResult
:
func main() {
p, _ := workerpool.NewPoolWithResults(4, func(job workerpool.Job[float64], workerID int) (int, error) {
result := math.Sqrt(job.Payload)
fmt.Println("result:", result)
return 0, nil
})
for i := 0; i < 100; i++ {
p.Submit(float64(i))
}
p.StopAndWait()
for result := range p.Results {
fmt.Println(result)
}
}
the code never stops and is stuck at iteration ~3
Hey there, really like your workerpool package. I am fiddling around with it to use the init
/deinit
feature to create a progress bar for each worker.
It might be a stupid idea to begin with (I have second thoughts), but for the sake of an argument.
An init function create a tracker:
func initWrk(workerID int) (tr *progress.Tracker, err error) { // worker init function
tr = &progress.Tracker{
Message: fmt.Sprintf("wrk%d", workerID),
Units: progress.UnitsDefault,
}
return tr, nil
}
The handler can increment the tracker each time it handles something...
func handler3(job workerpool.Job[Payload], workerID int, tracker *progress.Tracker) (Result, error) {
r := Result{}
r.Sqrt = math.Sqrt(float64(job.Payload.Num))
time.Sleep(100 * time.Millisecond)
tracker.Increment(1)
return r, nil
}
My problem is that I'd need the tracker in the main function as I need to add it to the progress.Writer
.
pw.AppendTracker(tracker)
Any chance I can get the init result from each worker handled up? SImilar to range p.Results
?
for tracker := range p.Connections {
pw.AppendTracker(tracker)
}
}
i read through the code to see if it may fit my needs and stumbled upon this very minor duplication in https://github.com/cmitsakis/workerpool-go/blob/master/main.go#L552
Concerning the debug statements it looks like a local struct method could clean things up.
// usage
// => w.debug(fmt.Sprintf("[workerpool/worker%d] finished", w.id))
func (w *worker[I, O, C]) debug(msg string){
if w.pool.loggerDebug != nil {
LoggerDebug.Println(msg) //this should also add the \n
}
}
I am not a go veteran, so i had to lookup some constructs you are using and found some methods are hard to interpret/read. Especially the loop func is quite a bit of magic :-)
Still i really like the basic ideas and having it encapsulated in a tiny self-contained package is great!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.