Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring Processes and OTP behaviours in Erlang

Exploring Processes and OTP behaviours in Erlang

Dive into Erlang processes and the most used OTP behaviours

Paolo D'Incau

October 17, 2015
Tweet

More Decks by Paolo D'Incau

Other Decks in Programming

Transcript

  1. Exploring Processes and OTP
    behaviours in
    17 ottobre
    Erlang - Elixir meetup Bologna

    View Slide

  2. @pdincau

    View Slide

  3. Funzionale
    General purpose
    Dinamico
    Concorrente
    Garbage collected
    - 1 -
    Erlang Run-Time System
    Ideato da Ericsson per la necessità di Business reali

    View Slide

  4. Soluzione ai problemi odierni e futuri
    Programmare diversamente
    - 2 -
    PERCHÈ USARLO?

    View Slide

  5. “If we want to write programs that behave as other objects behave in the
    real world, then these programs will have a concurrent structure.
    Use a language that was designed for writing concurrent applications,
    and development becomes a lot easier.
    Erlang programs model how we think and interact.”
    Joe Armstrong
    Programming Erlang - 1st Edition
    - 3 -

    View Slide

  6. - 4 -
    Moduli o Classi
    “It’s OO, but pretend it isn’t, please”
    Fred Hebert
    Joe Armstrong / Ralph Johnson
    infoq.com/interviews/johnson-armstrong-oop
    ferd.ca/an-open-letter-to-the-erlang-beginner-or-onlooker.html
    VENITE DAL MONDO OOP?

    View Slide

  7. - 5 -
    Actor Model
    TASK = fun(message)
    http:/
    /learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency
    LA REALTÀ

    View Slide

  8. - 6 -
    Green Threads e message passing
    - 6 -
    - 6 -
    PROCESSI

    View Slide

  9. - 7 -
    UN ESEMPIO PRATICO?

    View Slide

  10. - 8 -
    P1 P2
    P2 ! {increment, N}
    COUNTER SERVER

    View Slide

  11. - 9 -
    COUNTER CODICE

    View Slide

  12. P1 P2
    P2 ! {self(), {increment, N}}
    - 10 -
    P1 ! {Pid2, Result}
    CHIAMATA SINCRONA

    View Slide

  13. - 11 -
    COUNTER MIGLIORATO

    View Slide

  14. P1
    P2
    m1
    P3
    P4
    P5
    m2
    m3
    m4
    - 12 -
    COUNTERS

    View Slide

  15. - xx -
    PROVIAMO!

    View Slide

  16. P1
    P2
    P3
    P4
    - 13 -
    CRASH!

    View Slide

  17. - 14 -
    PRIMITIVE DI ERLANG
    spawn link spawn_link monitor

    View Slide

  18. NON COSÌ
    - 15 -

    View Slide

  19. {'EXIT', From, Reason}
    crash!
    crash!
    crash!
    - 16 -
    CRASH! CRASH! CHRASH!
    P1
    P2
    P3
    P4

    View Slide

  20. trap_exit
    - 17 -

    View Slide

  21. “…OTP, the 800-pound gorilla
    of process architecture”
    Dave Thomas
    Programming Elixir
    - 18 -

    View Slide

  22. Librerie con pattern comuni che ci consentono di
    sviluppare applicazioni:
    - xx -
    DISTRIBUITE e FAULT TOLERANT
    - 18 -

    View Slide

  23. - 19 -
    Modella il server di una relazione di tipo client-server
    Server
    Client
    Client
    Client Query
    Reply
    http:/
    /www.erlang.org/doc/design_principles/gen_server_concepts.html
    GEN_SERVER

    View Slide

  24. - 20 -
    GEN_SERVER

    View Slide

  25. - 21 -
    GEN_SERVER

    View Slide

  26. - 22 -
    State(S) x Event(E) -> Actions(A), State(S')
    Lobby Idle
    Disconnected
    Reconnected
    timeout
    Stop
    GEN_FSM

    View Slide

  27. Wander Pursue
    Act
    Eat
    Act Act
    Act
    Act
    Act
    Act
    - 23 -
    GEN_FSM

    View Slide

  28. - 24 -
    GEN_EVENT
    server
    event
    manager
    g(event)
    f(event)
    h(event)
    event
    “Profiling and Debugging Erlang Systems”
    Martin Kjellin, Roberto Aloi
    youtube.com/watch?v=4u6c2FNauYE

    View Slide

  29. - 25 -
    SUPERVISOR
    Main
    Sup.
    GCM 2
    GCM 1
    Il cuore della affidabilità di Erlang

    View Slide

  30. - 26 -
    SUPERVISOR
    Sup.
    Main
    Sup.
    Seq.
    Worker
    Stash
    Worker
    “OTP: Supervisors”
    Programming Elixir
    Dave Thomas

    View Slide

  31. - 27 -
    APPLICATION
    Raccoglie codice comune a una funzionalità
    start/stop
    Organizzazione dei file
    Gestione delle configurazioni
    Riutilizzabile

    View Slide

  32. - 28 -
    RELEASE
    Una "release" è un insieme di applicazioni Erlang
    deployate ed eseguite assieme
    rebar3 è un grande passo avanti
    Gestione configurazioni

    View Slide

  33. - 29 -
    Erlang in production: "I wish I'd known that when I started"
    Bernard Duggan
    youtube.com/watch?v=G0eBDWigORY
    Thinking like an Erlanger
    Torben Hoffmann
    youtube.com/watch?v=_fgaPGLGZI0
    RISORSE UTILI

    View Slide

  34. Grazie

    View Slide