Elixir Processes

16
PROCESSES ELIXIR

Transcript of Elixir Processes

Page 1: Elixir Processes

PROCESSESELIXIR

Page 2: Elixir Processes

OVERVIEW

TODAY’S AGENDA - PROCESSES▸ Why processes?▸ Conceptual framework▸ Basic usage▸ Next steps

Page 3: Elixir Processes

ELIXIR PROCESSES

WHY PROCESSES?▸ Need more throughput▸ Run parallel computations▸ Concurrency causes problems▸ Control of failure

Page 4: Elixir Processes

GIVEN A LIST OF CITIESAND A WEATHER APIRETRIEVE THE WEATHERFOR EACH CITY

User Story

Page 5: Elixir Processes

PROCESSES OVERVIEW▸ Not a server process or thread

▸ A process in the Erlang VM (BEAM)

▸ Lightweight

▸ Utilizes all cores

ELIXIR PROCESSES

Page 6: Elixir Processes

ELIXIR PROCESSES▸ send and receive messages▸ unique process id▸ message mailbox▸ execute asynchronously▸ isolated - no shared memory▸ hold state

ELIXIR PROCESSES

Page 7: Elixir Processes

DRAW IT OUTPROCESS INTERACTIONS CAN BE DIFFICULT TO REASON ABOUT

A SIMPLE DIAGRAM CAN MAKE IT MUCH EASIER

Page 8: Elixir Processes

GIVEN A LIST OF CITIESAND A WEATHER APIRETRIEVE THE WEATHERFOR EACH CITY

User Story

Page 9: Elixir Processes

ELIXIR PROCESSES VS RUBY OBJECTS▸ #spawn #new

▸ process id instance

▸ #send/#receive .

▸ has state has state

▸ isolated memory shared memory

▸ asynchronous synchronous

Page 10: Elixir Processes

live coding - spawn, send, receive

Page 11: Elixir Processes

GIVEN A LIST OF CITIESAND A WEATHER APIRETRIEVE THE WEATHERFOR EACH CITY

User Story

Page 12: Elixir Processes

ELIXIR PROCESSES

LINKING PROCESSES▸ Link processes with #spawn_link▸ Linking creates a parent-child relationship▸ Child exits by default cause the parent to exit▸ Exits can be “trapped” by a parent▸ Trapped exits let parents make a decision

Page 13: Elixir Processes

live coding - linking processes and trapping exits

Page 14: Elixir Processes

ELIXIR PROCESSES

WHAT HAVE WE GAINED?▸ More throughput▸ Easy, parallel computations▸ Avoidance of key concurrency problems▸ Failure control

Page 15: Elixir Processes

ELIXIR PROCESSES

OTP IS NEXT▸ "Open Telecom Platform" is an

outdated name

▸ Let's just call it OTP

▸ A set of tools and abstractions to manage BEAM processes

Page 16: Elixir Processes

‣ User story solution: https://github.com/adsteel/metex

‣ Live coding examples: https://gist.github.com/adsteel/3fb26673edc6892583c9fed588452d25

‣ Elixir Process Ping Pong - http://eddwardo.github.io/elixir/2015/10/22/elixir-pingpong-table/

‣ Trapping exits - http://www.akitaonrails.com/2015/11/22/observing-processes-in-elixir-the-little-elixir-otp-guidebook

‣ The Little Elixir and OTP Guidebook, by Benjamin Tan Wei Hao

‣ The Actor Model - https://www.youtube.com/watch?v=7erJ1DV_Tlo

Additional Resources