[英]Erlang process gets stuck
Im working on erlang for the first time. 我是第一次使用erlang。 everytime i try to run the erlang process it gets stuck and does not take input.
每次我尝试运行erlang进程时,它都会卡住,并且不会接受输入。 Im using erlide plugin in eclipse to test the erlang code.
我在eclipse中使用erlide插件来测试erlang代码。
CODE IS:: 代码是::
-module(message_router).
%% ====================================================================
%% API functions
%% ====================================================================
%%-compile(export_all).
-export([start/0]).
-export([stop/1]).
-export([send_chat_message/3]).
-export([route_messages/0]).
%% ====================================================================
%% Internal functions
%% ====================================================================
start() ->
spawn(message_router, route_messages, []).
stop(RouterPid) ->
RouterPid ! shutdown.
send_chat_message(RouterPid, Addressee, MessageBody) ->
io:format("send_chat_msg FROM:: ~p TO:: ~p ~n", [RouterPid, Addressee]),
RouterPid ! {send_chat_msg, Addressee, MessageBody}.
route_messages() ->
receive
{send_chat_msg, Addressee, MessageBody} ->
io:format("recv_chat_msg PID:: ~p ~n", [Addressee]),
Addressee ! {recv_chat_msg, MessageBody},
route_messages();
{recv_chat_msg, MessageBody} ->
io:format("Received: ~p~n", [MessageBody]);
shutdown ->
io:format("Shutting down ~n");
Oops ->
io:format("Warning! Received: ~p~n", [Oops]),
route_messages()
end.
When i hit try to run the code like in the shell 当我点击尝试在外壳中运行代码
Eshell V5.10.4
(nodename@pa)1> P1 = message_router:start().
<0.1308.0>
(nodename@pa)2> P2 = message_router:start().
<0.1373.0>
(nodename@pa)3> chat_client:send_message(P1, P2, "FIRST Msg").
Sending chat message from chat_client
send_chat_msg FROM:: <0.1308.0> TO:: <0.1373.0>
Every thing i enter in the shell after this has on effect. 此后,我进入外壳的所有内容均生效。 Also could anyone explain how loops are handled in erlang and best practices.
任何人也可以解释如何使用erlang和最佳实践来处理循环。
[edit] [编辑]
chat client code: 聊天客户端代码:
-module(chat_client).
-export([send_message/3]).
send_message(RouterPid, Addressee, MessageBody) ->
io:format("Sending chat message from chat_client~n"),
message_router:send_chat_message(RouterPid, Addressee, MessageBody).
Problem in module. 模块中的问题。 Call this
message_router:send_chat_message(P1, P2, "FIRST Msg")
将此
message_router:send_chat_message(P1, P2, "FIRST Msg")
Look at this example and compare with his: 看这个例子,并与他比较:
Have module call observer
it registers two process loop/0
and trans/0
有模块调用
observer
它注册两个过程loop/0
和trans/0
-module(observer).
-export([observer/0, loop/0, trans/0]).
observer() ->
register(loop, spawn(fun observer:loop/0)),
register(trans, spawn(fun observer:trans/0)).
loop() ->
receive
{'PRINT', Msg} ->
io:format("print from ~p~n~p~n", [self(), Msg]), loop();
{'EXIT', _FromPid} ->
io:format("Exit ~nFrom ~p", [_FromPid]), exit(self(), kill)
end.
trans() ->
receive
{Command, Msg} -> io:format("transports from: ~p~n~p~n", [self(), Msg]),
loop ! {Command, Msg}
end.
and have module send_messenger
one function send_message
that sends messages through trans
: 并具有
send_messenger
模块一个功能send_message
,该函数通过trans
发送消息:
-module(send_messenger).
-export([send_message/1]).
send_message({Command, Msg}) ->
trans ! {Command, Msg},
ok.
I hope this has helped you! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.