Ray 2.2多任务ray.wait()处理报错:object_refs参数异常排查
问题排查与修复
核心错误分析
你的代码存在两个关键问题,共同引发了异常:
索引越界错误:
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错误。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
相关产品推荐
相关产品推荐

