OWF12/OSDC.Fr Golang concurrency patterns
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,164
On Slideshare
1,164
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PROGRESS 1 Faisons travaillerdes gophers ensemble
  • 2. PROGRESS 2Présentation
  • 3. PROGRESS 3Bruno Michel✰ Directeur Technique d af83✰ Développeur de LinuxFr.org✰ Ancien président de Ruby France✰ github.com/nono✰ twitter.com/brmichel
  • 4. PROGRESS 4Gophers« Gaufre (ou gauphre) est un nom vernaculaire ambigu en français, pouvant désignerdes rongeurs […] » — Wikipédia
  • 5. PROGRESS 5Go✰ Langage Open Source, géré par la communauté✰ Une version stable, Go 1✰ Langage moderne et agréable à utiliser✰ Pour faire des logiciels simples, performants et fiables
  • 6. PROGRESS 6Concurrence et parallélisme
  • 7. PROGRESS 7Concurrence« Programming as the composition of independently executingprocesses. » — Ro b P i k e
  • 8. PROGRESS 8Parallélisme« Programming as the simultaneous execution of (possibly related) computations. » — Ro b Pik e
  • 9. PROGRESS 9Concurrence vs. parallélismeConcurrence : une question de structure de donnéesParallélisme : à propos de lexécution
  • 10. PROGRESS 10Les primitives de Go
  • 11. PROGRESS 11Goroutinefunc computation(n int) { time.Sleep(n * time.Milliseconds) fmt.Printf("%s seconds elapsedn", n)}go computation(3)go computation(1)go computation(5)computation(7)
  • 12. PROGRESS 12Channelsfunc pingpong(ch chan int) { n := <-ch fmt.Printf("Received %dn", n) ch <- n}func main() { ch := make(chan int) go pingpong(ch) ch <- 42 <-ch}
  • 13. PROGRESS 13Buffered Channelsfunc pingpong(ch chan int) { n := <-ch fmt.Printf("Received %dn", n) ch <- n}func main() { ch := make(chan int, 5) go pingpong(ch) ch <- 42 <-ch}
  • 14. PROGRESS 14Selectselect { case chan1 <- nb: fmt.Printf("c1") case str := <-chan2: fmt.Printf("c2") case str := <-chan3: fmt.Printf("c3")}
  • 15. PROGRESS 15Quelques patterns
  • 16. PROGRESS 16Génerateurfunc idGenerator() chan int { ids := make(chan int) go func() { id := 0 for { ch <- id id++ } } return ids}ids := idGenerator()id1 := <-idsid2 := <-ids
  • 17. PROGRESS 17Timeoutselect { case n := <-ch: fmt.Printf("Received %d", n) case <-time.After(2 * time.Seconds) fmr.Printf("Too late")}
  • 18. PROGRESS 18Tickersticker := time.NewTicker(50 * time.Millisecond) go func() { for t := range ticker.C { fmt.Println("Tick at", t) }}()time.Sleep(150 * time.Millisecond)ticker.Stop()
  • 19. PROGRESS 19Workerfunc worker(in <-chan *Work, out chan<- *Work) { for w := range in { w.z = w.x * w.y Sleep(w.z) out <- w }}
  • 20. PROGRESS 20Load balancerfunc Run() { in := make(chan *Work) out := make(chan *Work) for i := 0; i < NumWorkers; i++ { go worker(in, out) } go sendLotsOfWork(in) receiveLotsOfResults(out)}
  • 21. PROGRESS 21Conclusion
  • 22. PROGRESS 22Goroutines + Channels = <3✰ Ni mutex✰ Ni semaphore✰ Mais cest très puissant
  • 23. PROGRESS 23Mise en applicationExternal images for LinuxFr.orghttps://github.com/nono /img-LinuxFr.org
  • 24. PROGRESS 24Pour aller plus loinConcurrency is not Parallelism (its better) — Rob Pikehttp://waza.heroku.com/