发布于 2016-03-15 02:59:00 | 163 次阅读 | 评论: 0 | 来源: 网友投递
Erlang 编程语言
Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此非常适 合于构建分布式,实时软并行计算系统。
上一篇文章中说到, 注册进程关联的原子具有全局作用域, 这里的全局指的是当前 Erlang 虚拟机, 在分布式中, 便是当前分布式节点. 因此, 在一个节点中注册进程关联的原子, 在另一个节点中是不能直接使用, 而必须配和目标节点使用.
{RegName, Node} ! {messages}.
先启动一个 Server 节点
erl -sname server
先简单介绍几个常用函数
% 查看当前节点
node().
% => 'server@Gentoo-PC'
% 查看所有已连接的节点
nodes().
% => [] % 此时还未连接其它节点
% 查看当前节点是否存活
is_alive().
% => true
% 启动上一篇文章中最后的那个程序
test:start().
% Waiting for new message.
% => true
% 当前节点可以使用 testp 原子
testp ! message.
% New message: message
% Waiting for new message.
% => message
erl -sname client
nodes().
% => [] % 此时未连接节点
% 当前节点无法直接使用这个原子的
testp ! {}.
% ** exception error: bad argument
% in operator !/2
% called as testp ! {}
% 需要配合目标节点一起使用
{testp, 'server@Gentoo-PC'} ! {}.
% => {} % 语句返回值
% New message: {}
% Waiting for new message.
在 Client 节点中
nodes().
% => ['server@Gentoo-PC']
nodes().
% => ['client@Gentoo-PC']
结尾
当然, 这只是个方法, 由于在模块中定义了 call 函数包裹了起来, 所以可以使用远程调用, 调用 Server 节点上的 test:call 方法.
可以使用 rpc 模块中的 call/4 方法远程调用函数
% 在 Node 节点上执行 apply(Module, Function, Args)
% 调用成功时返回 Result, 调用失败时返回 {badrpc, Reason}
-spec rpc:call(Node, Module, Function, Args} -> Result | {badrpc, Reason}
rpc:call('server@Gentoo-PC', test, call, ['message from other node']).