Scala 演员 - 最糟糕的做法? [英] Scala actors - worst practices?

查看:24
本文介绍了Scala 演员 - 最糟糕的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在 Scala 中使用 actor 感到有点不安全.我已经阅读了有关如何做事的文档,但我想我还需要一些 DON'T 规则才能随意使用它们.我想我害怕我会以错误的方式使用它们,我什至不会注意到它.

I feel a bit insecure about using actors in Scala. I have read documentation about how to do stuff, but I guess I would also need some DON'T rules in order to feel free to use them. I think I am afraid that I will use them in a wrong way, and I will not even notice it.

你能想到什么,如果应用,会导致破坏 Scala actor 带来的好处,甚至是错误的结果?

Can you think of something, that, if applied, would result in breaking the benefits that Scala actors bring, or even erroneous results?

推荐答案

  • 尽可能避免!?.您获得一个锁定的系统!

    总是从 Actor 子系统线程发送消息.如果这意味着通过 Actor.actor 方法创建一个瞬态 Actor,那就这样吧:

    Always send a message from an Actor-subsystem thread. If this means creating a transient Actor via the Actor.actor method then so be it:

    case ButtonClicked(src) =>Actor.actor { 控制器!SaveTrade(trdFld.text) }

    向演员的反应添加任何其他消息" 处理程序.否则无法确定您是否向错误的参与者发送消息:

    Add an "any other message" handler to your actor's reactions. Otherwise it is impossible to figure out if you are sending a message to the wrong actor:

    case other =>log.warning(this + "收到了意外消息" + other

    不要将 Actor.actor 用于您的主要演员,而是使用 sublcass Actor.这样做的原因是只有通过子类化才能提供合理的 toString 方法.同样,如果您的日志中充斥着以下语句,则调试 Actor 将非常困难:

    Don't use Actor.actor for your primary actors, sublcass Actor instead. The reason for this is that it is only by subclassing that you can provide a sensible toString method. Again, debugging actors is very difficult if your logs are littered with statements like:

    12:03 [INFO] 发送 RequestTrades(2009-10-12) 到 scala.actors.Actor$anonfun$1

    记录系统中的参与者,明确说明他们将收到什么消息以及他们应该如何计算响应.使用参与者会导致标准过程(通常封装在方法中)转换为跨多个参与者反应的逻辑.没有好的文档很容易迷路.

    Document the actors in your system, explicitly stating what messages they will receive and precisely how they should calculate the response. Using actors results in the conversion of a standard procedure (normally encapsulated within a method) to become logic spread across multiple actor's reactions. It is easy to get lost without good documentation.

    始终确保您可以在其 react 循环之外与您的 actor 通信以找到其状态.例如,我总是声明一个通过 MBean 调用的方法,它看起来像下面的代码片段.否则很难判断您的 actor 是否正在运行、是否已关闭、是否有大量消息等.

    Always make sure you can communicate with your actor outside of its react loop to find its state. For example, I always declare a method to be invoked via an MBean which looks like the following code snippet. It can otherwise be very difficult to tell if your actor is running, has shut down, has a large queue of messages etc.

    .

    def reportState = {
      val _this = this
      synchronized {
        val msg = "%s Received request to report state with %d items in mailbox".format(
                       _this, mailboxSize) 
        log.info(msg)
      }
      Actor.actor { _this ! ReportState }
    }
    

    • 将您的演员链接在一起并使用 trapExit = true - 否则他们可能会无声无息地失败,这意味着您的程序没有按照您的想法行事,并且可能会因为消息保留而耗尽内存演员的邮箱.

      • Link your actors together and use trapExit = true - otherwise they can fail silently meaning your program is not doing what you think it is and will probably go out of memory as messages remain in the actor's mailbox.

        我认为围绕使用 actor 做出的设计决策的一些其他有趣选择已经被突出显示 这里这里

        I think that some other interesting choices around design-decisions to be made using actors have been highlighted here and here

        这篇关于Scala 演员 - 最糟糕的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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