在erlang接收语句中引入“保存队列”的目的是什么 [英] What's the purpose to introduce 'save queue' in erlang receive statement

查看:68
本文介绍了在erlang接收语句中引入“保存队列”的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是erlang的新手,并从Joe Armstrong的 Programming Erlang开始教程。

I am new to erlang and starting tutorial from 'Programming Erlang' by Joe Armstrong.

我对选择接收中提到的保存队列感到困惑。 8.6。如果消息根本不匹配,为什么不直接删除它呢?将其放回邮箱以供以后处理的目的是什么?如果这是默认行为,那么这些垃圾消息(意味着它们无法匹配)可能会导致性能下降,因为它们会在不释放的情况下累积。

I feel puzzled about the 'save queue' mentioned in the selective receive in 8.6. If the message is not matched at all, why not drop it directly? What's the purpose of putting it back to the mailbox for later processing? If it's default behavior, those garbage messages (meaning that they cannot be matched) could lead to performance penalties as they will accumulate without release.

我想知道我是否误会了这部分。我试图检查进程邮箱的内容以获得更好的理解,但是我无法。

I would like to know whether I misunderstood this part. I tried to inspect the contents of a process mailbox to get a better understanding but I was unable to.

请帮助,谢谢能证明这一点的代码段,谢谢。

Please help, I would appreciate any code snippet that proves a point, thanks.

推荐答案

本书的这一段描述了一个接收组完成的操作的详细信息。不要忘记,有一个进程可以依次处理多个接收块,因此一条消息与第一个接收块的任何条目都不匹配,这将被放入保存队列中(以提高效率),因为:

This paragraph of the book describes the details of what is done by one receive bloc. Don't forget that it is possible to have several receive blocs processed sequentially by one single process, so one message which doesn't match any entry of the first receive bloc will be put in the save queue (for efficiency) because:


  • 它永远不会匹配当前接收组的任何条目,

  • 保证当前接收组仅当一条输入消息与一项匹配或超时结束时结束。

一个接收组完成后,将保存队列返回,以原始接收顺序显示在邮箱中,因为下一个接收组有机会输入与保存队列消息之一匹配的条目。

When one receive bloc is done, then the save queue is put back, in original receive order, in the mail box because the next receive bloc has a chance to have an entry which match one of the "save queued" messages.

一个用法是管理优先级:

One usage is to manage priorities:

loop() ->
   ...
   receive
      {high_prio,Msg} -> process_priority_message(Msg)
   after 0
      ok  % no priority message
   end,

   receive
      {low_prio,Msg} -> process_normal_message(Msg);
      Other -> process_unexpected_message(Other)
   end,
   ...
   loop()

此代码允许处理消息{high_prio,Msg},即使它不在消息队列中的第一位置。

This code allow to process a message {high_prio,Msg} even if it is not in the first position within the message queue.

您说的对,那里邮箱中有意外消息堆积的风险,尤其是在永无止境的循环中,这就是为什么您会经常看到最后一行这样的原因

And you are right, there is a risk that unexpected messages accumulate in the mail box, especially in a never ending loop, it is why you will see very often something like the last line


其他-> process_unexpected_message(其他)

Other -> process_unexpected_message(Other)

清空邮箱。

这篇关于在erlang接收语句中引入“保存队列”的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆