class: center, middle # Concurrency with Golang ### Nate Brennand ### ADI  --- # Agenda - Concurrency - Parallelism - Asynchronous - Threads - Golang --- layout: false .left-column[ ## Concurrency ] .right-column[ __Concurrent__ programs can have multiple computations executing during overlapping periods of time. This is opposed to __sequential__ programs in which no computations can be executed in overlapping periods of time. ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ] .right-column[ Generally speaking, concurrency can be useful when there is large amounts of computation or when there is input/output: - data analysis - webservers - filesystems - web browsers - smartphones - etc ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ] .right-column[ ### We're going to focus on two ways you can execute your programs concurrently ### - asynchronous ### - parallel ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ] .right-column[ So imagine we have a ton of bricks...  ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ] .right-column[ So imagine we have a ton of bricks...  ... and we need to get them all to the oven to be fired ...  ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ] .right-column[ So imagine we have a ton of bricks...  ... and we need to get them all to the oven to be fired ...  ... thankfully we have a bunch of gophers to help!  ] -- .right-column[  ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ### Parallelism ] .right-column[ ## Parallel Execution Parallel execution is when your code takes advantage of each of your processors to make computations on each of them in parallel. This is very useful in CPU-bound tasks as you are utilizing each core in your processor. ] -- .right-column[  Think of each oven as a core in your CPU. ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ### Parallelism ] .right-column[ ## Parallel Execution Parallel execution is when your code takes advantage of each of your CPU to make computations on each of them in parallel. This is very useful in CPU-bound tasks as you are utilizing each processor.  ### Is parallel always faster? ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ### Parallelism ] .right-column[ ## Parallel Execution Parallel execution is when your code takes advantage of each of your CPU to make computations on each of them in parallel. This is very useful in CPU-bound tasks as you are utilizing each processor.  ### Is parallel always faster? No, in tasks that are IO-bound, the added time to communicate between the cores in your CPU(s) can eliminate any advantage that spreading the work gained. ] --- layout: false .left-column[ ## Concurrency ### When is this useful? ### Types of concurrency ### Parallelism ### Async ] .right-column[ ## Asynchronous Execution An asynchronous function call is a call that executes in the background. Asynchronous functions are useful in IO-bound tasks since ] -- .right-column[  Imagine that every time you call `fire_bricks()` a gopher is sent to do your work! You can call the function repeatedly and gophers will be sent to work at the same time. However, you're limited by how many gophers can use the oven at once. ] --- # So why have we been talking about gophers this whole time? -- Gophers are the mascot of Go(lang), a statically typed language developed by Google. Go is special in that there are concurrency primitives. This makes it very simple to make your programs asynchronous or parallel. --- ## Concurrency Primitives Golang is special in that you conccurency operators are primitives built into the language, not libraries you import. - `go` is a keyword that creates a go-routine (like a thread). - functions called with `go` are executed in the background -- ```go func two() { time.Sleep(time.Second) print("2 ") } print("1 ") go two() print("3 ") ``` So what should this print? -- ### `1 3 2` -- Check the link if you don't believe me! http://play.golang.org/p/m_LXVQhlru --- ## Concurrency Primitives - `chan` is a channel (like a queue). - Channels are used like queues, you can pass values in and take values out - All operations are threadsafe. - Channels must have a type -- ```go func sayHi(msgChan chan string) { msgChan <- "Hello" } // create a channel for strings messageChan := make(chan string) // call a function asynchronously go sayHi(messageChan) // wait for a return value // this will block until a string is sent into messageChan msg :=<- messageChan ``` --- layout: false # How does Go parallelize? -- The Golang runtime spreads the work of each go-routine between multiple threads. Currently you must tell the runtime if you wish to utilize multiple cores (this is on the roadmap to be abstracted away). ```go // enabling parallelism in Go import "runtime" runtime.GOMAXPROCS(runtime.NumCPU()) ``` --- layout: false .left-column[ ## Golang ] .right-column[ ## A quick intro to Go We're not going to do an overview of all the syntax, you can easily learn that on your own. ] -- .right-column[ # Why should I try go? ] -- .right-column[ - It's simple - You'll learn almost the entire language by going through the tour - It's easy to write - It's Fast - Runs close to C speed - Also, the compiler is blazing - Tools ] --- layout: false .left-column[ ## Golang ### Tools ] .right-column[ Despite it's young age, Golang has one of the most mature toolsets for development. This makes writing Go a pleasure. - Compile - Since it's so fast, it's common to compile on save - This makes your feedback loop very fast - Build - `go build` will intelligently find all dependencies as well as download them if necessary - Formatting - All Go code is formatted the same way - `go fmt` - Dependency management - Simply `go get
` to download and install - Installation - `go install
` - Want to install Ubuntu's enormous 23K commit devops system? - `go install -v github.com/juju/juju/...` - [Profiling](http://benchgraffiti.googlecode.com/hg/havlak/havlak1.svg) - Plus many community built tools ] --- ## Now lets move on to some code # http://bit.ly/adi_concurrency ### https://github.com/natebrennand/concurrency_and_golang/
If you wish to test out anything in Go without installing it: ## http://play.golang.org/