Elixir – Peeking into Elixir's Processes, OTP and Supervisors

4,853 views

Published on

Published in: Technology, News & Politics

Elixir – Peeking into Elixir's Processes, OTP and Supervisors

  1. 1. ElixirPeeking into Elixir’s Processes, OTP & Supervisors 21st March 2014 /benjamintanweihao /bentanweihao benjamintanweihao@gmail.com
  2. 2. What will we learn today? Elixir & Erlang In less than 5 minutes
  3. 3. Elixir & Erlang In less than 5 minutes Processes 101 The Basic Concurrency Primitive What will we learn today?
  4. 4. Elixir & Erlang In less than 5 minutes OTP Framework and much more Processes 101 The Basic Concurrency Primitive What will we learn today?
  5. 5. 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?
  6. 6. Ohai, Elixir!
  7. 7. Elixir & ErlangIn less than 5 minutes
  8. 8. 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.
  9. 9. 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.
  10. 10. • Free lunch is over Why Elixir ?
  11. 11. Why Elixir ? • Free lunch is over • Hyper-threading & Multicore • Faster software means using all cores! • But … Concurrency -> Coordination • Functional makes this easier
  12. 12. Design Goals of Elixir 1.Productivity 2.Extensibility 3.Compatibility
  13. 13. Productivity Complete Elixir Application
  14. 14. Productivity Includes Supervisor Chain
  15. 15. Productivity Testing built-in
  16. 16. Extensibility Macros & Meta-programming Implementing unless using if
  17. 17. Extensibility Macros & Meta-programming Implementing unless using if
  18. 18. Compatibility Elixir can call Erlang code, without any conversion cost at all.
  19. 19. Compatibility Elixir can use Erlang libraries!
  20. 20. The Actor Concurrency Model • Actor = Process
  21. 21. The Actor Concurrency Model • Actor = Process • A process performs a specific task when it receives a message
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. Processes 101The Basic Concurrency Primitive
  26. 26. Creating Processes & Sending Messages in Elixir Creating a Process
  27. 27. Creating Processes & Sending Messages in Elixir Module, Function, Arguments
  28. 28. Creating Processes & Sending Messages in Elixir Process id
  29. 29. Creating Processes & Sending Messages in Elixir Sending a Message to w1
  30. 30. Creating Processes & Sending Messages in Elixir Process waits for a message …
  31. 31. Creating Processes & Sending Messages in Elixir Pattern matches!
  32. 32. Creating Processes & Sending Messages in Elixir Result is sent back to the calling process (self)
  33. 33. Creating Processes & Sending Messages in Elixir Returns immediately
  34. 34. Creating Processes & Sending Messages in Elixir Get result from self
  35. 35. Creating Processes & Sending Messages in Elixir Demo
  36. 36. OTPFramework and much more
  37. 37. What is OTP? • Comes with Elixir/Erlang • Framework to build applications that are fault- tolerant, scalable, distributed • Databases + Profilers + Debuggers
  38. 38. OTP Behaviours • GenServer • Supervisor • Application
  39. 39. An Example GenServer Implement the GenServer Behaviour
  40. 40. An Example GenServer Implement GenServer Callbacks
  41. 41. An Example GenServer Callbacks are NOT called explicitly
  42. 42. An Example GenServer OTP calls the callbacks.
  43. 43. An Example GenServer Synchronous Call: Caller waits for reply
  44. 44. An Example GenServer Asynchronous Call: Caller doesn’t wait for reply
  45. 45. An Example GenServer Demo
  46. 46. SupervisorsFault Tolerance & Recovery
  47. 47. Supervisors for Fault Tolerance and Recovery one_for_one restart strategy
  48. 48. Supervisors for Fault Tolerance and Recovery rest_for_all restart strategy
  49. 49. Supervisors for Fault Tolerance and Recovery rest_for_one restart strategy
  50. 50. An Example Supervisor Implement the Supervisor Behaviour
  51. 51. An Example Supervisor Declaring the Supervision tree. Both Supervisors and Workers (e.g. GenServers) can be supervised.
  52. 52. An Example Supervisor
  53. 53. An Example Supervisor Declare the restart strategy
  54. 54. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1Worker 1Worker
  55. 55. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1Worker 1Worker one_for_one
  56. 56. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1Worker 1Worker one_for_all
  57. 57. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1Worker 1Worker simple_one_for_one
  58. 58. Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1Worker 1Worker one_for_one
  59. 59. Supervisor Demo Supervisor 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
  60. 60. Elixir & Erlang In less than 5 minutes OTP Framework and much more Supervisors Fault Tolerance & Recovery Processes 101 The Basic Concurrency Primitive
  61. 61. Resources
  62. 62. Work in Progress! ! Sign up at: http://www.exotpbook.com Me.
  63. 63. Stickers!**Very Limited Quantity
  64. 64. Thanks! /benjamintanweihao /bentanweihao benjamintanweihao@gmail.com

×