首页 > 解决方案 > 无法理解 ray 中的并行代码输出

问题描述

如果我理解正确,以下代码应该并行运行

@ray.remote
class Worker:
  ...

  def train(self, item, i):
    time.sleep(i)
    logging.info(f'{i} {item}')
...

worker = Worker.remote()
list = ['a', 'b', 'c']
results = ray.get([worker.train.remote(item, len(list) - idx) for idx, item in enumerate(list)])
print(results)
logging.info("successful print")

这应该输出

1 c
2 b
3 a
[1,2,3]

但是,这会输出:

3 a
2 b
[3,2,1]

我是使用 ray 的新手,无法理解这种行为。如果有人能指出我正确的方向,那就太好了!

标签: python-3.xray

解决方案


Actor is running within a single process. As a result, worker.train.remote() will be synchronous.


推荐阅读