Created
May 31, 2018 15:46
-
-
Save lucarin91/1d74c58b6ec02452b4f0a309ff0659f3 to your computer and use it in GitHub Desktop.
A simple example of an Erlang server that can dynamically change the code to process new requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(hot_change). | |
-export([start/0, send/1, update_code/1]). | |
start() -> | |
F = fun(Str) -> string:lowercase(Str) end, | |
register(hot_change_server, spawn(fun () -> loop(F) end)). | |
loop(F) -> | |
receive | |
{request, Pid, Q} -> | |
Res = F(Q), | |
Pid ! Res, | |
loop(F); | |
{update, F1} -> | |
loop(F1) | |
end. | |
send(Q) -> | |
hot_change_server ! {request, self(), Q}, | |
receive | |
Res -> Res | |
end. | |
update_code(F) -> hot_change_server ! {update, F}, ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A possible executing inside
erl