Erlang 消息可以发送哪些类型?
我主要想知道是否可以在分布式 Erlang 设置中在消息中发送函数。
在机器 1 上:
F1 = Fun()-> hey end,
gen_server:call(on_other_machine,F1)
在机器 2 上:
handler_call(Function,From,State) ->
{reply,Function(),State)
这有意义吗?
Mainly I want to know if I can send a function in a message in a distributed Erlang setup.
On Machine 1:
F1 = Fun()-> hey end,
gen_server:call(on_other_machine,F1)
On Machine 2:
handler_call(Function,From,State) ->
{reply,Function(),State)
Does it make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一篇关于“将 fun 传递给其他 Erlang 节点”的有趣文章。简单地恢复一下:
我建议您阅读整篇文章,因为它非常有趣。
Here's an interesting article about "passing fun's to other Erlang nodes". To resume it briefly:
I would suggest you to read the whole article, cause it's extremely interesting.
您可以发送任何有效的 Erlang 术语。虽然发帖的时候要小心。任何引用模块内函数的乐趣都需要该模块存在于目标节点上才能工作:
在此示例中,
io
存在于两个节点上,并且它可以从io
发送函数。代码>作为一种乐趣。但是,mymodule
仅存在于第一个节点上,并且当在另一个节点上调用 fun 时会生成undef
异常。You can send any valid Erlang term. Although you have to be careful when sending funs. Any fun referencing a function inside a module needs that module to exist on the target node to work:
In this example,
io
exists on both nodes and it works to send a function fromio
as a fun. However,mymodule
exists only on the first node and the fun generates anundef
exception when called on the other node.至于匿名函数,似乎可以发送并按预期工作。
t1@localhost:
t2@localhost:
我正在向基于 riak-core 构建的应用程序添加覆盖逻辑,如果无法在消息中使用匿名函数,则收集到的结果的合并可能会很棘手。
另请查看 riak_kv/src/riak_kv_coverage_filter.erl
riak_kv 可能会使用它<我猜,代码>过滤结果。
As for anonymous functions, it seems they can be sent and work as expected.
t1@localhost:
t2@localhost:
I am adding coverage logic to an app built on riak-core, and the merge of results gathered can be tricky if anonymous functions cannot be used in messages.
Also check out riak_kv/src/riak_kv_coverage_filter.erl
riak_kv might be using it to
filter
result, I guess.