Source code for thread_chunks._actor
1from typing import Tuple, Any, Callable
2
[docs]
3class LabelledActor():
4 """A ray actor that can execute the function `func` along with a label to
5 allow an ordering of calls to be re-established. Additionally, as the actor
6 stores `func` so that copying of `func` only occurs on initialisation opposed
7 to every call of `func`.
8 """
[docs]
9 def __init__(self, func: Callable):
10 """Initialises a :class:`LabelledActor`
11
12 Parameters
13 ----------
14 func : Callable
15 The function saved by the actor.
16 """
17 self._func = func
[docs]
18 def run(self,
19 label: Any,
20 *args: Any
21 ) -> Tuple[Any, Any]:
22 """A wrapping that allows labelled execution of the function.
23
24 Parameters
25 ----------
26 label : Any
27 The label assigned to the execution.
28 *args : Any
29 The arguments to pass to the stored function `func`.
30
31 Returns
32 -------
33 Tuple[Any, Any]
34 The label followed by the return of `func`.
35 """
36 return label, self._func(*args)
[docs]
37 def set_func(self, func: Callable):
38 """Updates the function `func` stored by the actor.
39
40 Parameters
41 ----------
42 func : Callable
43 The new function.
44 """
45 self._func = func
[docs]
46 def get_func(self) -> Callable:
47 """Gets the function `func` stored by the actor.
48
49 Returns
50 -------
51 Callable
52 The stored function.
53 """
54 return self._func