Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A golang parallel library, used for business logic aggregation and refactory without changing user function declaration.

License

NotificationsYou must be signed in to change notification settings

buptmiao/parallel

Repository files navigation

Build StatusCoverage StatusGo Report Card

zh_CN

A golang parallel library, used for business logic aggregation and refactory without changing function declaration.

Install

go get github.com/buptmiao/parallel

Usage

eg.1

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")}}

eg.2

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")}}

eg.3

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp