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

Ray 2.2多任务ray.wait()处理报错:object_refs参数异常排查

问题排查与修复

核心错误分析

你的代码存在两个关键问题,共同引发了异常:

  1. 索引越界错误:ray.wait(num_returns=1) 返回的 done_id 永远是长度为1的列表,你在 elif 中访问 done_id[1] 会直接触发 IndexError,这个异常可能间接导致后续 ray.wait 调用传入非法参数,进而出现 'object_refs' must either be an ObjectRef or a list of ObjectRefs 错误。

  2. ObjectRef合法性问题:如果 A 或 B 没有用 @ray.remote 装饰器标记,调用 .remote() 不会返回合法的 ObjectRef,而是普通函数返回值,此时 result_ids 列表中的元素不符合 ray.wait 的参数要求,会直接抛出上述异常。

修复后的代码

# 确保A和B已用@ray.remote装饰
@ray.remote
def A(a1, a2):
    # 任务逻辑
    pass

@ray.remote
def B(b1, b2):
    # 任务逻辑
    pass

task_1 = A.remote(a1,a2)
task_2 = B.remote(b1,b2)
result_ids = [task_1, task_2]

while len(result_ids):
    done_id, result_ids = ray.wait(result_ids, num_returns=1, timeout=None)
    # 仅访问done_id[0],因为num_returns=1
    if done_id[0] == task_1:
        print('task one completed')
    elif done_id[0] == task_2:
        print('task_2 is completed')

print('COMPLETED TASKS')

关键修复点

  • 移除对 done_id[1] 的访问,统一用 done_id[0] 判断完成的任务
  • 确保 A 和 B 函数被 @ray.remote 装饰,保证 .remote() 返回合法的 ObjectRef
  • 将 is 改为 ==,Ray的ObjectRef重载了==运算符,用等值比较判断ObjectRef相等更准确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:25:41