[erlang-questions] how to dynamically create function to run in a different node

Ulf Wiger <>
Thu Dec 25 20:29:39 CET 2014


On 25 Dec 2014, at 11:55, Dror Mein <> wrote:

> My preferred solution would be somehow to compile a function turning it into <erl_eval> in a module. Is it possible? will it work?
> 


One solution to this can be found in the locks application:

https://github.com/uwiger/locks/blob/master/src/locks_watcher.erl

The code is invoked via a parse transform:
https://github.com/uwiger/locks/blob/master/src/locks_agent.erl#L39

And the transform expands the pseudo-call `locks_watcher(self())`
https://github.com/uwiger/locks/blob/master/src/locks_agent.erl#L731


watch_node(N) ->
    {M, F, A} =
        locks_watcher(self()),  % expanded through parse_transform
    P = spawn(N, M, F, A),
    erlang:monitor(process, P).

The {M, F, A} tuple will be {erl_eval, exprs, [Exprs, Bindings]}, where
Exprs is the abstract-form representation of the code for
locks_watcher:locks_watcher/1.

Another version can be found in the deqc application:
https://github.com/uwiger/deqc/blob/master/src/deqc_proxy.erl#L80

where the function disconnect_script(Node) ends up returning a list of abstract forms via some parse transform magic.


disconnect_script(EqcNode) ->
    codegen:exprs(
      fun() ->
	      Nodes = nodes(),
	      [net_kernel:disconnect(N) || N <- Nodes -- [{'$var', EqcNode}]]
      end).

The function codegen:exprs/1 is a pseudo function handled by parse_trans_codegen.

BR,
Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141225/89222fac/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141225/89222fac/attachment.bin>


More information about the erlang-questions mailing list