forked from carson-katri/dream-textures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.py
216 lines (186 loc) · 7.41 KB
/
actor.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
from multiprocessing import Queue, Lock, current_process, get_context
import multiprocessing.synchronize
import enum
import traceback
import threading
from typing import Type, TypeVar, Generator
import site
import sys
from ..absolute_path import absolute_path
from .future import Future
def _load_dependencies():
site.addsitedir(absolute_path(".python_dependencies"))
deps = sys.path.pop(-1)
sys.path.insert(0, deps)
if current_process().name == "__actor__":
_load_dependencies()
class ActorContext(enum.IntEnum):
"""
The context of an `Actor` object.
One `Actor` instance is the `FRONTEND`, while the other instance is the backend, which runs in a separate process.
The `FRONTEND` sends messages to the `BACKEND`, which does work and returns a result.
"""
FRONTEND = 0
BACKEND = 1
class Message:
"""
Represents a function signature with a method name, positonal arguments, and keyword arguments.
Note: All arguments must be picklable.
"""
def __init__(self, method_name, args, kwargs):
self.method_name = method_name
self.args = args
self.kwargs = kwargs
CANCEL = "__cancel__"
END = "__end__"
def _start_backend(cls, message_queue, response_queue):
cls(
ActorContext.BACKEND,
message_queue=message_queue,
response_queue=response_queue
).start()
class TracedError(BaseException):
def __init__(self, base: BaseException, trace: str):
self.base = base
self.trace = trace
T = TypeVar('T', bound='Actor')
class Actor:
"""
Base class for specialized actors.
Uses queues to send actions to a background process and receive a response.
Calls to any method declared by the frontend are automatically dispatched to the backend.
All function arguments must be picklable.
"""
_message_queue: Queue
_response_queue: Queue
_lock: multiprocessing.synchronize.Lock
_shared_instance = None
# Methods that are not used for message passing, and should not be overridden in `_setup`.
_protected_methods = {
"start",
"close",
"is_alive",
"can_use",
"shared"
}
def __init__(self, context: ActorContext, message_queue: Queue = None, response_queue: Queue = None):
self.context = context
self._message_queue = message_queue if message_queue is not None else get_context('spawn').Queue(maxsize=1)
self._response_queue = response_queue if response_queue is not None else get_context('spawn').Queue(maxsize=1)
self._setup()
self.__class__._shared_instance = self
def _setup(self):
"""
Setup the Actor after initialization.
"""
match self.context:
case ActorContext.FRONTEND:
self._lock = Lock()
for name in filter(lambda name: callable(getattr(self, name)) and not name.startswith("_") and name not in self._protected_methods, dir(self)):
setattr(self, name, self._send(name))
case ActorContext.BACKEND:
pass
@classmethod
def shared(cls: Type[T]) -> T:
return cls._shared_instance or cls(ActorContext.FRONTEND).start()
def start(self: T) -> T:
"""
Start the actor process.
"""
match self.context:
case ActorContext.FRONTEND:
self.process = get_context('spawn').Process(target=_start_backend, args=(self.__class__, self._message_queue, self._response_queue), name="__actor__", daemon=True)
self.process.start()
case ActorContext.BACKEND:
self._backend_loop()
return self
def close(self):
"""
Stop the actor process.
"""
match self.context:
case ActorContext.FRONTEND:
self.process.terminate()
self._message_queue.close()
self._response_queue.close()
case ActorContext.BACKEND:
pass
@classmethod
def shared_close(cls: Type[T]):
if cls._shared_instance is None:
return
cls._shared_instance.close()
cls._shared_instance = None
def is_alive(self):
match self.context:
case ActorContext.FRONTEND:
return self.process.is_alive()
case ActorContext.BACKEND:
return True
def can_use(self):
if result := self._lock.acquire(block=False):
self._lock.release()
return result
def _backend_loop(self):
while True:
self._receive(self._message_queue.get())
def _receive(self, message: Message):
try:
response = getattr(self, message.method_name)(*message.args, **message.kwargs)
if isinstance(response, Generator):
for res in iter(response):
extra_message = None
try:
extra_message = self._message_queue.get(block=False)
except:
pass
if extra_message == Message.CANCEL:
break
if isinstance(res, Future):
res.add_response_callback(lambda _, res: self._response_queue.put(res))
res.add_exception_callback(lambda _, e: self._response_queue.put(RuntimeError(repr(e))))
res.add_done_callback(lambda _: None)
else:
self._response_queue.put(res)
else:
self._response_queue.put(response)
except Exception as e:
trace = traceback.format_exc()
try:
if sys.modules[e.__module__].__file__.startswith(absolute_path(".python_dependencies")):
e = RuntimeError(repr(e))
# might be more suitable to have specific substitute exceptions for cases
# like torch.cuda.OutOfMemoryError for frontend handling in the future
except (AttributeError, KeyError):
pass
self._response_queue.put(TracedError(e, trace))
self._response_queue.put(Message.END)
def _send(self, name):
def _send(*args, _block=False, **kwargs):
future = Future()
def _send_thread(future: Future):
self._lock.acquire()
self._message_queue.put(Message(name, args, kwargs))
while not future.done:
if future.cancelled:
self._message_queue.put(Message.CANCEL)
response = self._response_queue.get()
if response == Message.END:
future.set_done()
elif isinstance(response, TracedError):
response.base.__cause__ = Exception(response.trace)
future.set_exception(response.base)
elif isinstance(response, Exception):
future.set_exception(response)
else:
future.add_response(response)
self._lock.release()
if _block:
_send_thread(future)
else:
thread = threading.Thread(target=_send_thread, args=(future,), daemon=True)
thread.start()
return future
return _send
def __del__(self):
self.close()