Google I/O 2012 - Go Concurrency Patterns

Поділитися
Вставка
  • Опубліковано 17 чер 2024
  • Rob Pike
    Concurrency is the key to designing high performance network services. Go's concurrency primitives (goroutines and channels) provide a simple and efficient means of expressing concurrent execution. In this talk we see how tricky concurrency problems can be solved gracefully with simple Go code.
    For all I/O 2012 sessions, go to developers.google.com/io/
  • Наука та технологія

КОМЕНТАРІ • 159

  • @GoogleDevelopers
    @GoogleDevelopers  Місяць тому +1

    Register to save your spot for Google I/O✨➡ goo.gle/io24-yt

  • @speedTurtle
    @speedTurtle 11 місяців тому +10

    Rob's clarity in these talks is next level. Each one I watch gets me more excited about learning Go.

  • @serhioromano
    @serhioromano 4 роки тому +24

    I am a teacher myself and I can see how much work was put in the preparation for this talk. Few weeks probably for 40 minutes talk.

  • @yzhang2008
    @yzhang2008 Рік тому +2

    Though having seen these patterns when reading open source code before, it's still very helpful to watch this video. Thanks!

  •  8 років тому +27

    I'm relatively new to programming and was trying to get the hang of multithreading in Go. This video helped a lot. Thank you.

    • @kamilziemian995
      @kamilziemian995 Рік тому +1

      By now, you are probably programming expert.

    • @vikingthedude
      @vikingthedude 11 місяців тому +2

      Are you a super saiyan programmer bro

  • @luzon83
    @luzon83 12 років тому +4

    Go is growing on me more and more. In fact, I'm starting to love it.

    • @mgjulesdev
      @mgjulesdev 4 роки тому

      @John Markovich Well I do still love it :D

  • @shonhloi1
    @shonhloi1 6 років тому +35

    Rob really can explain

  • @EadwinTomlinson
    @EadwinTomlinson 3 роки тому

    Ker-Kling that's the penny dropping for me on channels in Go... Amazing talk

  • @FeiHedroxer
    @FeiHedroxer 10 років тому +6

    The best presentation for Go Concurrency.

    • @eileennoonan771
      @eileennoonan771 4 місяці тому

      Still true. I'm learning Go now, I've watched a lot of videos and read books and this is the best.

  • @konstantinwirz
    @konstantinwirz 2 роки тому +1

    Great talk, many thanks for publishing it

  • @guidogiuntoli4184
    @guidogiuntoli4184 2 місяці тому

    Very clear and concise, right timing too.

  • @willemdhaeseleer87
    @willemdhaeseleer87 6 років тому +3

    "To make the boring less interesting" what a great sentence.

  • @liviuchircu
    @liviuchircu 4 роки тому

    23:40 when you click on "Run" again, just to double-check whether you seeded your PRNG with a random value. But you didn't! XD THANK YOU FOR THE TALK!

  • @GuoGuangChiou
    @GuoGuangChiou 6 років тому

    Amazing language handling the concurrency

  • @sumantkanala
    @sumantkanala 6 років тому +1

    Superb explanation!

  • @GeckoCkCkCk
    @GeckoCkCkCk 12 років тому +13

    Thank you for such a great presentation! The concurrence examples were exactly what I was looking for, and convinced me to start learning Go today.
    Might you consider using concise file reading and parsing like in AWK? Thanks.

  • @LunfardoAR
    @LunfardoAR 8 років тому +2

    loved the last question: "if my routine is deadlocked, will it stay like that?", i mean, he gave fairly good number of examples of how timeout such things.

    • @jnevercast
      @jnevercast 7 років тому

      The question did state "and I don't have a timeout". Another asker followed up later with the same question. What happens if a writer writes to a channel and no one ever reads from it.
      From what I can gather in Go is that you simply "Don't let it happen". If you need to make a dynamic decision on a channel, you need to have the signal passed across, or use a buffered channel.

  • @shuaibolayinka7644
    @shuaibolayinka7644 2 роки тому +1

    Thanks for this Rob

  • @mgutzzz
    @mgutzzz 10 років тому +1

    Wonderful talk as is the language.

  • @U6D65
    @U6D65 12 років тому +2

    A good talk, i'm definitely going to learn Go.
    Also, about Heroku Waza talk that Rob mentioned, it's not out yet apparently (after 6 months).

  • @fingerprince_
    @fingerprince_ 11 років тому +5

    what an excellent man

  • @jatinoham
    @jatinoham 3 роки тому +14

    Is there any one watching this video in 2021 just like me

  • @user-ry5pu7wp9d
    @user-ry5pu7wp9d 6 місяців тому

    Awesome presentation!

  • @godsarmycy
    @godsarmycy 11 років тому

    A remarkable talk

  • @MichaelSoltys
    @MichaelSoltys 11 років тому +1

    Very interesting. Used it in my concurrency class.

  • @lucasquinnlife
    @lucasquinnlife 5 років тому +13

    Highly recommend to watch this video after you read all offical tour for deeper understanding.

    • @migueld2456
      @migueld2456 2 роки тому +1

      I agree. This is what I felt was "missing" from the tour

  • @AkashKumar-lr6hc
    @AkashKumar-lr6hc 9 днів тому

    great presentation

  • @anferneegui
    @anferneegui 12 років тому +1

    this is a great talk. very inspiring.

  • @ning7448
    @ning7448 3 роки тому

    great video

  • @tobiasgleiter
    @tobiasgleiter 3 місяці тому

    Awesome Video!

  • @GeertJohanRiemer
    @GeertJohanRiemer 12 років тому

    A switch isn't made for communication (channels), switch just does value comparison. It is not posible to use use a channel get (myInt :=

  • @MengWeichao
    @MengWeichao 10 років тому +1

    Though go is a static language, it can be so powerful that it has elegant grammar and better concurrency capability that make us write less yet robust code

    • @brandonchapman3110
      @brandonchapman3110 Рік тому

      Less code means more black box magic and opinionated design. It’s painful

  • @Tehploe
    @Tehploe 10 років тому +1

    I

  • @argxuy8226
    @argxuy8226 3 роки тому

    amazing presentation

  • @frederickrbrock
    @frederickrbrock 11 років тому

    great presentation.

  • @allyourcode
    @allyourcode 3 роки тому +3

    @25:23 This seems like an anti pattern, because both go routines (the parent and the child) play the role of sender and receiver on the quit channel. Seems to me, you generally want one go routine to only send, and the other to only receive (on the same channel). In this case, seems to make the most sense that the parent go routine is the sender (on the quit channel) and the child only receives (again, on quit). To make the parent block on cleanup returning, you'd have a third channel: done_cleaning_up, which (in addition to c, the primary result channel) gets returned by boring. I know that's 50% more channels than the design shown, but I feel that more channels is preferable vs. overloading quit.
    If we could change the language slightly, another way to make this nicer is if the receiver had the ability to close a channel (Go only allows the sender to do this), because then, instead of using quit to signal from the parent to the child that no more values are wanted via c, the parent simply closes c, and the send in the child would fail.

  • @silvapmo
    @silvapmo 10 років тому +19

    I hope google goes with go (golang) as the new official language for android development, it will be the most assertive choice. This will be the big “Nextstep” for android ;)

    • @JohnPauljohnp_js
      @JohnPauljohnp_js 9 років тому +1

      it would be great and cool!

    • @brandonchapman3110
      @brandonchapman3110 Рік тому +1

      I hope google realizes their err and goes back to Java. The most readable of the bunch. Kotlin is a notch more repulsive than go, but they’re both terrible

  • @AlesiaKrush
    @AlesiaKrush 7 років тому

    I think current would be a better name for channel, but maybe it's just me ;)

  • @able2663
    @able2663 2 роки тому

    Great!

  • @sntshkmr60
    @sntshkmr60 5 років тому +4

    34:55 Claps!!

  • @kioopi
    @kioopi 10 років тому +19

    Slides at talks.golang.org/2012/concurrency.slide

  • @rainbowlemon
    @rainbowlemon 8 років тому +131

    Hey guys I heard you like channels so I put a channel in your channel

  • @edwardli6685
    @edwardli6685 5 років тому +8

    damn, 6 years ago..

  • @gmcodes3797
    @gmcodes3797 4 місяці тому

    I was trying to follow this along on my local, an interesting finding at 18:06 , in the fanIn function when you spawn two anonymous goroutines, if you don't use for loop around c

  • @allyourcode
    @allyourcode 3 роки тому

    @31:56 The loop in the second half of this example is funny, because there is a magical number in there (i < 3), which you have to remember to update if you add something like Shopping. I think in other languages, you would join all the threads (or in this case, go routines). Unfortunately, go routines have no handle on which to join.

    • @kevinchou9155
      @kevinchou9155 3 роки тому +2

      Yeah it's a trick. Formally you need a wait group and a new goroutine to wait all the searching goroutine to finish, and then close the channel. And in main goroutine, you can use a for-range on the channel. The lecturer just tried to avoid the using of waitgroup 'cause it's gonna be a distraction.

    • @TheSurvivor1963
      @TheSurvivor1963 2 роки тому +2

      @@kevinchou9155 No, actually all the search functions have all the same signature, you could collect them in a slice and spawn them off as Go-routines in a for-loop. Then the second for-loop iterates the same number of times as the length of the slice. (i.e the same number as the number of search functions). You don't have to close a channel in order for it to be garbage collected, as soon as a channel goes out of scope and is not referenced it will get garbage collected. It's clearly stated in the language documentation. There is no need for a wait-group.
      If you need to signal a consumer that the producer is done, then you can do it by closing a channel,- or if you iterate over a channel with a for-loop, ( for element := range channel { ...} ), you need to close the channel in order to end the for-loop.

  • @daviddemps2977
    @daviddemps2977 Рік тому

    Nice

  • @rajivzidane
    @rajivzidane 11 років тому

    It is out on vimeo now

  • @atarzwell
    @atarzwell 11 років тому

    Maybe on the surface. If you want to see the difference, time running 100,000 async calls. ;)

  • @elimisteve
    @elimisteve 12 років тому

    Re 49:20 -- Instead of nesting, it seems like you could just use a switch statement, I think...

  • @youfu4496
    @youfu4496 9 років тому

    good

  • @PraveenLobo25
    @PraveenLobo25 2 роки тому

    WoW Moment at 28:16!!

  • @AbhinandanNM
    @AbhinandanNM 9 років тому +1

    shit flew above my head!

  • @johnhammer8668
    @johnhammer8668 2 роки тому

    28:50 "and some ads" Rob is humorous

    • @duaaalkaabi5158
      @duaaalkaabi5158 Рік тому

      سبانم٧اغزعخع٦لنع٦تت٥ل٥فد٤دثذثذث، ثصظضاخككحةك٠ةجةةكنبفيقتيثس🎉
      Lugina e Ujqërve Pjesa VI 1
      😢❤🥝😊الله
      مشاهدة

  • @user-ry5pu7wp9d
    @user-ry5pu7wp9d 6 місяців тому

    In the fanIn part, isn't it just unnecessary to make some sort of separate proxy function for them when we can just modify the boring function to take in more than one param and function just like a multiplexer, while the fmt.Println() function prints whatever value it gets first from the boring function

  • @luzon83
    @luzon83 12 років тому +2

    6:12 "These are going to be really boring programs, but they're going to use concurrency to make the boring a little less interesting." :D

  • @zerosandones701
    @zerosandones701 8 років тому

    So regardless of whether I use the fanIn function with two goroutines or the select statement (and copying the code straight from a copy of the slides) -- if I run the sleep with 'rand.Intn(1e3)' I always see Joe / Ann print in lockstep, and if I run it with 'rand.Intn(1e2)' I always see a pattern of 0,0,1,1,2,2,3,4,3,5 (sometimes Joe gets to 5, sometimes Ann does). I've ran it numerous times and never seen a deviation from this. Unless I'm mistaken -- this isn't the expected behavior. What's going on here?

    • @zerosandones701
      @zerosandones701 8 років тому

      +AnirudhC Nvm - I see the rand is deterministic, very interesting

    • @zerosandones701
      @zerosandones701 8 років тому +1

      +zubie7a Yup that was the issue!

    • @baochengliu333
      @baochengliu333 4 роки тому

      @@zerosandones701 tour.golang.org/basics/1

    • @baochengliu333
      @baochengliu333 4 роки тому

      @@zerosandones701may be "Note: The environment in which these programs are executed is deterministic, so each time you run the example program rand.Intn will return the same number.
      (To see a different number, seed the number generator; see rand.Seed. Time is constant in the playground, so you will need to use something else as the seed.)" from tour.golang.org/basics/1

  • @iGuitarified
    @iGuitarified 6 років тому

    Correct me if I'm wrong, but doesn't the select statement do exactly what he's trying to do with fan-in?

    • @pequei
      @pequei 6 років тому +1

      If you are talking about the fanIn with two goroutines vs with only one: he said it is equivalent, just a different way to express the same idea.
      p.s.: if you meant that the fanIn function is not necessary, simply inline it, it could be done for both too, I think.

  • @sntshkmr60
    @sntshkmr60 5 років тому +2

    This talk scares me about concurrency.

  • @philipottam8538
    @philipottam8538 3 роки тому

    08:40 Goroutines
    10:29 Channels

  • @samsammy95
    @samsammy95 4 роки тому

    The answer to the last qn should have been "Now why would you do something like that".

  • @tianhepeng9162
    @tianhepeng9162 4 роки тому +3

    Source code link: talks.golang.org/2012/concurrency/support

  • @bobx7711
    @bobx7711 2 роки тому

    Could somebody tell me how can I get the source code in this presentation? Thx a lot

  • @viewer7654
    @viewer7654 10 років тому +44

    I think I know how they've come up with golang logo :)

    • @v1Broadcaster
      @v1Broadcaster 7 років тому +2

      how i dont get it lol

    • @aruntechguy
      @aruntechguy 6 років тому +1

      Wow! :P I never looked at it that way until now. Pure discrimination! :P

  • @anonymouscode5297
    @anonymouscode5297 8 років тому

    21:21 :: recieved ? from c2

  • @grim.reaper
    @grim.reaper Рік тому

    Can I please get the code snippets and slides for this?

  • @tsobf242
    @tsobf242 5 років тому

    Rob expands on his comment at the end of this about blocking goroutines, that won't return, being garbage collected, saying the goroutines do need to return, otherwise they will be a leak. time.After solves this by using a buffered channel, so even if the channel is not being read, it will return, and if there's no other reference to the channel after time.After returns, the channel is garbage collected.
    groups.google.com/forum/m/#!topic/golang-nuts/bfuwmhbZHYw/discussion

  • @svran1234
    @svran1234 8 років тому

    The answer to the first question was unclear. So the indeterminism of go routines cannot be mocked and you just mock the service being called ?

  •  4 роки тому

    Could someone explain through the Daisy Chain example at 26:27? Thanks!

    • @kevinchou9155
      @kevinchou9155 3 роки тому +1

      The main idea is about creating 10000 blocked goroutines, each copies value from the right channel to the left channel. The creation sequence is from the last channel(leftmost) to the first one(rightmost). In each creation procedure, after finished creating the goroutine, the left channel is no longer needed, and the right channel becomes the left channel we will need in the next iteration, so we made the right become the left for next iteration during which a new right will be freshly made. After the creation completes, all 10000 goroutine are blocked, since no value is sent through the first(rightmost) channel. Then we sent a value, and the value get passed through all the channels and goroutines. The key point is that the whole procedure is really fast with go considering the total numbers of the goroutines. So goroutine is realy light-weighted.

    • @user-sn6zo2oi6v
      @user-sn6zo2oi6v 4 місяці тому

      Hi from 2024!
      I met some difficulties in understanding of this code part, so I decided to rewrite one a little bit and add comments for better understanding. Here it its:
      func main() {
      n := 100000
      // In Rob's code `right` variable has been initialized as `right := leftmost`
      // However, it rewrites every time in for-loop below, so we don't have to initialize `right` here (what a pan!)
      // But we have to keep visibility of this variable within the main function because we have to call it after for-loop
      // The clearest way for us is to declare `right` variable here without initialization
      var right chan int
      leftmost := make(chan int)
      left := leftmost
      // I spent a lot of time to get the one simple point
      // We're building a chains here from left to right
      // Not from right to left as you can see in the picture with Gophers
      // Don't be fooled by arrows direction
      for i := 0; i < n; i++ {
      right = make(chan int)
      go f(left, right)
      left = right
      }
      // I rewrite this code a little bit
      // It's clearer for me than the Rob's code
      // Just put a 0 to the rightmost channel to start the whole process
      go func() {
      right

  • @brionl9377
    @brionl9377 5 років тому +2

    The slides link
    talks.golang.org/2012/concurrency.slide#1

  • @shturm681
    @shturm681 10 років тому +1

    Looks cool, but I still don't get it - how are channels all that much different from threading ? What's the advantage over threads?

    • @guild_navigator
      @guild_navigator 8 років тому +3

      +Alex George tleyden.github.io/blog/2014/10/30/goroutines-vs-threads/

    • @matpsycic
      @matpsycic 7 років тому

      Alex George All concurrency implementation problems arise from threads accessing shared memory at the same time. Go takes a different approach by passing around memory to channels therefore providing implicit locking in a sense.

    • @VitalyZdanevich
      @VitalyZdanevich 3 роки тому

      Goroutines are lighter than threads.

  • @vijaykumarsubramainyan8694
    @vijaykumarsubramainyan8694 8 років тому

    where is boring func in FanIn program,give me a clear program to understand.pls tis a wonderful in golang

  • @allyourcode
    @allyourcode 3 роки тому

    @19:32 I do not see the point of these wait channels. If you want to alternate between the two original channels, don't be using fanIn to mix things up in the first place!

  • @ashisranjandey635
    @ashisranjandey635 7 місяців тому

    The daisy chain is confusing. how its giving 100001.
    Otherwise awesome tutorial.

  • @elimisteve
    @elimisteve 11 років тому

    Here's the URL: rspace googlecode com/hg/slide/concur.html#title-slide

  • @MaksymMinenko
    @MaksymMinenko 2 роки тому

    So, where's the code?

  • @powertube5671
    @powertube5671 6 років тому

    This reminds me of fork() and exec() calls to UNIX.

  • @MagnusKwan
    @MagnusKwan 11 років тому

    Not sure did watch it yet.
    The Talk is posted (after a year)
    blog.golang.org/2013/01/concurrency-is-not-parallelism.html

  • @RandomChatterRoom
    @RandomChatterRoom 5 років тому

    Where can I find the ppt of it?

  • @davidfurminieux4357
    @davidfurminieux4357 10 років тому +3

    erlang without erlang

  • @peterhansen4935
    @peterhansen4935 4 роки тому

    FYI: The example here ua-cam.com/video/f6kdp27TYZs/v-deo.html does not work, runs forever. Had to refactor to use this: timeChannel := time.After(1 * time.Second). Go version: 1.14

    • @peterhansen4935
      @peterhansen4935 4 роки тому

      Or a local problem time.After(900 * time.Millisecond), works

  • @grutten
    @grutten 14 днів тому

    Is there any one watching this video in 2024 just like me

  • @babuOOabc
    @babuOOabc 8 років тому

    I think you better do what you have all the characteristics of all programming languages only it is activated and enable des modes
    because by eliminating pointer index it increases security but loses dynamic processes ingenious little suspect. better have all the features including haskel but the default is to show and recommend, is concerned activate other types of programming to be even more popular and more free ... !!!

    • @babuOOabc
      @babuOOabc 8 років тому

      +babu OO creo que lo mejor es hacer lo que tenga todas las características de todos los lenguajes de programación solo que se activa y se des activa las modalidades
      porque al eliminar los el indice de puntero aumenta la seguridad pero pierde dinamismo de procesos ingeniosos poco sospechados. mejor que tenga todas la características incluyendo haskel pero el por defecto es la que muestran y recomiendan, es cuestión activar las otras modalidades de programacion para que sea mas popular todavia y mas libre...!!!

  • @brandonchapman3110
    @brandonchapman3110 Рік тому

    This video confused the hell out of me. So many assumptions and skipping steps. I’m left with at least an order of magnitude more questions than when I came in.

  • @user-dg6re6lh8h
    @user-dg6re6lh8h 6 місяців тому

    我是搞Java的英语贼差竟然能听懂,哈哈哈

  • @v1Broadcaster
    @v1Broadcaster 7 років тому

    ur url is broked dood

  • @omuleanu
    @omuleanu 11 років тому

    looks like .net async

  • @shiyuwang
    @shiyuwang 8 років тому

    到此一游

  • @youssefayachichaine
    @youssefayachichaine 9 років тому +2

    this is a c++ modified

    • @___-ls9ly
      @___-ls9ly 7 років тому +12

      its more like c modified.

  • @sooqua
    @sooqua 7 років тому

    Andrei Chikatilo?

  • @zaccarter5110
    @zaccarter5110 7 років тому

    is Go mostly dead now?

    • @___-ls9ly
      @___-ls9ly 7 років тому +7

      Nope, its still super popular. And its popularity is still going up. It jumped from 54th to 13th in a year on the TIOBE index

    • @Eyenagel
      @Eyenagel 7 років тому +3

      lol, every single startup that you can find anywhere in europe and us are using go if not node.js . It's really popular for backend related tasks & systems programming. Because it's simple to read yet has all the tools to express something really complex.

  • @NickolayKharchevyn
    @NickolayKharchevyn 6 років тому

    So boring...

  • @MrMartingale1
    @MrMartingale1 9 років тому

    Try Go
    // You can edit this code!
    // Click here and start typing.
    package main
    import "fmt"
    func main() {
    fmt.Println("Hello, world")
    }
    Same shit in Python : print 'Hello, world'
    yeah thanks ill stick with python

    • @maxdewaele9660
      @maxdewaele9660 8 років тому +3

      Now try doing the channels example in Python

    • @XiphosPL
      @XiphosPL 8 років тому

      +koda kitty most of them would be trivial to do with asyncio

    • @___-ls9ly
      @___-ls9ly 7 років тому +5

      If you are judging a whole language based on hello world you are a idiot.

    • @konstantinwirz
      @konstantinwirz 2 роки тому

      also have fun with GIL

  • @alaubolatov9422
    @alaubolatov9422 8 років тому +5

    Sorry, but C# and Java are better

    • @___-ls9ly
      @___-ls9ly 7 років тому +4

      Go is a lot faster. By a very large margin.

    • @AtanasovPetar
      @AtanasovPetar 7 років тому +3

      Go is a lower level language which actually requires you to know programming, if you want to do stuff.
      C# and Java is high level programming which compiles as ugly buggy crap.

    • @___-ls9ly
      @___-ls9ly 7 років тому +3

      saying c# and java compile into "buggy crap" is pretty ignorant. You would be hard pressed to find an issue caused by compiling in c# or java. The issue most people have is with the garbage collection (go is also garbage collected by the way) being slow and the fact that memory management is mostly abstracted away.
      As always though, a language is just a tool, and no one tool is universally the best. There are use cases where using c# (mainly for the .net stuff) or java is better than using go.

    • @AtanasovPetar
      @AtanasovPetar 7 років тому

      [___ ] compare code generated by go and by Java. if you don't believe me, look what Linus Tordsvalds says.

    • @user-mi8dq6sy4p
      @user-mi8dq6sy4p 7 років тому +3

      There is the perfect language. And its name is Haskell. This language has an almost human AI and it writes code by itself. But the Dark Government is trying hard to keep this language in secret.

  • @zerochannel5797
    @zerochannel5797 2 роки тому

    waitForIt = make(chan bool) Shared between all messages(struct), so all goroutines will wait for bool message from "waiForIt chan" after finishing its own tasks. Then if the main function sends a bool to the shared "waiForIt" channel, which goroutine will get the bool first? it still could keep the sequence in order?