Erlang tips: remote module loading

Context: when remote module pushing is needed.

Initial setup:
1. 2 nodes (physical nodes, each one having one erlang instance started): node1@10.0.0.1 and node2@10.0.0.2
2. both erlang instances must be started with the same cookie, in order to be able to successfully communicate

Steps:

(On node1, create test module)

-module(test).
-export([test_fun/0]).

test_fun() ->
    io:format("Hello ~s~n",["World"]).

(On node1, start erlang instance)

$ erl -name node1@10.0.0.1 -setcookie testcookie
Erlang R13B (erts-5.7.1)  [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.7.1  (abort with ^G)
(node1@10.0.0.1)1> 

(On node2, start erlang instance)

$ erl -name node2@10.0.0.2 -setcookie testcookie
Erlang R13B (erts-5.7.1)  [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.7.1  (abort with ^G)
(node1@10.0.0.2)1> 

(On node1, compile/load/remote load test module)

(node1@10.0.0.1)1> c(test).
{ok,test}
(node1@10.0.0.1)2> {Mod, Bin, _} = code:get_object_code(test).
{test,<<70,79,82,49,0,0,1,252,66,69,65,77,65,116,111,109,
        0,0,0,63,0,0,0,7,4,116,101,...>>,
      "/home/alin/test.beam"}
(node1@10.0.0.1)3> rpc:call('node2@10.0.0.2',erlang,load_module, [Mod, Bin]).
{module,test}
(node1@10.0.0.1)4> spawn('node2@10.0.0.2', test, test_fun, []).
Hello World
<8592.45.0>
(node1@10.0.0.1)5> 

(On node1, edit test.erl)

Instead of

... io:format("Hello ~s~n",["World"]).

set

... io:format("Hello ~s~n",["World2"]).

(On node1, go back to opened erlang shell)

(node1@10.0.0.1)5> f().
ok
(node1@10.0.0.1)6> c(test).
{ok,test}
(node1@10.0.0.1)7> {Mod, Bin, _} = code:get_object_code(test).
{test,<<70,79,82,49,0,0,1,252,66,69,65,77,65,116,111,109,
        0,0,0,63,0,0,0,7,4,116,101,...>>,
      "/home/alin/test.beam"}
(node1@10.0.0.1)8> rpc:call('node2@10.0.0.2',code,purge, [Mod]).
false
(node1@10.0.0.1)9> rpc:call('node2@10.0.0.2',erlang,load_module, [Mod, Bin]).
{module,test}
(node1@10.0.0.1)10> spawn('node2@10.0.0.2', test, test_fun, []).
<8592.48.0>
Hello World2
(node1@10.0.0.1)11> 

Note: node2 was not touched for anything else than starting (erl -name node2@10.0.0.2 -setcookie testcookie)

Done.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: