scala Akka中询问和告诉之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22841653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:10:11  来源:igfitidea点击:

Difference between ask and tell in Akka?

scalaasynchronousakka

提问by blue-sky

Reading the Scala doc I am having difficulty understanding the difference between ask and tell.

阅读 Scala 文档我很难理解 ask 和 tell 之间的区别。

http://doc.akka.io/docs/akka/snapshot/scala/actors.htmlstates :

http://doc.akka.io/docs/akka/snapshot/scala/actors.html状态:

! means “fire-and-forget”, e.g. send a message asynchronously and return immediately. Also known as tell.

? sends a message asynchronously and returns a Future representing a possible reply. Also known as ask.

!表示“即发即忘”,例如异步发送消息并立即返回。也称为告诉。

? 异步发送消息并返回表示可能回复的 Future。也称为问。

If the actor I'm using spawns a web request then what is the difference between ask and tell ? In both cases the request will be spawned asynchronously and must wait for a response, in other words how can "tell" return immediately if the actor is invoking a web service and awaiting a response ?

如果我使用的演员产生一个网络请求,那么 ask 和 tell 之间有什么区别?在这两种情况下,请求都将异步产生并且必须等待响应,换句话说,如果参与者正在调用 Web 服务并等待响应,如何“告诉”立即返回?

采纳答案by AmigoNico

It sounds like you already know the basic difference between askand tell, but don't understand how tellcould be used to involve other actors in handling HTTP requests.

听起来您已经知道ask和之间的基本区别tell,但不明白如何tell用于让其他参与者参与处理 HTTP 请求。

In order for it to make sense to use tellin your HTTP request handlers, you have to use an HTTP server that does not require that request handlers returntheir responses. Sprayis such an HTTP server.

为了让它tell在您的 HTTP 请求处理程序中使用有意义,您必须使用不需要请求处理程序返回其响应的 HTTP 服务器。 Spray就是这样一个 HTTP 服务器。

In Spray a request handler does not return its response; it is given a RequestContext object, and responding to the request involves invoking some method on it. You can simply send that RequestContext to another actor, which can then respond to the request:

在 Spray 中,请求处理程序不返回其响应;它被赋予一个 RequestContext 对象,响应请求涉及调用它的一些方法。您可以简单地将该 RequestContext 发送给另一个actor,然后它可以响应请求:

path("foo") {
  rc => rc complete "foo"  // respond here
} ~
path("bar") {
  rc => barActor ! DoBar(rc)  // send it to bar; NO RESPONSE HERE
}

Then the actor referred to by barActor could say

那么 barActor 所指的 actor 可以说

case DoBar(rc) =>
  rc complete "bar"  // respond here, in another actor

The fact that Spray packages up the request context into an object that can be passed around and completed from any actor is a great fit for the actor model. If, on the other hand, your web framework requires that the invoked handler return the response, then if you want to involve another actor your only choice is to use ask.

Spray 将请求上下文打包成一个可以从任何角色传递和完成的对象,这一事实非常适合角色模型。另一方面,如果您的 Web 框架要求调用的处理程序返回响应,那么如果您想涉及另一个参与者,您唯一的选择就是使用ask.

Typesafe announced that Playwill soon use Spray underneath. I hope that means that Playwill then allow requests to be sent along to other actors for processing.

Typesafe 宣布Play将很快在下面使用 Spray。我希望这意味着这Play将允许将请求发送给其他参与者进行处理。

回答by Richard Close

The difference between askand tellis from the point of view of the message sender (which is not necessarily an actor). askwill send the message and return a future, which can be awaited until timeout or a reply is received, tellwill send the message and return immediately.

ask和之间的区别tell是从消息发送者(不一定是参与者)的角度来看的。ask将发送消息并返回一个未来,可以等待直到超时或收到回复,tell将发送消息并立即返回。

In the case of ask, the actor that receives the message should reply to the sender when the operation is completed.

在 的情况下ask,接收消息的参与者应在操作完成后回复发送者。