- Notifications
You must be signed in to change notification settings - Fork25
A golang parallel library, used for business logic aggregation and refactory without changing user function declaration.
License
buptmiao/parallel
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A golang parallel library, used for business logic aggregation and refactory without changing function declaration.
go get github.com/buptmiao/parallel
There are three methods: testjobA, testjobB, testjobC, execute them in parallel:
import ("github.com/buptmiao/parallel")functestJobA()string {return"job"}functestJobB(x,yint)int {returnx+y}functestJobC(xint)int {return-x}funcmain() {varsstringvarx,yintp:=parallel.NewParallel()p.Register(testJobA).SetReceivers(&s)p.Register(testJobB,1,2).SetReceivers(&x)p.Register(testJobC,3).SetReceivers(&y)// block herep.Run()ifs!="job"||x!=3||y!=-3{panic("unexpected result")}}
Let's see a little complex case, there are three parallel jobs: jobA, jobB, jobC and a final Job which aggregates the result. The final depends on jobA and middle which depends on jobB and jobC.
jobA jobB jobC \ \ / \ \ / \ middle \ / \ / final
Refer to thedemo below:
import ("github.com/buptmiao/parallel")typemiddlestruct {BintCint}typetestResultstruct {AstringMmiddle}functestJobA()string {return"job"}functestJobB(x,yint)int {returnx+y}functestJobC(xint)int {return-x}functestFinal(s*string,m*middle)testResult {returntestResult{*s,*m,}}funcmain() {varmmiddlevarsstringvarrestestResultp:=parallel.NewParallel()// Create a child 1child1:=p.NewChild()child1.Register(testJobA).SetReceivers(&s)// Create another child 2child2:=p.NewChild()child2.Register(testJobB,1,2).SetReceivers(&m.B)child2.Register(testJobC,2).SetReceivers(&m.C)p.Register(testFinal,&s,&m).SetReceivers(&res)// block herep.Run()expect:=testResult{"job",middle{3,-2,},}ifres!=expect {panic("unexpected result")}}
By default, Parallel will ignore panics of jobs. But parallel supports customized exception handler, which is used for dealing with unexpected panics. For example, alerting or logging.
// handle the panicfuncexceptionHandler(topicstring,einterface{}) {fmt.Println(topic,e)}// will panicfuncexceptionJob() {varamap[string]int//assignment to entry in nil mapa["123"]=1}funcmain() {p:=parallel.NewParallel()p.Register(exceptionJob)// miss the last argument on purposep.Except(exceptionHandler,"topic1")p.Run()}
About
A golang parallel library, used for business logic aggregation and refactory without changing user function declaration.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.