Parallel Requests Utilities¶

class ray.rllib.execution.parallel_requests.AsyncRequestsManager(workers: List[ray.actor.ActorHandle], max_remote_requests_in_flight_per_worker: int = 2, ray_wait_timeout_s: Optional[float] = 0.0, return_object_refs: bool = False)[source]¶

A manager for asynchronous requests to actors.

Parameters
  • workers – A list of ray remote workers to operate on. These workers must have an apply method which takes a function and a list of arguments to that function.

  • max_remote_requests_in_flight_per_worker – The maximum number of remote requests that can be in flight per actor. Any requests made to the pool that cannot be scheduled because the max_remote_requests_in_flight_per_worker per actor has been reached will be queued.

  • ray_wait_timeout_s – The maximum amount of time to wait for inflight requests to be done and ready when calling AsyncRequestsManager.get_ready_results().

Example

>>> import time 
>>> import ray 
>>> from ray.rllib.execution.parallel_requests import AsyncRequestsManager 
>>>
>>> @ray.remote 
... class MyActor: 
...    def apply(self, fn, *args: List[Any], **kwargs: Dict[str, Any]) -> Any: 
...        return fn(*args, **kwargs) 
...
...    def task(self, a: int, b: int) -> Any: 
...        time.sleep(0.5) 
...        return a + b 
>>>
>>> workers = [MyActor.remote() for _ in range(3)] 
>>> manager = AsyncRequestsManager(workers, 
...                                max_remote_requests_in_flight_per_worker=2) 
>>> manager.call(lambda worker, a, b: worker.task(a, b), fn_args=[1, 2]) 
>>> print(manager.get_ready()) 
>>> manager.call(lambda worker, a, b: worker.task(a, b), 
...                fn_kwargs={"a": 1, "b": 2}) 
>>> time.sleep(2) # Wait for the tasks to finish. 
>>> print(manager.get_ready()) 
call(remote_fn: Callable, *, actor: Optional[ray.actor.ActorHandle] = None, fn_args: Optional[List[Any]] = None, fn_kwargs: Optional[Dict[str, Any]] = None) bool[source]¶

Call remote function on any available Actor or - if provided - on actor.

Parameters
  • remote_fn – The remote function to call. The function must have a signature of: [RolloutWorker, args, kwargs] and return Any.

  • actor – The actor to call the remote function on.

  • fn_args – The arguments to pass to the remote function.

  • fn_kwargs – The keyword arguments to pass to the remote function.

Returns

True if the remoted_fn was scheduled on an actor. False if it was unable to be scheduled.

Raises
  • ValueError – If actor has not been added to the manager.

  • ValueError – If there are no actors available to submit a request to.

call_on_all_available(remote_fn: Callable, *, fn_args: Optional[List[Any]] = None, fn_kwargs: Optional[Dict[str, Any]] = None) int[source]¶

Call remote_fn on all available workers.

Available workers are those that have less than the maximum requests currently in-flight. The max. requests is set via the constructor’s max_remote_requests_in_flight_per_worker argument.

Parameters
  • remote_fn – The remote function to call. The function must have a signature of: [RolloutWorker, args, kwargs] and return Any.

  • fn_args – The arguments to pass to the remote function.

  • fn_kwargs – The keyword arguments to pass to the remote function.

Returns

The number of remote calls of remote_fn that were able to be launched.

get_ready() Dict[ray.actor.ActorHandle, List[Any]][source]¶

Get results that are ready to be returned.

Returns

A dictionary of actor handles to lists of returns from tasks that were previously submitted to this actor pool that are now ready to be returned. If self._return_object_refs is True, return only the object store references, not the actual return values.

add_workers(new_workers: Union[List[ray.actor.ActorHandle], ray.actor.ActorHandle]) None[source]¶

Add a new worker to the manager

Parameters

new_workers – The actors to add

remove_workers(workers: Union[List[ray.actor.ActorHandle], ray.actor.ActorHandle], remove_in_flight_requests: bool = False) None[source]¶

Make workers unschedulable and remove them from this manager.

Parameters
  • workers – The actors to remove.

  • remove_in_flight_requests – If True, will remove the actor completely from this manager, even all of its in-flight requests. Useful for removing a worker after some detected failure.

get_manager_statistics() Dict[str, Any][source]¶

Get statistics about the the manager

Some of the statistics include the number of actors that are available, the number of pending inflight requests, and the number of pending requests to be scheduled on the available actors.

Returns

A dictionary of statistics about the manager.