Slide 1

Slide 1 text

(and Elixir) for Rubyists

Slide 2

Slide 2 text

Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

BEAM OTP Process Node Message spawn

Slide 10

Slide 10 text

Operating System Operating System BEAM stdlib OTP Rails MRI ERTS STDLIB kernel Erlang Elixir JavaScript CoffeeScript Ruby Erlang/OTP

Slide 11

Slide 11 text

OS Process Erlang VM Erlang Node NOT Erlang Process Erlang Process Actor light-weight Thread NOT OS Process

Slide 12

Slide 12 text

Heap Mailbox Stack

Slide 13

Slide 13 text

**********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" **********" One$.NET$4.0$Thread$$ (allocates$one$megabyte)$ One$Erlang$Process$(allocates$one$kilobyte)$ *$ @bryan_hunter

Slide 14

Slide 14 text

Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack

Slide 15

Slide 15 text

Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Message Heap Mailbox Stack

Slide 16

Slide 16 text

Heap Mailbox Stack Message

Slide 17

Slide 17 text

Mailbox Message 1 Message 2 Message 3 calculate something send message to P1 send message to P2 wait for incoming message calculate something send message to P1 wait for incoming message

Slide 18

Slide 18 text

wait for incoming message handle message by sending more messages and then waiting for answers from other processes send an answer message back

Slide 19

Slide 19 text

Who sends/receives messages? •other processes (obviously) •open files •open sockets •external programs (Ports) •Erlang VM notifications •a process you watched died •a remote node you watched died

Slide 20

Slide 20 text

Who sends/receives messages? •open files •“write this” •“read 15 bytes” -> “here are 15 bytes” •open sockets •“write this” •“some bytes just arrived” •external programs (Ports) •STDOUT/STDIN/STDERR

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

require  'actor' pong  =  nil ping  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000        pong  <<  (count  +  1)    end end pong  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000                ping  <<  (count  +  1)    end end ping  <<  1 sleep  1

Slide 23

Slide 23 text

defmodule  PingPong  do    def  start  do        :ok  =  :proc_lib.start_link(__MODULE__,  :init,  [:ping,  :pong])        :ok  =  :proc_lib.start_link(__MODULE__,  :init,  [:pong,  :ping])        :ping  <-­‐  1    end      def  init(my_name,  other_name)  do        Process.register(self(),  my_name)        :proc_lib.init_ack(:ok)        loop(my_name,  other_name)    end      def  loop(my_name,  other_name)  do        receive  do            count  -­‐>                other_name  <-­‐  (count  +  1)                if  count  >  1000  do                    IO.puts("#{my_name}  #{count}")                else                    loop(my_name,  other_name)                end        end    end end

Slide 24

Slide 24 text

-­‐module(pingpong). -­‐export([start/0,  init/2]).   start()  -­‐>    ok  =  proc_lib:start_link(?MODULE,  init,  [ping,  pong]),    ok  =  proc_lib:start_link(?MODULE,  init,  [pong,  ping]),    ping  !  1.   init(MyName,  OtherName)  -­‐>    true  =  erlang:register(MyName,  self()),    proc_lib:init_ack(ok),    loop(MyName,  OtherName).   loop(MyName,  OtherName)  -­‐>    receive        Count  -­‐>            OtherName  !  (Count  +  1),            case  Count  >  1000  of                true  -­‐>                    io:fwrite("~p  ~p~n",  [MyName,  Count]);                false  -­‐>                    loop(MyName,  OtherName)            end    end.

Slide 25

Slide 25 text

require  'actor' pong  =  nil ping  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000        pong  <<  (count  +  1)    end end pong  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000                ping  <<  (count  +  1)    end end ping  <<  1 sleep  1

Slide 26

Slide 26 text

require  'actor' pong  =  nil ping  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000        pong  <<  (count  +  1)    end end pong  =  Actor.spawn  do    loop  do        count  =  Actor.receive        break  puts(count)  if  count  >  1000        BCrypt::Password.create('secret',  cost:  50)  #  <-­‐-­‐  oh  well        ping  <<  (count  +  1)    end end ping  <<  1 sleep  1

Slide 27

Slide 27 text

calculate(something) calculate(something) calculate(something) calculate(something) calculate(something) calculate(something) calculate(something)

Slide 28

Slide 28 text

2000 reductions left: calculate(something) 1000 reductions left: still calculating(something) 0 reductions left: suspend process run next process

Slide 29

Slide 29 text

2000 reductions left: calculate(something) 1000 reductions left: still calculating(something) 0 reductions left: suspend process run next process green threads = cheap context switching

Slide 30

Slide 30 text

Supervisors

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

OTP Behaviours gen_server gen_fsm gen_event

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Conn 1 Conn 2 Conn 3 Conn 4 Conn 5 Player 1 Player 2 Player 3 Player 4 Player 5 Table 1 Table 2 Game 1 Game 5

Slide 36

Slide 36 text

BERT Ernie bert-rpc.org

Slide 37

Slide 37 text

AT'SCALE'WITH'STYLE Erlang'<3'<3'<3'Ruby Mar$n&Rehfeld,&@klickmich

Slide 38

Slide 38 text

Distributed Erlang

Slide 39

Slide 39 text

Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack Heap Mailbox Stack

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

speakerrate.com/talks/26471 @MSch [email protected] APOCH meetup.com/Vienna-BEAMers