为什么演员会“问”?模式被认为是反模式或“代码气味?” [英] Why is the actor "ask" pattern considered an anti-pattern or "code smell?"

查看:92
本文介绍了为什么演员会“问”?模式被认为是反模式或“代码气味?”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我收集到的信息来看,询问模式被认为是一种不好的做法,应避免使用。相反,推荐的模式是每个请求的参与者模型。但是,这对我来说没有意义,因为问模式可以做到这一点-它会为每个请求创建一个轻量级的actor。那么,为什么这会被认为是不好的,尤其是在期货的可组合性更好并且能够更优雅地处理多个发送/接收的排序规则时呢?

From what I've gathered, the "ask" pattern is considered a bad practice and should be avoided. Instead, the recommended pattern is the "actor per request" model. However, this doesn't make sense to me, as the "ask" pattern does exactly this - it creates a lightweight actor per request. So why is this then considered bad, especially when futures are far more composable and are able to more elegantly handle the collation of multiple send/receives?

推荐答案

来自Akka 文档


使用Ask可能会对性能产生影响,因为某些东西需要
来跟踪其超时时间,因此需要
能够桥接一个Promise放入ActorRef中,并且还需要通过远程处理达到
。因此,始终希望告诉性能,并且仅在必要时询问

"There are performance implications of using ask since something needs to keep track of when it times out, there needs to be something that bridges a Promise into an ActorRef and it also needs to be reachable through remoting. So always prefer tell for performance, and only ask if you must."

但是有时您希望从演员外部发送消息,在这种情况下,您可以使用询问。使用 ask 可以确保您在指定的超时时间内得到响应,有时这就是您想要的。但是,当您使用 ask 模式时,您应该问自己一个问题,是否可以改为使用 Future s。

But sometimes you want to send a message from outside of an actor in which case you can use ask. Using ask will guarantee that you get a response within the specified timeout and sometimes that's what you want. However, when you use ask pattern you should ask yourself a question whether you could just use Futures instead.

有一个要问的地方,但是由于上述原因,它的用途应该非常有限。

There is a place for ask but it should have very limited use due to the aforementioned reasons.

您不必为每个请求使用actor。有些演员本来会长寿,有些则不会。如果演员执行潜在的危险或阻止操作,则可能需要为每个请求创建一个。符合您应用逻辑的任何内容。

You don't have to use actor per request. Some actors are meant to be long lived and some not. If an actor performs a potentially dangerous or blocking operation you might want to create one per request. Whatever fits your application logic.

这篇关于为什么演员会“问”?模式被认为是反模式或“代码气味?”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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