什么样的类型可以发送到erlang消息? [英] what kind of types can be sent on an erlang message?

查看:130
本文介绍了什么样的类型可以发送到erlang消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要是想知道我是否可以在一个分布式的erlang设置的消息中发送一个函数。

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

在机器1上

F1 = Fun()-> hey end,

gen_server:call(on_other_machine,F1)

2

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

有意义?

推荐答案

这里是一篇有趣的文章,将乐趣传递给其他Erlang节点。要简单地恢复:

Here's an interesting article about "passing fun's to other Erlang nodes". To resume it briefly:


[...]您可能知道,Erlang分发
通过发送二进制编码
的条款;所以发送一个乐趣也是
本质上通过使用
erlang:term_to_binary / 1编码来完成;将
导出的二进制文件传递给另一个节点,然后
再次使用
erlang:binary_to_term / 1进行解码。[...]
这是非常明显的
大多数数据类型;但是
如何为函数对象工作?

[...] As you might know, Erlang distribution works by sending the binary encoding of terms; and so sending a fun is also essentially done by encoding it using erlang:term_to_binary/1; passing the resulting binary to another node, and then decoding it again using erlang:binary_to_term/1.[...] This is pretty obvious for most data types; but how does it work for function objects?

当编码一个乐趣时,编码的
只是对函数的引用,
不是函数的实现。
[...]

When you encode a fun, what is encoded is just a reference to the function, not the function implementation. [...]

[...]函数的定义不传递;如果模块在那里,只需要足够的信息来重建另一个节点的乐趣。

[...]the definition of the function is not passed along; just exactly enough information to recreate the fun at an other node if the module is there.

[...]如果包含乐趣的模块尚未加载,目标节点以交互方式运行;然后使用常规模块加载机制(包含在模块error_handler中)尝试加载模块;然后尝试查看在该模块中是否有可用的给定ID的乐趣。但是,只有当您尝试应用该功能时,这只会发生懒惰。

[...] If the module containing the fun has not yet been loaded, and the target node is running in interactive mode; then the module is attempted loaded using the regular module loading mechanism (contained in the module error_handler); and then it tries to see if a fun with the given id is available in said module. However, this only happens lazily when you try to apply the function.

[...]如果您从未尝试应用该功能,则不会发生任何不良事件。乐趣可以传递给另一个节点(它有模块/有趣的问题),然后每个人都快乐。
也许目标节点有一个加载了所述名称的模块,但也可能在不同的版本中;那么那么很可能会有一个不同的MD5校验和,那么如果你试图应用它,你会得到错误。

[...] If you never attempt to apply the function, then nothing bad happens. The fun can be passed to another node (which has the module/fun in question) and then everybody is happy. Maybe the target node has a module loaded of said name, but perhaps in a different version; which would then be very likely to have a different MD5 checksum, then you get the error badfun if you try to apply it.

我建议您阅读整篇文章,因为这非常有趣。

I would suggest you to read the whole article, cause it's extremely interesting.

这篇关于什么样的类型可以发送到erlang消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆