Erlang 消息循环

发布于 2024-09-03 06:17:34 字数 218 浏览 4 评论 0原文

Erlang 中的消息循环如何工作,在处理消息时它们是否同步?

据我了解,循环将从“接收”消息开始,然后执行某些操作并进行循环的另一次迭代。

那么一定要同步吗?正确的?

如果多个客户端将消息发送到同一个消息循环,那么所有这些消息都会排队并依次执行,或者?

要并行处理多个消息,您必须在不同的进程中生成多个消息循环,对吧?

还是我误解了这一切?

How does message loops in erlang work, are they sync when it comes to processing messages?

As far as I understand, the loop will start by "receive"ing a message and then perform something and hit another iteration of the loop.

So that has to be sync? right?

If multiple clients send messages to the same message loop, then all those messages are queued and performed one after another, or?

To process multiple messages in parallell, you would have to spawn multiple message loops in different processes, right?

Or did I misunderstand all of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-09-10 06:17:34

发送消息是异步的。处理消息是同步的 - 一次接收一条消息 - 因为每个进程都有自己的(且只有一个)邮箱。

Sending a message is asynchronous. Processing a message is synchronous - one message is receive'd at a time - because each process has its own (and only one) mailbox.

提赋 2024-09-10 06:17:34

来自手册(Erlang 并发

每个进程都有自己的输入队列来存储它接收的消息。收到的新消息将放在队列末尾。当进程执行接收时,队列中的第一条消息与接收中的第一个模式进行匹配,如果匹配,则从队列中删除该消息并执行与该模式对应的操作。
但是,如果第一个模式不匹配,则测试第二个模式,如果匹配,则从队列中删除消息并执行与第二个模式对应的操作。如果第二个模式不匹配,则尝试第三个模式,依此类推,直到没有更多模式可供测试。如果没有更多模式可供测试,则第一条消息将保留在队列中,然后我们尝试第二条消息。如果这与任何模式匹配,则执行适当的操作,并将第二条消息从队列中删除(将第一条消息和任何其他消息保留在队列中)。如果第二条消息不匹配,我们将尝试第三条消息,依此类推,直到到达队列末尾。如果我们到达队列末尾,则进程会阻塞(停止执行)并等待,直到收到新消息并重复此过程。
当然,Erlang 实现是“聪明的”,并且最大限度地减少了每个消息的次数针对每次接收中的模式进行测试。

因此,您可以使用正则表达式创建 prio,但并发性是通过多个进程完成的。

From the manual (Erlang concurrency

Each process has its own input queue for messages it receives. New messages received are put at the end of the queue. When a process executes a receive, the first message in the queue is matched against the first pattern in the receive, if this matches, the message is removed from the queue and the actions corresponding to the the pattern are executed.
However, if the first pattern does not match, the second pattern is tested, if this matches the message is removed from the queue and the actions corresponding to the second pattern are executed. If the second pattern does not match the third is tried and so on until there are no more pattern to test. If there are no more patterns to test, the first message is kept in the queue and we try the second message instead. If this matches any pattern, the appropriate actions are executed and the second message is removed from the queue (keeping the first message and any other messages in the queue). If the second message does not match we try the third message and so on until we reach the end of the queue. If we reach the end of the queue, the process blocks (stops execution) and waits until a new message is received and this procedure is repeated.
Of course the Erlang implementation is "clever" and minimizes the number of times each message is tested against the patterns in each receive.

So you could create prios with the regex, but the concurrency is done via multiple processes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文