[英]Erlang receive and after
p(M, C, B, I) when B =:= 32#3J ->
receive {_} -> a
after 27#5C ->
C ! { self(), { M, (B * 13#37) rem 35#7B, I }} end;
This is a part of code that expects input.这是需要输入的代码的一部分。 I understand that it needs to look like
(num,num,115,num)
to pass the first part, but I don't understand what happens after that with the receive
and the after
part, can anybody explain?我知道它需要看起来像
(num,num,115,num)
才能通过第一部分,但我不明白receive
和after
的部分会发生什么,有人可以解释一下吗?
I tried reading documentation of erlang and just couldn't understand that part of the code.我尝试阅读 erlang 的文档,但无法理解那部分代码。
Here's what a receive expression looks like:这是接收表达式的样子:
receive
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
after
ExprT ->
BodyT
end
In erlang, a receive
waits for a message to be sent to the process executing the receive.在 erlang 中,
receive
等待消息发送到执行接收的进程。 The message has to match a Pattern, in your case {_}
, for the Body of the matching clause to execute (otherwise the receive waits indefinitely for a matching message).该消息必须匹配模式,在您的情况下为
{_}
,才能执行匹配子句的正文(否则接收将无限期等待匹配消息)。 The term {_}
matches any message that consists of a tuple, {}
, with a single term inside the tuple, _
.术语
{_}
匹配任何由元组{}
组成的消息,元组中有一个术语_
。 The notation _
matches anything, so the tuple could contain a nested tuple, a list, a string, a number...anything.符号
_
匹配任何内容,因此元组可以包含嵌套元组、列表、字符串、数字……任何内容。 So, the tuple {[1,a,"hello"]}
would match, but not the tuple {a, 2}
.因此,元组
{[1,a,"hello"]}
会匹配,但元组{a, 2}
不会匹配。
receive..after
works exactly as receive, except that if no matching message has arrived within ExprT milliseconds, then BodyT is evaluated instead. receive..after
工作方式与 receive 完全相同,只是如果在 ExprT 毫秒内没有匹配的消息到达,则改为评估 BodyT。
Here's your code:这是你的代码:
p(M, C, B, I) when B =:= 32#3J ->
receive
{_} ->
a
after 27#5C ->
C ! { self(), { M, (B * 13#37) rem 35#7B, I }}
end;
Integers can be expressed in the format: base#value , and 27#5C
is the same as the decimal number 147.整数可以表示为: base#value ,
27#5C
等同于十进制数 147。
In this line:在这一行中:
C ! { self(), { M, (B * 13#37) rem 35#7B, I }}
the variable C
will contain a process identifier, called a "pid" in erlang, and the operator !
变量
C
将包含一个进程标识符,在 erlang 中称为“pid”,以及运算符!
sends a message to the process specified on the left hand side.向左侧指定的进程发送消息。 The right had side is the message that is sent to process
C
, which in this case is a tuple containing the current process identifier, self()
, as the first term, and another tuple as the second term.右侧是发送到进程
C
的消息,在本例中是一个元组,其中包含当前进程标识符self()
作为第一项,另一个元组作为第二项。
Your code consists of a function that contains a receive statement, where the receive has a 147 millisecond timeout.您的代码由一个包含接收语句的函数组成,其中接收有 147 毫秒的超时。 After the
receive
line executes, if there's a message currently in the process mail box that matches {_}
or if the process receives a message that matches {_}
within the next 147 milliseconds, then the function returns the atom a
, otherwise the function sends a message to another process and returns the same message--because !
receive
行执行后,如果当前进程邮箱中有匹配{_}
的消息,或者进程在接下来的 147 毫秒内收到匹配{_}
的消息,则函数返回原子a
,否则函数向另一个进程发送消息并返回相同的消息——因为!
returns the right hand side.返回右侧。
In erlang, you can easily start 1 million processes on a basic laptop, and the processes can send each other messages and the processes can receive particular messages and execute code in response to those messages.在 erlang 中,您可以轻松地在一台基本笔记本电脑上启动 100 万个进程,进程可以相互发送消息,进程可以接收特定消息并执行代码以响应这些消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.