[erlang-questions] spawning funs on other nodes that does not contain my module

Kresten Krab Thorup krab@REDACTED
Tue Nov 30 20:19:42 CET 2010


Marcel,

I discuss these issues at some length in this blog post: http://bit.ly/fQoKlu; and towards the end you'll see some code that lets you construct an interpreted fun, like this:

1> FunStr = "fun (A) -> A+B end.". 
2> {ok, Tokens, _} = erl_scan:string(FunStr).
3> {ok, [Form]} = erl_parse:parse_exprs(Tokens).
4> Bindings = erl_eval:add_binding('B', 2, erl_eval:new_bindings()).
5> {value, Fun, _} = erl_eval:expr(Form, Bindings).
6> Fun(1).
3

Such a fun should be passable between nodes, if only the target node has erl_eval (and related modules).  Which should be very likely, but it needs to be the same version of erl_eval.   It is not very fast though, but that may not be important to you; especially if what you want to do is something simple.

Kresten


On Nov 30, 2010, at 19:36 , Marcel Meyer wrote:

> Hi there,
> 
> Is it possible to spawn a function (which is pretty straight
> forward, essentially a receive loop that grabs the message and
> file:write_file's it to the file system) on another node, which does not
> have a copy of my module?
> 
> I thought I would be able to make a fun that returns the mentioned fun above
> and then spawn that message sink on another node.
> 
> I am investigating writing a file propagation system and it would be nice if
> there is one process that gets notified of new files, which spawns a fun on
> each recipient node, which tells that node how to handle it.
> 
> Any insights? Or should each node be running a file sink that just listens
> for these {save, Data} msgs? I am trying to write something where I dont
> have to manage the child nodes and their code. Perhaps a file sink
> gen_server on each node that supports upgrades?
> 
> Regards,
> Marcel

Kresten Krab Thorup, CTO, Trifork



More information about the erlang-questions mailing list