Spaces:
Running
on
Zero
Running
on
Zero
File size: 12,502 Bytes
d1ed09d |
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
from __future__ import annotations
import asyncio
import inspect
import logging
import multiprocessing
import os
import re
import threading
import weakref
from collections.abc import Callable
from queue import Queue as PyQueue
from typing import TYPE_CHECKING
from tornado.concurrent import Future
from tornado.ioloop import IOLoop
import dask
from distributed.utils import get_mp_context
if TYPE_CHECKING:
# TODO import from typing (requires Python >=3.11)
from typing_extensions import Self
logger = logging.getLogger(__name__)
def _loop_add_callback(loop, func, *args):
"""
Helper to silence "IOLoop is closing" exception on IOLoop.add_callback.
"""
try:
loop.add_callback(func, *args)
except RuntimeError as exc:
if not re.search("IOLoop is clos(ed|ing)", str(exc)):
raise
def _call_and_set_future(loop, future, func, *args, **kwargs):
try:
res = func(*args, **kwargs)
except Exception as exc:
# Tornado futures are not thread-safe, need to
# set_result() / set_exc_info() from the loop's thread
_loop_add_callback(loop, future.set_exception, exc)
else:
_loop_add_callback(loop, future.set_result, res)
class _ProcessState:
is_alive = False
pid = None
exitcode = None
class AsyncProcess:
"""
A coroutine-compatible multiprocessing.Process-alike.
All normally blocking methods are wrapped in Tornado coroutines.
"""
_process: multiprocessing.Process
def __init__(self, loop=None, target=None, name=None, args=(), kwargs=None):
kwargs = kwargs or {}
if not callable(target):
raise TypeError(f"`target` needs to be callable, not {type(target)!r}")
self._state = _ProcessState()
self._loop = loop or IOLoop.current(instance=False)
# _keep_child_alive is the write side of a pipe, which, when it is
# closed, causes the read side of the pipe to unblock for reading. Note
# that it is never closed directly. The write side is closed by the
# kernel when our process exits, or possibly by the garbage collector
# closing the file descriptor when the last reference to
# _keep_child_alive goes away. We can take advantage of this fact to
# monitor from the child and exit when the parent goes away unexpectedly
# (for example due to SIGKILL). This variable is otherwise unused except
# for the assignment here.
parent_alive_pipe, self._keep_child_alive = get_mp_context().Pipe(duplex=False)
self._process = get_mp_context().Process(
target=self._run,
name=name,
args=(
target,
args,
kwargs,
parent_alive_pipe,
self._keep_child_alive,
dask.config.global_config,
),
)
self._name = self._process.name
self._proc_finalizer = weakref.finalize(
self, _asyncprocess_finalizer, self._process
)
self._watch_q = PyQueue()
self._exit_future = Future()
self._exit_callback = None
self._closed = False
self._start_threads()
def __repr__(self):
return f"<{self.__class__.__name__} {self._name}>"
def _check_closed(self):
if self._closed:
raise ValueError("invalid operation on closed AsyncProcess")
def _start_threads(self):
self._watch_message_thread = threading.Thread(
target=self._watch_message_queue,
name="AsyncProcess %s watch message queue" % self.name,
args=(
weakref.ref(self),
self._process,
self._loop,
self._state,
self._watch_q,
self._exit_future,
),
)
self._watch_message_thread.daemon = True
self._watch_message_thread.start()
def stop_thread(q):
q.put_nowait({"op": "stop"})
# We don't join the thread here as a finalizer can be called
# asynchronously from anywhere
self._thread_finalizer = weakref.finalize(self, stop_thread, q=self._watch_q)
self._thread_finalizer.atexit = False
def _on_exit(self, exitcode: int) -> None:
# Called from the event loop when the child process exited
self._process = None # type: ignore[assignment]
if self._exit_callback is not None:
self._exit_callback(self)
self._exit_future.set_result(exitcode)
@classmethod
def _immediate_exit_when_closed(cls, parent_alive_pipe):
"""
Immediately exit the process when parent_alive_pipe is closed.
"""
def monitor_parent():
try:
# The parent_alive_pipe should be held open as long as the
# parent is alive and wants us to stay alive. Nothing writes to
# it, so the read will block indefinitely.
parent_alive_pipe.recv()
except EOFError:
# Parent process went away unexpectedly. Exit immediately. Could
# consider other exiting approaches here. My initial preference
# is to unconditionally and immediately exit. If we're in this
# state it is possible that a "clean" process exit won't work
# anyway - if, for example, the system is getting bogged down
# due to the running out of memory, exiting sooner rather than
# later might be needed to restore normal system function.
# If this is in appropriate for your use case, please file a
# bug.
os._exit(-1)
else:
# If we get here, something odd is going on. File descriptors
# got crossed?
raise RuntimeError("unexpected state: should be unreachable")
t = threading.Thread(target=monitor_parent)
t.daemon = True
t.start()
@classmethod
def _run(
cls, target, args, kwargs, parent_alive_pipe, _keep_child_alive, inherit_config
):
_keep_child_alive.close()
# Child process entry point
cls._immediate_exit_when_closed(parent_alive_pipe)
threading.current_thread().name = "MainThread"
# Update the global config given priority to the existing global config
dask.config.update(dask.config.global_config, inherit_config, priority="old")
target(*args, **kwargs)
@classmethod
def _watch_message_queue( # type: ignore[no-untyped-def]
cls, selfref, process: multiprocessing.Process, loop, state, q, exit_future
):
# As multiprocessing.Process is not thread-safe, we run all
# blocking operations from this single loop and ship results
# back to the caller when needed.
r = repr(selfref())
name = selfref().name
def _start():
process.start()
thread = threading.Thread(
target=AsyncProcess._watch_process,
name="AsyncProcess %s watch process join" % name,
args=(selfref, process, state, q),
)
thread.daemon = True
thread.start()
state.is_alive = True
state.pid = process.pid
logger.debug(f"[{r}] created process with pid {state.pid!r}")
while True:
msg = q.get()
logger.debug(f"[{r}] got message {msg!r}")
op = msg["op"]
if op == "start":
_call_and_set_future(loop, msg["future"], _start)
elif op == "terminate":
# Send SIGTERM
_call_and_set_future(loop, msg["future"], process.terminate)
elif op == "kill":
# Send SIGKILL
_call_and_set_future(loop, msg["future"], process.kill)
elif op == "stop":
break
else:
assert 0, msg
@classmethod
def _watch_process(cls, selfref, process, state, q):
r = repr(selfref())
process.join()
exitcode = original_exit_code = process.exitcode
if exitcode is None:
# The child process is already reaped
# (may happen if waitpid() is called elsewhere).
exitcode = 255
state.is_alive = False
state.exitcode = exitcode
# Make sure the process is removed from the global list
# (see _children in multiprocessing/process.py)
# Then notify the Process object
self = selfref() # only keep self alive when required
try:
if self is not None:
_loop_add_callback(self._loop, self._on_exit, exitcode)
finally:
self = None # lose reference
# logging may fail - defer calls to after the callback is added
if original_exit_code is None:
logger.warning(
"[%s] process %r exit status was already read will report exitcode 255",
r,
state.pid,
)
else:
logger.debug("[%s] process %r exited with code %r", r, state.pid, exitcode)
def start(self):
"""
Start the child process.
This method returns a future.
"""
self._check_closed()
fut = Future()
self._watch_q.put_nowait({"op": "start", "future": fut})
return fut
def terminate(self) -> asyncio.Future[None]:
"""Terminate the child process.
This method returns a future.
See also
--------
multiprocessing.Process.terminate
"""
self._check_closed()
fut: Future[None] = Future()
self._watch_q.put_nowait({"op": "terminate", "future": fut})
return fut
def kill(self) -> asyncio.Future[None]:
"""Send SIGKILL to the child process.
On Windows, this is the same as terminate().
This method returns a future.
See also
--------
multiprocessing.Process.kill
"""
self._check_closed()
fut: Future[None] = Future()
self._watch_q.put_nowait({"op": "kill", "future": fut})
return fut
async def join(self, timeout=None):
"""
Wait for the child process to exit.
This method returns a coroutine.
"""
self._check_closed()
assert self._state.pid is not None, "can only join a started process"
if self._state.exitcode is not None:
return
# Shield otherwise the timeout cancels the future and our
# on_exit callback will try to set a result on a canceled future
await asyncio.wait_for(asyncio.shield(self._exit_future), timeout)
def close(self):
"""
Stop helper thread and release resources. This method returns
immediately and does not ensure the child process has exited.
"""
if not self._closed:
self._thread_finalizer()
self._process = None
self._closed = True
def set_exit_callback(self: Self, func: Callable[[Self], None]) -> None:
"""
Set a function to be called by the event loop when the process exits.
The function is called with the AsyncProcess as sole argument.
The function may not be a coroutine function.
"""
# XXX should this be a property instead?
assert not inspect.iscoroutinefunction(
func
), "exit callback may not be a coroutine function"
assert callable(func), "exit callback should be callable"
assert (
self._state.pid is None
), "cannot set exit callback when process already started"
self._exit_callback = func
def is_alive(self):
return self._state.is_alive
@property
def pid(self):
return self._state.pid
@property
def exitcode(self):
return self._state.exitcode
@property
def name(self):
return self._name
@property
def daemon(self):
return self._process.daemon
@daemon.setter
def daemon(self, value):
self._process.daemon = value
def _asyncprocess_finalizer(proc):
if proc.is_alive():
try:
logger.info(f"reaping stray process {proc}")
proc.terminate()
except OSError:
pass
|