gen-server

Erlang shell stopped loading my modules

坚强是说给别人听的谎言 提交于 2019-12-12 03:46:38
问题 This was working fine last week. I would open cd myprojectdir erl (erlang shell). c(room). {ok, R} = room:go(). then I could use gen_server to send messages to R, interacting with my room module. I started in a cowboy routing branch. I was seeing things work fine, when I looked at it today I was getting compile errors. So I rolled back to my master branch. Still got errors in the shell, even rolling back a few commits, hm. When I try to call room:go(), the other modules are printed as undef .

Erlang gen_server communication

天涯浪子 提交于 2019-12-11 23:14:57
问题 If I have several instances of server, how can I pass info from one to another, for example: I have this: ... -record(id,{name,hash}). -record(state, {id ,m, succ, pred}). start(Name, M) -> gen_server:start_link({local, Name}, ?MODULE, {new_ring, Name, M}, []). join_ring(Name, Other) -> gen_server:start_link({local, Name}, ?MODULE, {join_ring, Name, Other}, []). ... init({new_ring, Name, M}) -> Me=#id{name=Name,hash=M} {ok, #state{ id = Me, m = M, succ = Me, pred = nil, }}; init({join_ring,

Why do we need simple_one_for_one?

巧了我就是萌 提交于 2019-12-11 10:04:18
问题 Somebody told me that simple_one_for_one is very useful for chat applications, because each chat client is a server process (gen_server). Is this right? And I wonder why do we need it? Why not create only one center server (gen_server) to handle all chat client communication? Because maybe the number of chat clients is very large so only one server couldn't handle fast, make the system slow down? I think maybe creating too many servers like simple_one_for_one may take too much system resource

Erlang stop gen_server

北战南征 提交于 2019-12-10 11:25:52
问题 I have gen_server: start(UserName) -> case gen_server:start({global, UserName}, player, [], []) of {ok, _} -> io:format("Player: " ++ UserName ++ " started"); {error, Error} -> Error end ... Now i want to write function to stop this gen server. I have: stop(UserName) -> gen_server:cast(UserName, stop). handle_cast(stop, State) -> {stop, normal, State}; handle_cast(_Msg, State) -> {noreply, State}. I run it: start("shk"). Player: shk startedok stop(shk). ok start("shk"). {already_started,<0

Erlang: difference between using gen_server:cast/2 and standard message passing

情到浓时终转凉″ 提交于 2019-12-05 17:49:19
问题 I was working though a problem and noticed some code where a previous programmer was passing messages using the standard convention of PID ! Message. I have been using gen_server:cast/2. I was wondering if somebody could explain to me the critical differences and considerations when choosing between the two? 回答1: There are a few minor differences: Obviously, the gen_server handles casts in handle_cast and "normal" messages in handle_info . A cast never fails; it always returns ok . Sending a

Graceful shutdown of GenServer

大憨熊 提交于 2019-12-04 17:05:15
问题 I writing an Elixir app with GenServer that starts an external application on boot and shuts it down and does other clean-up on exit. I've added bootup functionality in the init/1 callback and cleanup code in the terminate/2 callback. The init code works fine when the GenServer is started, and the terminate method is also called when the :stop signal is manually sent, but in the cases of unexpected shutdowns and interrupts (as in the case of hitting Ctrl+C) in IEx, the terminate code is not

Graceful shutdown of GenServer

半腔热情 提交于 2019-12-03 10:25:21
I writing an Elixir app with GenServer that starts an external application on boot and shuts it down and does other clean-up on exit. I've added bootup functionality in the init/1 callback and cleanup code in the terminate/2 callback. The init code works fine when the GenServer is started, and the terminate method is also called when the :stop signal is manually sent, but in the cases of unexpected shutdowns and interrupts (as in the case of hitting Ctrl+C) in IEx, the terminate code is not called. Currently, I've gone over tons of forum threads, blog posts and documentation, including:

Erlang gen_server with long-running tasks

和自甴很熟 提交于 2019-12-03 08:13:12
Good day, I have a gen_server process which does some long-running state-updating tasks periodically in handle_info : handle_info(trigger, State) -> NewState = some_long_running_task(), erlang:send_after(?LOOP_TIME, self(), trigger), {noreply, NewState}. But when such task runs, then whole server gets unresponsive and any call to it leads to whole server crash: my_gen_server:status(). ** exception exit: {timeout,{gen_server,call,[my_gen_server,status]}} in function gen_server:call/2 How it is possible to avoid blocking of gen_server ? And when one call my_gen_server:status() at any time, the

erlang OTP Supervisor crashing

喜欢而已 提交于 2019-11-30 07:13:33
问题 I'm working through the Erlang documentation, trying to understand the basics of setting up an OTP gen_server and supervisor. Whenever my gen_server crashes, my supervisor crashes as well. In fact, whenever I have an error on the command line, my supervisor crashes. I expect the gen_server to be restarted when it crashes. I expect command line errors to have no bearing whatsoever on my server components. My supervisor shouldn't be crashing at all. The code I'm working with is a basic "echo

what kind of types can be sent on an erlang message?

安稳与你 提交于 2019-11-30 05:22:07
Mainly I want to know if I can send a function in a message in a distributed erlang setup. on Machine 1 F1 = Fun()-> hey end, gen_server:call(on_other_machine,F1) on Machine 2 handler_call(Function,From,State) -> {reply,Function(),State) make sense? Here 's an interesting article about "passing fun's to other Erlang nodes". To resume it briefly: [...] As you might know, Erlang distribution works by sending the binary encoding of terms; and so sending a fun is also essentially done by encoding it using erlang:term_to_binary/1; passing the resulting binary to another node, and then decoding it