在ray.io中存储和检索对象

时间:2020-04-01 16:38:32

标签: actor consumer ray publisher

我的光线集群在机器上运行如下:

ray start --head --redis-port=6379

我有两个需要在群集上运行的文件。 生产者p_ray.py:

import ray
ray.init(address='auto', redis_password='5241590000000000')


@ray.remote
class Counter(object):
    def __init__(self):
        self.n = 0

    def increment(self):
        self.n += 1

    def read(self):
        return self.n


counters = [Counter.remote() for i in range(4)]
[c.increment.remote() for c in counters]
futures = [c.read.remote() for c in counters]
print(futures, type(futures[0]))
obj_id = ray.put(ray.get(futures))
print(obj_id)
print(ray.get(obj_id))
while True:
    pass

消费者c_ray.py:

import ray
ray.init(address='auto', redis_password='5241590000000000')
[objs] = ray.objects()
print('OBJ-ID:', objs, 'TYPE:', type(objs))
print(ray.get([objs]))

我的目的是存储生产者的期货对象并在消费者中检索它。我可以在使用者中检索对象ID。但是,获取消费者永远不会回来。

我在做什么错了?

如何解决我的要求?

1 个答案:

答案 0 :(得分:0)

这种特殊情况可能是一个错误(我不确定100%)。我在Ray Github上创建了一个问题。

但是,这不是获取由p_ray.py创建的对象的好方法。如果您有许多对象,则管理起来将极其复杂。您可以使用detached actor实现类似的操作。 https://ray.readthedocs.io/en/latest/advanced.html#detached-actors

这个想法是创建一个独立的actor,该actor可以由在同一集群中运行的任何驱动程序/工作者检索。

p_ray.py

import ray
ray.init(address='auto', redis_password='5241590000000000')

@ray.remote
class DetachedQueue:
    def __init__(self):
        self.dict = {}
    def put(key, value):
        self.dict[key] = value
    def get(self):
        return self.dict


@ray.remote
class Counter(object):
    def __init__(self):
        self.n = 0

    def increment(self):
        self.n += 1

    def read(self):
        return self.n


queue = DetachedQueue.remote(name="queue_1", detached=True)
counters = [Counter.remote() for i in range(4)]
[c.increment.remote() for c in counters]
futures = [c.read.remote() for c in counters]
print(futures, type(futures[0]))
queue.put.remote("key", ray.get(futures)))
while True:
    pass

c_ray.py:

import ray
ray.init(address='auto', redis_password='5241590000000000')
queue = ray.util.get_actor("queue_1")
print(ray.get(queue.get.remote()))
相关问题