Skip to content

Instantly share code, notes, and snippets.

@planetceres
Last active February 25, 2020 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save planetceres/0c0fc6d40de30406f896a0e95de37c30 to your computer and use it in GitHub Desktop.
Save planetceres/0c0fc6d40de30406f896a0e95de37c30 to your computer and use it in GitHub Desktop.
Ray ActorPool to operate on a fixed pool of actors

Ray ActorPool

An actor pool is a utility class similar to multiprocessing.Pool that lets you schedule Ray tasks over a fixed pool of actors.

References:

import ray
# num_cpus = psutil.cpu_count(logical=False)
num_cpus = len(psutil.Process().cpu_affinity())
print('Initializing Ray on {} cpus.'.format(num_cpus))
ray.init(num_cpus=num_cpus)
print('Ray initialization {} on {} cores.'.format('successful' if ray.is_initialized() else 'failed', num_cpus))
@ray.remote
class MyActor(object):
def __init__(self):
pass
def func(self, x):
return x * 2
actors = [MyActor.remote() for _ in range(num_cpus)]
actor_pool = ray.util.ActorPool(actors)
def ray_batch(args_list):
res = actor_pool.map(lambda a, v: a.func.remote(v), args_list)
return res
args_list = [range(10)]
results_list = ray_batch(args_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment