View
139
Download
0
Category
Tags:
Preview:
Citation preview
Erlang
ElixirWebilea Hands-On Session at Obst & Gemüse, 19.3.2014
By André Graf@der_graf / @erlio_basel
● Erlang and Elixir Programs are compiled for the BEAM*
● Similar datatypes and runtime behavior● Different syntax, language features, stdlib
Preliminaries
*Bogdan/Björn's Erlang Abstract Machine
Erlang has been developed by Ericsson, for building
highly available telco systems
Massively concurrentserver systems
Server systems that run forever
Hot-Code Upgrades
Inherently parallelproblem domain
High availabilitythrough redundancy
DebuggingLive Systems
1986
● Functional● Compiled● Dynamically typed● VM & Shell● Very lightweight processes● Processes don't share state● Message Passing Concurrency● Process Supervision (Let it crash)● Built-in Distribution● Hot-Code Upgrades
Erlang OTP
● Architecture Patterns● Middlewares● Libraries● Tools
● You could use Erlang witout OTP (nobody does)● OTP is included in the Erlang distribution● OTP is closely tied to the Erlang VM
Erlang has been opensourced and other
companies started using it.Ericsson went for C++
Massively concurrentserver systems
Server systems that run forever
Hot-Code Upgrades
Inherently parallelproblem domain
High availabilitythrough redundancy
DebuggingLive Systems
1998
● Functional● Compiled● Dynamically typed● VM & Shell● Very lightweight processes● Processes don't share state● Message Passing Concurrency● Process Supervision (Let it crash)● Built-in Distribution● Hot-Code Upgrades
Erlang vs. Elixirvery subjective
+ Elixir has a more familiar Syntax.+ Elixir fixes several language design mistakes from the past. *+ Elixir will bring more people to the Beam.+ Elixir has better meta-programming support.
- Elixir has (currently) no own OTP framework. > killer for production use.
* http://joearms.github.io/2013/05/31/a-week-with-elixir.html
Erlang Hands-On
1. Installation Party
2. Language introduction http://www.tryerlang.org/
3. My first Erlang module
4. My first Erlang process
5. Let it crash
6. OTP behaviours gen_server / supervisor
7. Recap / Discussion
My first Erlang Module
● Module-Name == Filename.erl – e.g. my_first_module.erl
-module(my_first_module).-export([my_first_function/1]).
% this is a comment for my first function% this is another line of the commentmy_first_function(Param) ->
io:format(“wow my first function got called with param: ~p~n”, [Param]).
1. Compile with erlc my_first_module.erl2. Start Erlang shell with: erl3. Call function my_first_module:my_first_function(“hello world”).
My first Erlang Process-module(my_first_module).-export([my_first_function/0,
start_my_first_process/0]).
my_first_function() ->receive
Msg ->io:format(“wow my first process got a msg: ~p~n”, [Msg])
end.
start_my_first_process() ->spawn(?MODULE, my_first_function, []).
1. Compile with erlc my_first_module.erl or if you are inside the shell with:
c(my_first_module).l(my_first_module).
2. Start your first process:Pid = my_first_module:start_my_first_process().
3. Send your first message:Pid ! yeah_this_is_my_first_message.
4. How can you reuse the Process???
Let it Crash-module(my_first_module).-export([my_first_function/0,
start_my_first_process/0]).
my_first_function() ->receive
Msg ->dispatch_my_msg(Msg),my_first_function() %% yay, recursion!
end.
start_my_first_process() ->spawn(?MODULE, my_first_function, []).
dispatch_my_msg({hello, world}) ->io:format(“what a great hello world~n”);
dispatch_my_msg({A, B}) -> io:format(“tuple with ~p and ~p~n”, [A,B]).
1. kinda stupid dispatcher, right???
Let it Crash-module(my_first_module).-export([my_first_function/0,
start_my_first_monitored_process/1,my_monitor/1]).
my_first_function() ->receive
Msg ->dispatch_my_msg(Msg),my_first_function()
end.
my_monitor(ProcName) when is_atom(ProcName) ->Pid = spawn(?MODULE, my_first_function, []),true = register(ProcName, Pid),MonitorRef = monitor(process, Pid),receive
{'DOWN', MonitorRef, _Type, Pid, _Info} ->my_monitor(ProcName);
stop ->demonitor(MonitorRef)
end.
start_my_first_monitored_process(ProcName) ->spawn(?MODULE, my_monitor, [ProcName]).
...
OTP Behaviours
● gen_server– Generic way to write servers in Erlang OTP Style
● Supervisor– A Supervisor that supervises other OTP compliant
processes (e.g. gen_servers, gen_fsms)
gen_server-module(sample_gen_server).-behaviour(gen_server).-export([start_link/0]).
%% gen_server callbacks-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%%% APIstart_link() -> gen_server:start_link(?MODULE, [], []).
%%% gen_server callbacksinit([]) -> {ok, []}.
handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}.
handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
supervisor-module(sample_supervisor).-behaviour(supervisor).-export([start_link/0]).
%% Supervisor callbacks-export([init/1]).
%% ===================================================================%% API functions%% ===================================================================
start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% ===================================================================%% Supervisor callbacks%% ===================================================================
init([]) -> {ok, { {one_for_one, 5, 10}, [ %% 5 restarts within 10 seconds are allowed
{sample_gen_server, {sample_gen_server, start_link, []
}, permanent, 5000, worker, [sample_gen_server]}]}}.
Recommended