/
actor_pool.py
340 lines (288 loc) · 12.6 KB
/
actor_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from typing import List, Callable, Any
import ray
from ray.util.annotations import DeveloperAPI
@DeveloperAPI
class ActorPool:
"""Utility class to operate on a fixed pool of actors.
Arguments:
actors: List of Ray actor handles to use in this pool.
Examples:
>>> import ray
>>> from ray.util.actor_pool import ActorPool
>>> @ray.remote # doctest: +SKIP
>>> class Actor: # doctest: +SKIP
... ... # doctest: +SKIP
>>> a1, a2 = Actor.remote(), Actor.remote() # doctest: +SKIP
>>> pool = ActorPool([a1, a2]) # doctest: +SKIP
>>> print(list(pool.map(lambda a, v: a.double.remote(v), # doctest: +SKIP
... [1, 2, 3, 4]))) # doctest: +SKIP
[2, 4, 6, 8]
"""
def __init__(self, actors: list):
ray._private.usage.usage_lib.record_library_usage("util.ActorPool")
# actors to be used
self._idle_actors = list(actors)
# get actor from future
self._future_to_actor = {}
# get future from index
self._index_to_future = {}
# next task to do
self._next_task_index = 0
# next task to return
self._next_return_index = 0
# next work depending when actors free
self._pending_submits = []
def map(self, fn: Callable[[Any], Any], values: List[Any]):
"""Apply the given function in parallel over the actors and values.
This returns an ordered iterator that will return results of the map
as they finish. Note that you must iterate over the iterator to force
the computation to finish.
Arguments:
fn: Function that takes (actor, value) as argument and
returns an ObjectRef computing the result over the value. The
actor will be considered busy until the ObjectRef completes.
values: List of values that fn(actor, value) should be
applied to.
Returns:
Iterator over results from applying fn to the actors and values.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> print(list(pool.map(lambda a, v: a.double.remote(v),
... [1, 2, 3, 4]))) # doctest: +SKIP
[2, 4, 6, 8]
"""
# Ignore/Cancel all the previous submissions
# by calling `has_next` and `gen_next` repeteadly.
while self.has_next():
try:
self.get_next(timeout=0, ignore_if_timedout=True)
except TimeoutError:
pass
for v in values:
self.submit(fn, v)
while self.has_next():
yield self.get_next()
def map_unordered(self, fn: Callable[[Any], Any], values: List[Any]):
"""Similar to map(), but returning an unordered iterator.
This returns an unordered iterator that will return results of the map
as they finish. This can be more efficient that map() if some results
take longer to compute than others.
Arguments:
fn: Function that takes (actor, value) as argument and
returns an ObjectRef computing the result over the value. The
actor will be considered busy until the ObjectRef completes.
values: List of values that fn(actor, value) should be
applied to.
Returns:
Iterator over results from applying fn to the actors and values.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> print(list(pool.map_unordered(lambda a, v: a.double.remote(v),
... [1, 2, 3, 4]))) # doctest: +SKIP
[6, 2, 4, 8]
"""
# Ignore/Cancel all the previous submissions
# by calling `has_next` and `gen_next_unordered` repeteadly.
while self.has_next():
try:
self.get_next_unordered(timeout=0)
except TimeoutError:
pass
for v in values:
self.submit(fn, v)
while self.has_next():
yield self.get_next_unordered()
def submit(self, fn, value):
"""Schedule a single task to run in the pool.
This has the same argument semantics as map(), but takes on a single
value instead of a list of values. The result can be retrieved using
get_next() / get_next_unordered().
Arguments:
fn: Function that takes (actor, value) as argument and
returns an ObjectRef computing the result over the value. The
actor will be considered busy until the ObjectRef completes.
value: Value to compute a result for.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 2) # doctest: +SKIP
>>> print(pool.get_next(), pool.get_next()) # doctest: +SKIP
2, 4
"""
if self._idle_actors:
actor = self._idle_actors.pop()
future = fn(actor, value)
future_key = tuple(future) if isinstance(future, list) else future
self._future_to_actor[future_key] = (self._next_task_index, actor)
self._index_to_future[self._next_task_index] = future
self._next_task_index += 1
else:
self._pending_submits.append((fn, value))
def has_next(self):
"""Returns whether there are any pending results to return.
Returns:
True if there are any pending results not yet returned.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> print(pool.has_next()) # doctest: +SKIP
True
>>> print(pool.get_next()) # doctest: +SKIP
2
>>> print(pool.has_next()) # doctest: +SKIP
False
"""
return bool(self._future_to_actor)
def get_next(self, timeout=None, ignore_if_timedout=False):
"""Returns the next pending result in order.
This returns the next result produced by submit(), blocking for up to
the specified timeout until it is available.
Returns:
The next result.
Raises:
TimeoutError if the timeout is reached.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> print(pool.get_next()) # doctest: +SKIP
2
"""
if not self.has_next():
raise StopIteration("No more results to get")
if self._next_return_index >= self._next_task_index:
raise ValueError(
"It is not allowed to call get_next() after get_next_unordered()."
)
future = self._index_to_future[self._next_return_index]
timeout_msg = "Timed out waiting for result"
raise_timeout_after_ignore = False
if timeout is not None:
res, _ = ray.wait([future], timeout=timeout)
if not res:
if not ignore_if_timedout:
raise TimeoutError(timeout_msg)
else:
raise_timeout_after_ignore = True
del self._index_to_future[self._next_return_index]
self._next_return_index += 1
future_key = tuple(future) if isinstance(future, list) else future
i, a = self._future_to_actor.pop(future_key)
self._return_actor(a)
if raise_timeout_after_ignore:
raise TimeoutError(
timeout_msg + ". The task {} has been ignored.".format(future)
)
return ray.get(future)
def get_next_unordered(self, timeout=None, ignore_if_timedout=False):
"""Returns any of the next pending results.
This returns some result produced by submit(), blocking for up to
the specified timeout until it is available. Unlike get_next(), the
results are not always returned in same order as submitted, which can
improve performance.
Returns:
The next result.
Raises:
TimeoutError if the timeout is reached.
Examples:
>>> from ray.util.actor_pool import ActorPool
>>> pool = ActorPool(...) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 2) # doctest: +SKIP
>>> print(pool.get_next_unordered()) # doctest: +SKIP
4
>>> print(pool.get_next_unordered()) # doctest: +SKIP
2
"""
if not self.has_next():
raise StopIteration("No more results to get")
# TODO(ekl) bulk wait for performance
res, _ = ray.wait(list(self._future_to_actor), num_returns=1, timeout=timeout)
timeout_msg = "Timed out waiting for result"
raise_timeout_after_ignore = False
if res:
[future] = res
else:
if not ignore_if_timedout:
raise TimeoutError(timeout_msg)
else:
raise_timeout_after_ignore = True
i, a = self._future_to_actor.pop(future)
self._return_actor(a)
del self._index_to_future[i]
self._next_return_index = max(self._next_return_index, i + 1)
if raise_timeout_after_ignore:
raise TimeoutError(
timeout_msg + ". The task {} has been ignored.".format(future)
)
return ray.get(future)
def _return_actor(self, actor):
self._idle_actors.append(actor)
if self._pending_submits:
self.submit(*self._pending_submits.pop(0))
def has_free(self):
"""Returns whether there are any idle actors available.
Returns:
True if there are any idle actors and no pending submits.
Examples:
>>> @ray.remote # doctest: +SKIP
>>> class Actor: # doctest: +SKIP
... ... # doctest: +SKIP
>>> a1 = Actor.remote() # doctest: +SKIP
>>> pool = ActorPool(a1) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> print(pool.has_free()) # doctest: +SKIP
False
>>> print(pool.get_next()) # doctest: +SKIP
2
>>> print(pool.has_free()) # doctest: +SKIP
True
"""
return len(self._idle_actors) > 0 and len(self._pending_submits) == 0
def pop_idle(self):
"""Removes an idle actor from the pool.
Returns:
An idle actor if one is available.
None if no actor was free to be removed.
Examples:
>>> @ray.remote # doctest: +SKIP
>>> class Actor: # doctest: +SKIP
... ... # doctest: +SKIP
>>> a1 = Actor.remote() # doctest: +SKIP
>>> pool = ActorPool([a1]) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> print(pool.pop_idle()) # doctest: +SKIP
None
>>> print(pool.get_next()) # doctest: +SKIP
2
>>> print(pool.pop_idle()) # doctest: +SKIP
<ptr to a1>
"""
if self.has_free():
return self._idle_actors.pop()
return None
def push(self, actor):
"""Pushes a new actor into the current list of idle actors.
Examples:
>>> @ray.remote # doctest: +SKIP
>>> class Actor: # doctest: +SKIP
... ... # doctest: +SKIP
>>> a1, b1 = Actor.remote(), Actor.remote() # doctest: +SKIP
>>> pool = ActorPool([a1]) # doctest: +SKIP
>>> pool.submit(lambda a, v: a.double.remote(v), 1) # doctest: +SKIP
>>> print(pool.get_next()) # doctest: +SKIP
2
>>> pool2 = ActorPool([b1]) # doctest: +SKIP
>>> pool2.push(pool.pop_idle()) # doctest: +SKIP
"""
busy_actors = []
if self._future_to_actor.values():
_, busy_actors = zip(*self._future_to_actor.values())
if actor in self._idle_actors or actor in busy_actors:
raise ValueError("Actor already belongs to current ActorPool")
else:
self._return_actor(actor)