Skip to content

A worker pool which processes work in parallel but outputs results in the order the work was given

License

Notifications You must be signed in to change notification settings

codesoap/lineworker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lineworker provides worker pools that perform work in parallel, but output the work results in the order the work was given.

Take a look at the documentation for more info: https://godocs.io/github.com/codesoap/lineworker

Example

slowSprint := func(a int) (string, error) {
	delay := rand.Int()
	time.Sleep(time.Duration(delay%6) * time.Millisecond)
	return fmt.Sprint(a), nil
}

// Start the worker goroutines:
pool := lineworker.NewWorkerPool(runtime.NumCPU(), slowSprint)

// Put in work:
go func() {
	for i := 0; i < 10; i++ {
		workAccepted := pool.Process(i)
		if !workAccepted {
			// Cannot happen in this example, because pool.Stop is not called
			// outside this goroutine, but is handled for demonstration
			// purposes.
			return
		}
	}
	pool.Stop()
}()

// Retrieve the results:
for {
	res, err := pool.Next()
	if err == lineworker.EOS {
		break
	} else if err != nil {
		panic(err)
	}
	fmt.Println(res)
}

About

A worker pool which processes work in parallel but outputs results in the order the work was given

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages