如何让Actor的task任务并行执行

官网链接:Actors — Ray 2.2.0

官网代码示例:
// Create ten Counter actors.
std::vector<ray::ActorHandle> counters;
for (int i = 0; i < 10; i++) {
counters.emplace_back(ray::Actor(CreateCounter).Remote());
}

// Increment the first Counter five times. These tasks are executed serially
// and share state.
object_refs.clear();
for (int i = 0; i < 5; i++) {
object_refs.emplace_back(counters[0].Task(&Counter::Increment).Remote());
}
// prints 2, 3, 4, 5, 6
results = ray::Get(object_refs);
for (const auto &result : results) {
std::cout << *result;
}

问题:如何让counters[0].Task并行执行,而不是串行?

https://docs.ray.io/en/latest/ray-core/actors/async_api.html#threaded-actors
可以尝试通过设置max_concurrency来使用Threaded Actors:

@ray.remote
class ThreadedActor:
    def task_1(self): print("I'm running in a thread!")
    def task_2(self): print("I'm running in another thread!")

a = ThreadedActor.options(max_concurrency=2).remote()
ray.get([a.task_1.remote(), a.task_2.remote()])
1 个赞

有C++版本用法么

如果是Python, 建议使用anysc actor, 或者concurrency group。
如果是C++的,应该还没有类似的支持,方便的话你可以在github上file一个issue,后续会有相关同学进行支持。