[erlang-questions] Sending fun/1 over the network and apply/2 failure

Tony Rogvall tony@REDACTED
Mon Jul 25 15:47:23 CEST 2016


I did some experiments for ”fun” a couple of years ago.
This can surely be used for some non critical applications.

You can modify the error_handler.erl ( global or per use )

Change undefined_lambda/3 to:

undefined_lambda(Module, Fun, Args) ->
    case ensure_loaded(Module) of
        {module, Module} ->
            %% There is no need (and no way) to test if the fun is present.
            %% apply/2 will not call us again if the fun is missing.
            apply(Fun, Args);
        {module, _} ->
            crash(Fun, Args);
        _Other ->
            {pid,Pid} = erlang:fun_info(Fun,pid),
            case try_load_remote_code(Pid,Module) of
                ok ->
                    case ensure_loaded(Module) of
                        {module, Module} ->
                            apply(Fun, Args);
                        _ ->
                            crash(Fun, Args)
                    end;
                _ ->
                    crash(Fun, Args)
            end
    end.

and add the function to load remote code.

%%
%% Try load code from remote node
%%
try_load_remote_code(Pid,Module) ->
    if node() =:= node(Pid) ->
            ok;  %% no need to load
       true ->
            case rpc:call(node(Pid),code,get_object_code,[Module]) of
                {Module, Binary, Filename} ->
                    case code:load_binary(Module,Filename,Binary) of
                        {module,Module} -> ok;
                        _ -> error
                    end;
                error -> error
            end
    end.

Home assignment: Wrap Funs with a resource handle ( https://github.com/tonyrog/resource ) the will enable garbage collection of
the code. :-)

/Tony

> On 25 jul 2016, at 15:20, Sid Muller <sid5@REDACTED> wrote:
> 
>> Sent: Monday, July 25, 2016 at 8:14 AM
>> From: "Joe Armstrong" <erlang@REDACTED>
>> To: "Sid Muller" <sid5@REDACTED>
>> Cc: "Mikael Pettersson" <mikpelinux@REDACTED>, Erlang <erlang-questions@REDACTED>
>> Subject: Re: [erlang-questions] Sending fun/1 over the network and apply/2 failure
>> 
> [SNIP]
>> If you want to make this work you just have to make sure that all
>> nodes have the same
>> version of the module(s) concerned. I guess just should be in quotes
>> (ie "just") since
>> this is very difficult (having 2 different versions of code on one
>> node is just about
>> understandable) but doing this over multiple nodes is far more difficult.
>> 
>> The "send the same code to all machines - then restart" strategy is
>> just about the
>> easiest way to make this work.
> 
> Hi, Joe-
> 
> yes that's what I ended up doing. I put the code into the module, send it over, upgrade, and then run the fun....
> 
> And it works, I was assuming previously that since I was sending an anon function it would work but I guess due to reasons pointed out by Mikael this didn't work...
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160725/b4ebb6bf/attachment.bin>


More information about the erlang-questions mailing list