gpt4 book ai didi

erlang - 从一个节点向另一个节点发送 Erlang 消息返回 badarg

转载 作者:行者123 更新时间:2023-12-02 09:00:11 27 4
gpt4 key购买 nike

我正在尝试学习 Erlang,但在向另一个节点发送消息时遇到问题。该程序只是一个非常简单的乒乓球,应该像这样工作。我在节点 m1@ASUS-N55SF 上启动一个程序。它创建了一个进程,并在节点 m2 上成功生成了一个进程。所以我得到包含 {PidPing, PidPong} 元组的 Pids。 PidPing 和 PidPong 是进程 ID,如 <0.X.0> 和
之后我调用 ps5:start_pinging(Pids) 函数并收到错误:

Pinging started
Ping received 0 on <0.53.0>
{ping,{<0.53.0>,<6807.43.0>},0}
Nodes: 'm1@ASUS-N55SF' 'm2@ASUS-N55SF'
(m1@ASUS-N55SF)8>
=ERROR REPORT==== 1-Dec-2012::15:27:18 ===
Error in process <0.53.0> on node 'm1@ASUS-N55SF' with exit
value: {badarg,[{ps5,loop_ping,0,[{file,"ps5.erl"},{line,33}]}]}

因此,初始消息到达 PidPing 并被 Loop_ping 接收并成功匹配,但我无法将消息发送到另一个节点,即使它们彼此可见。这会成功,因为此初始消息是在同一节点上发送的:

PidPing ! {pong, {PidPing, PidPong}, 0}.

据我了解,要向另一个节点发送消息,我应该发送如下消息:

{PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},

但它返回 badarg。节点已连接,使用相同的 cookie 启动,在同一台机器上运行,并且可以互相 ping 通并且可见。它们在同一文件夹中启动,因此它们会查找相同的 ps5.erl 文件。我也在不同的文件夹中尝试过,但也不起作用。如果代码在 seme 节点上运行,只需删除另一个节点上的生成并在同一节点上创建进程,它就可以工作。

这是代码:

-module(ps5).
-define(NODE_AS, 'm1@ASUS-N55SF').
-define(NODE_TP, 'm2@ASUS-N55SF').
%-define(NODE_TP, 'm2@ThinkPad-Z60m').

-compile(export_all).
start() ->
PidPing = ps5:start_ping(),
PidPong = ps5:start_pong(),
Pids = {PidPing, PidPong}.

start_ping() ->
Pid = spawn(?NODE_AS, ps5, loop_ping, []).

start_pong() ->
Pid = spawn(?NODE_TP, ps5, loop_pong, []).

start_pinging({PidPing, PidPong}) ->
io:format("Pinging started ~n", []),
PidPing ! {ping, {PidPing, PidPong}, 0}.

stop({PidPing, PidPong}) ->
exit(PidPing, normal),
exit(PidPong, normal).

loop_ping() ->
receive
{ping, {PidPing, PidPong}, Number} when Number < 10 ->
io:format("Ping received ~p on ~p~n", [Number, PidPing]),
io:format("Nodes: ~p ~p ~n", [?NODE_AS, ?NODE_TP]),
{PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},
io:format("Ping sent ping ~p to ~p~n", [Number, PidPong]),
ps5:loop_ping();
{ping, {PidPing, PidPong}, Number} when Number >= 10->
io:format("Ping received ~p on ~p~n and closing.~n", [Number, PidPing]);
Oops ->
io:format("Oops received: ~p~n", [Oops]),
ps5:loop_ping()
end.

loop_pong() ->
receive
{pong, {PidPing, PidPong}, Number} when Number < 10 ->
io:format("Pong received ~p on ~p~n", [Number, PidPong]),
io:format("Nodes: ~p ~p ~n", [?NODE_AS, ?NODE_TP]),
{PidPing, ?NODE_AS} ! {ping, {PidPing, PidPong}, Number + 1},
io:format("Pong sent ping ~p to ~p~n", [Number, PidPing]),
ps5:loop_pong();
{pong, {PidPing, PidPong}, Number} when Number >= 10->
io:format("Pong received ~p on ~p~n and closing.~n", [Number, PidPing]);
Oops ->
io:format("Oops received: ~p~n", [Oops]),
ps5:loop_pong()
end.

编译过程中出现一些警告,但它们与这个问题无关(至少我这么认为)。

最佳答案

构造{Registered_process_name, Erlang_node}! Some_message 用于向您不知道 PID 的远程注册进程发送消息。

在您的情况下,您知道 pid(并且进程未注册),这意味着您不能使用 {name, node} !消息而是PID!消息即更改:

{PidPong, ?NODE_TP} ! {pong, {PidPing, PidPong}, Number + 1},

PidPong ! {pong, {PidPing, PidPong}, Number + 1},

{PidPing, ?NODE_AS} ! {ping, {PidPing, PidPong}, Number + 1}, 

PidPing ! {ping, {PidPing, PidPong}, Number + 1},

关于erlang - 从一个节点向另一个节点发送 Erlang 消息返回 badarg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com