Simple fork and join of goroutines - easy to use; no fuss.
Puts the 'P' of CSP back into Go.
All it does is handle the join when a group of goroutines terminate. Internally, a sync.WaitGroup
is
administered for you. There's not much to it but it makes your job easier.
go get -u github.com/rickb777/process
Just create a new group then tell some functions to Go
:
processes := process.NewGroup()
processes.Go(func() {
... some work, just a normal goroutine function
})
processes.Go(func() {
... some other work
})
processes.Join()
The Join()
method does not terminate until all the sub-processes have terminated. In other words,
the code that follows has to wait until then.
The second way to use process groups is to start a pool of several identical goroutines using GoN
:
processes := process.NewGroup()
processes.GoN(3, func() {
... some work, just a normal goroutine function
})
processes.Join()
You can of course mix Go
and GoN
in the same process group:
processes := process.NewGroup()
processes.GoN(3, func() {
... some work, just a normal goroutine function
})
processes.Go(func() {
... some other work
})
processes.Join()
GoN1
is a variant of GoN
that provides the index counter as a parameter, counting up from 1:
processes := process.NewGroup()
processes.GoN1(3, func(i int) {
... some work, just a normal goroutine function
})
processes.Join()
A process group contains processes. These processes can also be process groups, or they can contain process
groups. As long as the Join
calls are positioned so that each group terminates tidily, the nesting should
just work (TM).