def remote_worker_caller(executor: ActorPool, sleep_time: int, number_invokes: int): start = time.time() [ executor.submit(lambda a, v: a.computation.remote(v), sleep_time) for n in range(number_invokes) ] starttime = time.time() - start while (executor.has_next()): executor.get_next() return starttime, time.time() - start
def test_multiple_returns(init): @ray.remote class Foo(object): @ray.method(num_returns=2) def bar(self): return 1, 2 pool = ActorPool([Foo.remote() for _ in range(2)]) for _ in range(4): pool.submit(lambda a, v: a.bar.remote(), None) while pool.has_next(): assert pool.get_next(timeout=None) == [1, 2]
def test_get_next(init): @ray.remote class MyActor: def __init__(self): pass def f(self, x): return x + 1 def double(self, x): return 2 * x actors = [MyActor.remote() for _ in range(4)] pool = ActorPool(actors) for i in range(5): pool.submit(lambda a, v: a.f.remote(v), i) assert pool.get_next() == i + 1
def test_pop_idle(init): @ray.remote class MyActor: def __init__(self): pass def f(self, x): return x + 1 def double(self, x): return 2 * x actors = [MyActor.remote()] pool = ActorPool(actors) pool.submit(lambda a, v: a.double.remote(v), 1) assert pool.pop_idle() is None assert pool.has_free() is False # actor is busy assert pool.get_next() == 2 assert pool.has_free() pool.pop_idle() # removes actor from pool assert pool.has_free() is False # no more actors in pool
@ray.remote class PoolActor: def __init__(self): self.id = str(uuid4()) def computation(self, num): print(f'Actor with id {self.id} waiting for {num} sec') for x in range(num): sleep(1) print(f'Actor with id {self.id} slept for {x} sec') return num # Create actors and add them to the pool a1, a2, a3 = PoolActor.remote(), PoolActor.remote(), PoolActor.remote() pool = ActorPool([a1, a2, a3]) print(list(pool.map(lambda a, v: a.computation.remote(v), [3, 4, 5, 4]))) pool.submit(lambda a, v: a.computation.remote(v), 3) pool.submit(lambda a, v: a.computation.remote(v), 4) pool.submit(lambda a, v: a.computation.remote(v), 5) pool.submit(lambda a, v: a.computation.remote(v), 4) print(pool.get_next()) print(pool.get_next()) print(pool.get_next()) print(pool.get_next())