際際滷

際際滷Share a Scribd company logo
Runtime & Concurrency
in Go
V N Nikhil Anurag
GopherCon Singapore 2017
Agenda
Concurrency & Parallelism
Lifecycle of Binaries
Coroutines & Green Threads
Goroutines
Pop Quiz
Concurrency & Parallelism
Concurrency Parallelism
Concurrency is about dealing with
lots of things at once.*
Parallelism is about doing lots of
things at once.*
* https://blog.golang.org/concurrency-is-not-parallelism
Lifecycle of Binaries
Lifcycle
Of
Binaries
Lifecycle of Interpreted Languages
Lifecycle of a Go Binary
Lifecycle of Interpreted Languages (VM)
Single Executable
Virtual
Machine
Byte Code
Lifecycle of Interpreted Languages (VM)
(VM + Application Code) * No. of Workers
Virtual
Machine
Byte Code
Virtual
Machine
Byte Code
Virtual
Machine
Byte Code
Lifecycle of Interpreted Languages (VM)
(VM + Application Code) * No. of Workers
This is required because...
Virtual
Machine
Byte Code
Virtual
Machine
Byte Code
Virtual
Machine
Byte Code
Global Interpreter Lock
Task to Run
GIL
Func 1 Func 2 Func 3 Func 4
Task
1
Task
2
Task
4
Task
3
?
A Go Binary
Program
OS
Architecture
Runtime
A Go Binary
What about multiple requests?
Program
OS
Architecture
Runtime
?We don¨t need no Multiple Instances?
A Go Binary
Program
OS
Architecture
Runtime
Coroutines & Green Threads
No one likes Threads.
Coroutines
Coroutines
Event Loop
Components of a Coroutine
Life of a Coroutine
Coroutines
EVENT LOOP
Coroutines
Task
1
Task
2
Task
6
Task
4
Task
10
EVENT LOOP
Components of Coroutine
Coroutines
?
??
Components of Coroutine
Coroutines
Yield
??
Components of Coroutine
Coroutines
Yield
?Await
Components of Coroutine
Coroutines
Yield
Await Result
Components of Coroutine
Life of a Coroutine
Coroutines
Coroutines
〔
Coroutines
〔
〔
Coroutines
〔
〔
Coroutines
〔
〔
Coroutines
〔
〔
〔
Coroutines
〔
〔
〔
X
Coroutines
〔
〔
〔
X 〔
Coroutines
〔
〔
〔
X 〔
Coroutines
〔
〔
〔
X 〔
〔
Coroutines
Coroutines
〔
〔
〔
〔
〔
〔
Coroutines - Python
Green Threads
They are a lot like Coroutines
Green Threads
THREAD 1
Green Threads
THREAD 1 THREAD 2
Green Threads
CORE 1
THREAD 1
CORE 1
THREAD 2
CORE 2?
THREAD 1?
Threads implemented by Runtime (and across Cores?)
Parallelism(?) by Runtime
Green Threads
Spawned by Runtime
Concurrency handled by User
Green Threads - Python (greenlet)
Goroutines
Goroutines
Goroutines
THREAD 1 THREAD 2
Goroutines
Goroutines
Goroutines
???
Goroutines
Goroutines
Spawned by Runtime
Parallelism &
execution by Runtime
Goroutines
Spawned by Runtime
Implemented across Threads
& Core
Parallelism &
execution by Runtime
Goroutines
Spawned by Runtime
Implemented across Threads
& Core
Parallelism &
execution by Runtime
Concurrency handled by User
Goroutines
Spawned by Runtime
Goroutines
Pop Quiz
Goroutine Pop Quiz
Q: Since they are running across
multiple threads, does failure in one
goroutine halt the program?
Goroutine Pop Quiz
Q: Since they are running across
multiple threads, does failure in one
goroutine halt the program?
YES!
Goroutine Pop Quiz
Q: Will running CPU intensive tasks in
goroutines block them?
Goroutine Pop Quiz
Q: Will running CPU intensive tasks in
goroutines block them?
MAYBE?
I/O Bound == Concurrency
CPU Bound == Parallelism
Goroutines:
Coroutines + Green Threads
+ Much Much More!
? Twitter - @last_ent
? Github C github.com/last-ent
Runtime & Concurrency in go slides

More Related Content

Runtime & Concurrency in go slides