如何更改组合期货中的超时?
在 akka 1.1 文档中关于 组合 futures,我想知道如何以编程方式设置生成的 future 的超时。我知道我可以在 akka.conf 中调整全局超时,但我只想针对这段代码进行适当的调整。
示例代码如下所示,
val f1 = actor1 !!! msg1
val f2 = actor2 !!! msg2
val f3 = for {
a: Int <- f1
b: Int <- f2
c: String <- actor3 !!! (a + b)
} yield c
val result = f3.get()
我说的对吗,akka 在这个例子中总共创建了四个 future?
- 每条消息发送到 actor1、2 和 3
- 一个包装这三个 future
在第一种情况下很容易更改超时,例如,
val f1 = actor1 !!! (msg1, 15000) // sets timeout to 15 seconds
但如何设置包装 future 的超时?有什么想法吗?
In the example from the akka 1.1 documentation on composing futures, I'm wondering how it's possible to programmatically set the timeout of the generated future. I'm aware that I can adjust the global timeout in akka.conf, but I want to do it in place only for this piece of code.
The example code looks as follows
val f1 = actor1 !!! msg1
val f2 = actor2 !!! msg2
val f3 = for {
a: Int <- f1
b: Int <- f2
c: String <- actor3 !!! (a + b)
} yield c
val result = f3.get()
Am I right that akka creates a total of four futures in this example?
- One for each message send to actor1, 2 and 3
- One to wrap these three futures
It's easy to change the timeouts in the first case, e.g.
val f1 = actor1 !!! (msg1, 15000) // sets timeout to 15 seconds
but how can I Set the timeout for the wrapping future? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信实际上总共有 6 个 future,其中 3 个来自 actor,每个 flatMap/map 方法有 1 个。
3 个演员未来的超时设置就像您所说的那样,但是 3 个包装器未来将继承 for-compression 的每个部分右侧的未来的超时。但是,返回的 future 将使用 for 理解中第一个 future 的超时。
对于 Akka 2.0,只要测试期间没有出现问题,这将更改为允许指定隐式超时。
There would actually be 6 futures total I believe, the 3 you got from the actors, and 1 for each flatMap/map method.
The timeout for the 3 actor futures are set just like you stated, but the 3 wrapper futures will inherit the timeout from the future on the right hand side of each section of the for-comprehension. However, the returned future will use the timeout of the first future in the for-comprehension.
For Akka 2.0 this will change to allow an implicit timeout to be specified, as long as no issues pop up during testing.