这个receive语句在这个judge函数中起到什么作用呢?
What purpose does this receive statement fullfil in this judge function?
我最近从 https://learnyousomeerlang.com 开始学习 Erlang
在本章errors and processes中,我了解了程序的作用以及它是如何执行的,但是我无法弄清楚判断函数中接收语句的目的是什么时候以及如何调用它?
根据我的理解,如果元组模式与 Pid 匹配并将其原子化 returns 一个原子。我将如何发送消息以接收内部法官?
start_critic() ->
spawn(?MODULE, critic, []).
judge(Pid, Band, Album) ->
Pid ! {self(), {Band, Album}},
receive
{Pid, Criticism} -> Criticism
after 2000 ->
timeout
end.
critic() ->
receive
{From, {"Rage Against the Turing Machine", "Unit Testify"}} ->
From ! {self(), "They are great!"};
{From, {"System of a Downtime", "Memoize"}} ->
From ! {self(), "They're not Johnny Crash but they're good."};
{From, {"Johnny Crash", "The Token Ring of Fire"}} ->
From ! {self(), "Simply incredible."};
{From, {_Band, _Album}} ->
From ! {self(), "They are terrible!"}
end,
critic().
输出
c(linkmon).
{ok,linkmon}
Critic = linkmon:start_critic().
<0.109.0>
linkmon:judge(Critic, "Genesis", "The Lambda Lies Down on Broadway").
"They are terrible!"
linkmon:judge(Critic, "Genesis", "A trick of the Tail Recursion").
"They are terrible!"
linkmon:judge(Critic, "Johnny Crash", "The Token Ring of Fire").
"Simply incredible."
第Pid ! ...
行向评论家发送消息。然后,评论家将通过 From ! ...
行之一发送响应。 judge
函数中的 receive
等待所述响应,然后简单地 returns 响应中包含的字符串。
我最近从 https://learnyousomeerlang.com 开始学习 Erlang 在本章errors and processes中,我了解了程序的作用以及它是如何执行的,但是我无法弄清楚判断函数中接收语句的目的是什么时候以及如何调用它?
根据我的理解,如果元组模式与 Pid 匹配并将其原子化 returns 一个原子。我将如何发送消息以接收内部法官?
start_critic() ->
spawn(?MODULE, critic, []).
judge(Pid, Band, Album) ->
Pid ! {self(), {Band, Album}},
receive
{Pid, Criticism} -> Criticism
after 2000 ->
timeout
end.
critic() ->
receive
{From, {"Rage Against the Turing Machine", "Unit Testify"}} ->
From ! {self(), "They are great!"};
{From, {"System of a Downtime", "Memoize"}} ->
From ! {self(), "They're not Johnny Crash but they're good."};
{From, {"Johnny Crash", "The Token Ring of Fire"}} ->
From ! {self(), "Simply incredible."};
{From, {_Band, _Album}} ->
From ! {self(), "They are terrible!"}
end,
critic().
输出
c(linkmon).
{ok,linkmon}
Critic = linkmon:start_critic().
<0.109.0>
linkmon:judge(Critic, "Genesis", "The Lambda Lies Down on Broadway").
"They are terrible!"
linkmon:judge(Critic, "Genesis", "A trick of the Tail Recursion").
"They are terrible!"
linkmon:judge(Critic, "Johnny Crash", "The Token Ring of Fire").
"Simply incredible."
第Pid ! ...
行向评论家发送消息。然后,评论家将通过 From ! ...
行之一发送响应。 judge
函数中的 receive
等待所述响应,然后简单地 returns 响应中包含的字符串。