You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Concurrent::Promise.all?失效:回调Proc未触发技术求助

解决Concurrent::Promise.all?后续Proc未执行的问题

嘿,我一眼就看到问题出在哪了——你在then块里只是返回了Proc对象,但根本没调用它!这就是为什么你的逻辑始终没执行的原因。

问题分析

你的代码里:

Concurrent::Promise.all?([cbr_promise, bitfinex_promise]).then { proc }

这里的then块仅仅把proc这个对象返回给了Promise链,但并没有触发它的执行。Concurrent::Promise的then方法需要你在块里直接执行逻辑,或者显式调用Proc的call方法。

修正方案

有两种简单的修正方式:

  1. 直接在then块里调用proc
cbr_promise = Concurrent::Promise.execute { CbrRatesService.call }
bitfinex_promise = Concurrent::Promise.execute { BitfinexService.call }

proc = Proc.new do
  puts 10
end

# 关键:调用proc.call而不是只返回proc
Concurrent::Promise.all?([cbr_promise, bitfinex_promise]).then { proc.call }
  1. 把逻辑直接写在then块里(更简洁)
    如果你不需要复用这个Proc,完全可以省去单独定义的步骤,直接把逻辑写在then的块中:
cbr_promise = Concurrent::Promise.execute { CbrRatesService.call }
bitfinex_promise = Concurrent::Promise.execute { BitfinexService.call }

Concurrent::Promise.all?([cbr_promise, bitfinex_promise]).then do
  puts 10
  # 这里写你的计算逻辑
end

关于轮询的疑问

完全不需要写100ms轮询的循环!Concurrent::Promise.all?的设计初衷就是等待所有传入的Promise完成后,自动执行后续的then逻辑,轮询不仅多余,还会占用不必要的系统资源,完全是画蛇添足。

Sidekiq环境的小提示

在Sidekiq中使用concurrent-ruby时,只要确保你的Promise执行逻辑没有长时间阻塞(不过这里的异步等待是框架处理的,没问题),重点还是要保证Promise链的回调逻辑被正确触发——也就是上面说的,要主动调用你的Proc或者直接写逻辑在块里。

内容的提问来源于stack exchange,提问作者Alexander Kireyev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:07:36