25
BY MICHEL PEREZ ELIXIR & PARALLEL PROGRAMMING

Elixir par

Embed Size (px)

Citation preview

Page 1: Elixir par

BY MICHEL PEREZELIXIR & PARALLEL PROGRAMMING

Page 2: Elixir par

CONCURRENCY VS PARALLELISM

Page 3: Elixir par

ELIXIR MECHANISMS

▸ PROCESS

▸ SPAWNING

▸ OTP

▸ GEN SERVER

▸ SUPERVISOR

▸ APPLICATION

Page 4: Elixir par

PROCESSES

▸ Processes are the fundamental unit of concurrency in Elixir

▸ The Erlang VM supports up to 134 million of processes

▸ Lightweight processes

▸ Each actor is a process

▸ Each process performs a specific task

▸ Sends messages to communicate with the process

▸ The processes don’t share information

Page 5: Elixir par

SPAWNING A PROCESS

Page 6: Elixir par

PID

▸ Identifies a process in the EVM

▸ To send a message must point the pid

Page 7: Elixir par

OTP

▸ The Erlang interpreter and compiler

▸ Erlang standard libraries

▸ Dialyzer, a static analysis tool

▸ Mnesia, a distributed database

▸ Erlang Term Storage (ETS), an in-memory database

▸ A debugger,

▸ An event tracer

▸ A release management tool

Page 8: Elixir par

OTP BEHAVIOURS

▸ GenServer A behaviour module for implementing the server of a client-server relation.

▸ Supervisor A behaviour module for implementing supervision functionality

▸ Application A module for working with applications and defining application callbacks.

Page 9: Elixir par

GEN SERVER - MODULE CALLS

▸ GenServer.start_link/3

▸ GenServer.call/3

▸ GenServer.cast/2

Page 10: Elixir par

GEN SERVER - CALLBACKS

▸ init(args)

▸ handle_call(msg, {from, ref}, state}

▸ handle_cast(msg, state}

▸ handle_info(msg, state)

▸ terminate(reason, state)

▸ code_change(old_vsn, state, extra)

Page 11: Elixir par

GENSERVER - INIT

▸ init(args)

▸ {:ok, state}

▸ {:ok, state, timeout}

▸ :ignore

▸ {:stop, reason}

Page 12: Elixir par

GENSERVER - HANDLE CALL

▸ handle_call(msg, {from, ref},state)

▸ {:reply, reply, state}

▸ {:reply, reply, state, timeout}

▸ {:reply, reply, state, :hibernate}

▸ {:noreply, state}

▸ {:noreply, state, timeout}

▸ {:noreply, state, hibernate}

▸ {:stop, reason, reply, state}

▸ {:stop, reason, state}

Page 13: Elixir par

GENSERVER - HANDLE CALL

▸ handle_cast(msg, state)

▸ {:noreply, state}

▸ {:noreply, state, timeout}

▸ {:noreply, state, :hibernate}

▸ {:stop, reason, state}

Page 14: Elixir par

GENSERVER - HANDLE INFO - TERMINATE

▸ handle_info(msg, state)

▸ {:noreply, state}

▸ {:noreply, state, timeout}

▸ {:stop, reason, state}

▸ terminate(reason, state)

▸ :ok

Page 15: Elixir par

GENSERVER - CODE CHANGE

▸ code_change(old_vsn, state, extra)

▸ {:ok, new_state}

▸ {:error, reason}

Page 16: Elixir par

GENSERVER - EXAMPLE

Page 17: Elixir par

SUPERVISOR

▸ Strategies

▸ :one_for_one

▸ :rest_for_one

▸ :one_for_all

▸ :simple_one_for_one

Page 18: Elixir par

SUPERVISOR - EXAMPLE

Page 19: Elixir par

APPLICATION

▸ Component implementing some specific functionality, that can be started and stopped as a unit, and which can be re-used in other systems

▸ Defines a supervision tree that must be started and stopped when the application starts and stops

▸ The start callback should return {:ok, pid}

Page 20: Elixir par

APPLICATION - EXAMPLE

Page 21: Elixir par

EXTREME

B1N1 N2

N3

N4

F1 N5

Page 22: Elixir par

EXSTREME - CREATING A GRAPH

Page 23: Elixir par

EXSTREME - RUNNING THE GRAPH

Page 24: Elixir par

EXSTREME - NOTES

▸ https://github.com/mrkaspa/Exstreme

▸ Future

▸ Add Backpresure (GenStage)

▸ Supervising

▸ Remote nodes

Page 25: Elixir par

THANKS!