elixir – peeking into elixir's processes, otp and supervisors
TRANSCRIPT
![Page 1: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/1.jpg)
Elixir
Peeking into Elixir’s
Processes, OTP & Supervisors 21st March 2014
/benjamintanweihao /bentanweihao
![Page 2: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/2.jpg)
What will we learn today?
Elixir & Erlang
In less than 5 minutes
![Page 3: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/3.jpg)
Elixir & Erlang
In less than 5 minutesProcesses 101
The Basic Concurrency Primitive
What will we learn today?
![Page 4: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/4.jpg)
Elixir & Erlang
In less than 5 minutes
OTP Framework and much more
Processes 101 The Basic Concurrency
Primitive
What will we learn today?
![Page 5: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/5.jpg)
Elixir & Erlang
In less than 5 minutes
OTP Framework and much more
Supervisors Fault Tolerance & Recovery
Processes 101 The Basic Concurrency
Primitive
What will we learn today?
![Page 6: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/6.jpg)
Ohai, Elixir!
![Page 7: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/7.jpg)
Elixir & Erlang
In less than 5 minutes
![Page 8: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/8.jpg)
Ohai, Erlang!Erlang is a general-purpose concurrent, garbage-collected programming language and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non-stop applications. It supports hot swapping, so that code can be changed without stopping a system.
![Page 9: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/9.jpg)
Ohai, Erlang!Erlang is a general-purpose concurrent, garbage-collected programming language and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non-stop applications. It supports hot swapping, so that code can be changed without stopping a system.
![Page 10: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/10.jpg)
• Free lunch is over
Why Elixir ?
![Page 11: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/11.jpg)
Why Elixir ?• Free lunch is over • Hyper-threading & Multicore • Faster software means using all
cores! • But … Concurrency -> Coordination • Functional makes this easier
![Page 12: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/12.jpg)
Design Goals of Elixir
1.Productivity
2.Extensibility
3.Compatibility
![Page 13: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/13.jpg)
Productivity
Complete Elixir Application
![Page 14: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/14.jpg)
Productivity
Includes Supervisor Chain
![Page 15: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/15.jpg)
Productivity
Testing built-in
![Page 16: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/16.jpg)
ExtensibilityMacros & Meta-programming
Implementing unless using if
![Page 17: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/17.jpg)
ExtensibilityMacros & Meta-programming
Implementing unless using if
![Page 18: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/18.jpg)
Compatibility
Elixir can call Erlang code, without any conversion cost at all.
![Page 19: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/19.jpg)
Compatibility
Elixir can use Erlang libraries!
![Page 20: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/20.jpg)
The Actor Concurrency Model
• Actor = Process
![Page 21: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/21.jpg)
The Actor Concurrency Model
• Actor = Process • A process performs a specific task
when it receives a message
![Page 22: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/22.jpg)
The Actor Concurrency Model
• Actor = Process • A process performs a specific task
when it receives a message • In turn, the process can reply to the
sender
![Page 23: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/23.jpg)
The Actor Concurrency Model
• Actor = Process • A process performs a specific task
when it receives a message • In turn, the process can reply to the
sender • All messages go to a processes’
mailbox – Q of unprocessed messages sent from other processes that are not yet consumed
![Page 24: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/24.jpg)
The Actor Concurrency Model
• Actor = Process • A process performs a specific task
when it receives a message • In turn, the process can reply to the
sender • All messages go to a processes’
mailbox – Q of unprocessed messages sent from other processes that are not yet consumed Shared-nothing Async Message-passing
![Page 25: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/25.jpg)
Processes 101 The Basic Concurrency Primitive
![Page 26: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/26.jpg)
Creating Processes & Sending Messages in Elixir
Creating a Process
![Page 27: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/27.jpg)
Creating Processes & Sending Messages in Elixir
Module, Function, Arguments
![Page 28: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/28.jpg)
Creating Processes & Sending Messages in Elixir
Process id
![Page 29: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/29.jpg)
Creating Processes & Sending Messages in Elixir
Sending a Message to w1
![Page 30: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/30.jpg)
Creating Processes & Sending Messages in Elixir
Process waits for a message …
![Page 31: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/31.jpg)
Creating Processes & Sending Messages in Elixir
Pattern matches!
![Page 32: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/32.jpg)
Creating Processes & Sending Messages in Elixir
Result is sent back to the calling process (self)
![Page 33: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/33.jpg)
Creating Processes & Sending Messages in Elixir
Returns immediately
![Page 34: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/34.jpg)
Creating Processes & Sending Messages in Elixir
Get result from self
![Page 35: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/35.jpg)
Creating Processes & Sending Messages in Elixir Demo
![Page 36: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/36.jpg)
OTP
Framework and much more
![Page 37: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/37.jpg)
What is OTP?• Comes with Elixir/Erlang • Framework to build
applications that are fault-tolerant, scalable, distributed
• Databases + Profilers + Debuggers
![Page 38: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/38.jpg)
OTP Behaviours
•GenServer • Supervisor •Application
![Page 39: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/39.jpg)
An Example GenServerImplement the GenServer Behaviour
![Page 40: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/40.jpg)
An Example GenServer
Implement GenServer Callbacks
![Page 41: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/41.jpg)
An Example GenServer
Callbacks are NOT called explicitly
![Page 42: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/42.jpg)
An Example GenServer
OTP calls the callbacks.
![Page 43: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/43.jpg)
An Example GenServer
Synchronous Call: Caller waits for reply
![Page 44: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/44.jpg)
An Example GenServer
Asynchronous Call: Caller doesn’t wait for reply
![Page 45: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/45.jpg)
An Example GenServer Demo
![Page 46: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/46.jpg)
Supervisors
Fault Tolerance & Recovery
![Page 47: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/47.jpg)
Supervisors for Fault Tolerance and Recovery
one_for_one restart strategy
![Page 48: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/48.jpg)
Supervisors for Fault Tolerance and Recovery
rest_for_all restart strategy
![Page 49: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/49.jpg)
Supervisors for Fault Tolerance and Recovery
rest_for_one restart strategy
![Page 50: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/50.jpg)
An Example SupervisorImplement the Supervisor Behaviour
![Page 51: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/51.jpg)
An Example Supervisor
Declaring the Supervision tree. Both Supervisors and Workers (e.g. GenServers) can be supervised.
![Page 52: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/52.jpg)
An Example Supervisor
![Page 53: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/53.jpg)
An Example Supervisor
Declare the restart strategy
![Page 54: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/54.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
![Page 55: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/55.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
one_for_one
![Page 56: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/56.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
one_for_all
![Page 57: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/57.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
simple_one_for_one
![Page 58: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/58.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
one_for_one
![Page 59: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/59.jpg)
Supervisor DemoSupervisor A
Supervisor B Supervisor C
Supervisor D
Server D
Server B
Worker 1Worker 1Worker
one_for_one
simple_one_for_one
one_for_all
one_for_one
![Page 60: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/60.jpg)
Elixir & Erlang
In less than 5 minutes
OTP Framework and much more
Supervisors Fault Tolerance & Recovery
Processes 101 The Basic Concurrency
Primitive
![Page 61: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/61.jpg)
Resources
![Page 62: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/62.jpg)
Work in Progress! !Sign up at: http://www.exotpbook.com
Me.
![Page 63: Elixir – Peeking into Elixir's Processes, OTP and Supervisors](https://reader034.vdocuments.site/reader034/viewer/2022042607/554f48beb4c905524c8b47e9/html5/thumbnails/63.jpg)
Stickers!**Very Limited Quantity