OWF12/OSDC.Fr Golang concurrency patterns

Post on 01-Jul-2015

810 views 4 download

Transcript of OWF12/OSDC.Fr Golang concurrency patterns

Faisonstravaillerdesgophersensemble

PROGRESS 1

Présentation

PROGRESS 2

BrunoMichelBrunoMichel✰ Directeur Technique d 'D irecteur Technique d ' a f83af83

✰ Déve loppeur de Déve loppeur de L inuxFr.orgLinuxFr.org

✰ Anc ienp rés ident de Anc ienp rés ident de Ruby F ranceRuby F rance

✰ github. com/nonogithub. com/nono

✰ twitter. com/brmiche ltwitter. com/brmiche l

PROGRESS 3

GophersGophers«« Gaufre (ou gauphre) est un nomvernacu la ire ambigu en f rança is , pouvant dés ignerGaufre (ou gauphre) est un nomvernacu la ire ambigu en f rança is , pouvant dés igner

des rongeurs […]des rongeurs […] »»—— Wik ipéd i aWik i péd i a

PROGRESS4

GoGo✰ Langage Open Source , géré pa r la communautéLangage Open Source , géré pa r la communauté

✰ Une vers ion s table , Go 1Une vers ion s table , Go 1

✰ Langage moderne et agréable à uti l ise rLangage moderne et agréable à uti l ise r

✰ Pour fa ire des logic ie ls s imp les , performants et f iablesPour fa ire des logic ie ls s imp les , performants et f iables

PROGRESS 5

Concurrenceetparallélisme

PROGRESS 6

ConcurrenceConcurrence«« P rogrammingas the composit ion o f independently executingP rogrammingas the composit ion o f independently executing

p rocesses .p rocesses . »»—— Rob P i k eRob P i k e

PROGRESS7

ParallélismeParallélisme«« P rogramming a s t he s imul t aneous execut i on o f (poss i b l y re l a ted ) computa t i ons .Programming a s t he s imul t aneous execut i on o f (poss i b l y re l a ted ) computa t i ons . »»

—— Ro b P ik eRo b P ik e

PROGRESS8

Concurrencevs.parallélismeConcurrencevs.parallélismeConcurrence : une questionde s tructure de donnéesConcurrence : une questionde s tructure de données

Pa ra l lé l isme : à p ropos de l 'exécutionPa ra l lé l isme : à p ropos de l 'exécution

PROGRESS 9

LesprimitivesdeGo

PROGRESS10

GoroutineGoroutinefunccomputation(nint){time.Sleep(n*time.Milliseconds)fmt.Printf("%ssecondselapsed\n",n)}gocomputation(33)gocomputation(11)gocomputation(55)computation(77)

PROGRESS11

ChannelsChannelsfuncpingpong(chchanint){n:=<-chfmt.Printf("Received%d\n",n)ch<-n}

funcmain(){ch:=make(chanint)gopingpong(ch)ch<-4242<-ch}

PROGRESS12

BufferedChannelsBufferedChannelsfuncpingpong(chchanint){n:=<-chfmt.Printf("Received%d\n",n)ch<-n}

funcmain(){ch:=make(chanint,55)gopingpong(ch)ch<-4242<-ch}

PROGRESS13

SelectSelectselect{casechan1<-nb:fmt.Printf("c1")casestr:=<-chan2:fmt.Printf("c2")casestr:=<-chan3:fmt.Printf("c3")}

PROGRESS14

Quelquespatterns

PROGRESS15

GénerateurGénerateurfuncidGenerator()chanint{ids:=make(chanint)gofunc(){id:=00for{ch<-idid++}}returnids}

ids:=idGenerator()id1:=<-idsid2:=<-ids

PROGRESS16

TimeoutTimeout

select{casen:=<-ch:fmt.Printf("Received%d",n)case<-time.After(22*time.Seconds)fmr.Printf("Toolate")}

PROGRESS17

TickersTickersticker:=time.NewTicker(5050*time.Millisecond)gofunc(){fort:=rangeticker.C{fmt.Println("Tickat",t)}}()

time.Sleep(150150*time.Millisecond)ticker.Stop()

PROGRESS18

WorkerWorkerfuncworker(in<-chan*Work,outchan<-*Work){forw:=rangein{w.z=w.x*w.ySleep(w.z)out<-w}}

PROGRESS19

LoadbalancerLoadbalancerfuncRun(){in:=make(chan*Work)out:=make(chan*Work)fori:=00;i<NumWorkers;i++{goworker(in,out)}gosendLotsOfWork(in)receiveLotsOfResults(out)}

PROGRESS20

Conclusion

PROGRESS21

Goroutines+Channels=<3Goroutines+Channels=<3✰ Ni mutexNi mutex

✰ Ni semaphoreNi semaphore

✰ Ma is c'est très puissantMa is c'est très puissant

PROGRESS22

MiseenapplicationMiseenapplicationExterna l images for L inuxFr.orgExterna l images for L inuxFr.org

https ://github. com/nono/ img-LinuxFr.orghttps ://github. com/nono/ img-LinuxFr.org

PROGRESS23

PourallerplusloinPourallerplusloinConcurrency is not Pa ra l le l ism ( it's better) —Rob P ikeConcurrency is not Pa ra l le l ism ( it's better) —Rob P ike

http ://waza .heroku. com/http ://waza .heroku. com/

PROGRESS24