What types can be sent to erlang message?

Basically I want to know if I can send a function in a message in erlang distributed setup.

by car 1

F1 = Fun()-> hey end, gen_server:call(on_other_machine,F1) 

by car 2

 handler_call(Function,From,State) -> {reply,Function(),State) 

has the meaning?

+6
source share
3 answers

Here's an interesting article on how to "travel with other Erlang nodes." To resume it briefly:

[...] As you know, Erlang distribution works by sending a binary encoding of terms; and therefore sending pleasure also essentially by encoding it using Erlang: term_to_binary / 1; passing leading the binary to another node, and then decodes it again using Earl :. binary_to_term / 1 [...] This is pretty obvious for most data types; but how does this work for function objects?

When you code the pleasure that encoded is just a reference to a function, not an implementation of a function. [...]

[...] the definition of the function is not passed; just enough information to recreate the fun in another node if the module is there.

[...] If the module containing the fun has not yet been loaded, and the target node is operating interactively; then the module is loaded using the regular module loading mechanism (contained in the error_handler module); and then he tries to see if the pleasure of the given identifier is available in the specified module. However, this only happens lazily when you try to apply this function.

[...] If you never try to apply a function, then nothing bad happens. Fun can be passed on to another node (which has module / fun), and then everyone is happy. Perhaps the target node has a module loaded with the specified name, but perhaps in a different version; which then most likely will have a different MD5 checksum, then you will get a badfun error if you try to apply it.

I would suggest you read the whole article, because it is very interesting.

+7
source

You can submit any valid Erlang term. Although you should be careful when sending pleasure. Any entertainment that references a function inside a module needs this module to exist on the target node to work:

 ( first@host )9> rpc:call( second@host , erlang, apply, [fun io:format/1, ["Hey!~n"]]). Hey! ok ( first@host )10> mymodule:func("Hey!~n"). 5 ( first@host )11> rpc:call( second@host , erlang, apply, [fun mymodule:func/1, ["Hey!~n"]]). {badrpc,{'EXIT',{undef,[{mymodule,func,["Hey!~n"]}, {rpc,'-handle_call_call/6-fun-0-',5}]}}} 

In this example, io exists on both nodes, and it works to send functions from io as entertainment. However, mymodule exists only on the first node, and fun throws a undef exception when called on another node.

+5
source

As for the anonymous functions, it seems they can be sent and worked as expected.

t1 @ local:

 ( t1@localhost )7> register(shell, self()). true ( t1@localhost )10> A = me, receive Fun when is_function(Fun) -> Fun(A) end. hello me you ok 

t2 @ local:

 ( t2@localhost )11> B = you. you ( t2@localhost )12> Fn2 = fun (A) -> io:format("hello ~p ~p~n", [A, B]) end. #Fun<erl_eval.6.54118792> ( t2@localhost )13> {shell, ' t1@localhost '} ! Fn2. 

I am adding coverage logic to an application built on riak-core, and merging collected results can be difficult if anonymous functions cannot be used in messages.

Also check riak_kv / src / riak_kv_coverage_filter.erl

riak_kv can use it for the filter result, I think.

+2
source

Source: https://habr.com/ru/post/888907/


All Articles