forked from sagemath/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
382 lines (314 loc) · 13 KB
/
utils.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
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
379
380
381
382
r"""
Utilities
"""
import errno
import os
import traceback
from typing import Optional
class RemoteException(Exception):
"""
Raised if an exception occurred in one of the child processes.
"""
tb: str
def __init__(self, tb: str):
"""
Initialize the exception.
INPUT:
- ``tb`` -- the traceback of the exception.
"""
self.tb = tb
def __str__(self):
"""
Return a string representation of the exception.
"""
return self.tb
class RemoteExceptionWrapper:
"""
Used by child processes to capture exceptions thrown during execution and
report them to the main process, including the correct traceback.
"""
exc: BaseException
tb: str
def __init__(self, exc: BaseException):
"""
Initialize the exception wrapper.
INPUT:
- ``exc`` -- the exception to wrap.
"""
self.exc = exc
self.tb = traceback.format_exc()
# We cannot pickle the traceback, thus convert it to a string.
# Later on unpickling, we set the original tracback as the cause of the exception
# This approach is taken from https://bugs.python.org/issue13831
tb = traceback.format_exception(type(exc), exc, exc.__traceback__)
tb = "".join(tb)
self.exc = exc
self.tb = f'\n"""\n{tb}"""'
@staticmethod
def _rebuild_exc(exc: BaseException, tb: str):
"""
Reconstructs the exception, putting the original exception as cause.
"""
exc.__cause__ = RemoteException(tb)
return exc
def __reduce__(self):
"""
TESTS::
sage: import pickle
sage: from sage_docbuild.utils import RemoteExceptionWrapper
sage: pickle.dumps(RemoteExceptionWrapper(ZeroDivisionError()), 0).decode()
...RemoteExceptionWrapper...ZeroDivisionError...
"""
return RemoteExceptionWrapper._rebuild_exc, (self.exc, self.tb)
class WorkerDiedException(RuntimeError):
"""Raised if a worker process dies unexpected."""
original_exception: Optional[BaseException]
def __init__(
self, message: Optional[str], original_exception: Optional[BaseException] = None
):
super().__init__(message)
self.original_exception = original_exception
def build_many(target, args, processes=None):
"""
Map a list of arguments in ``args`` to a single-argument target function
``target`` in parallel using ``multiprocessing.cpu_count()`` (or
``processes`` if given) simultaneous processes.
This is a simplified version of ``multiprocessing.Pool.map`` from the
Python standard library which avoids a couple of its pitfalls. In
particular, it can abort (with a ``RuntimeError``) without hanging if one of
the worker processes unexpectedly dies. It also has semantics equivalent
to ``maxtasksperchild=1``; that is, one process is started per argument.
As such, this is inefficient for processing large numbers of fast tasks,
but appropriate for running longer tasks (such as doc builds) which may
also require significant cleanup.
It also avoids starting new processes from a pthread, which results in at
least two known issues:
* On versions of Cygwin prior to 3.0.0 there were bugs in mmap handling
on threads (see :trac:`27214#comment:25`).
* When PARI is built with multi-threading support, forking a Sage
process from a thread leaves the main Pari interface instance broken
(see :trac:`26608#comment:38`).
In the future this may be replaced by a generalized version of the more
robust parallel processing implementation from ``sage.doctest.forker``.
EXAMPLES::
sage: from sage_docbuild.utils import build_many
sage: def target(N):
....: import time
....: time.sleep(float(0.1))
....: print('Processed task %s' % N)
sage: _ = build_many(target, range(8), processes=8)
Processed task ...
Processed task ...
Processed task ...
Processed task ...
Processed task ...
Processed task ...
Processed task ...
Processed task ...
Unlike the first version of ``build_many`` which was only intended to get
around the Cygwin bug, this version can also return a result, and thus can
be used as a replacement for ``multiprocessing.Pool.map`` (i.e. it still
blocks until the result is ready)::
sage: def square(N):
....: return N * N
sage: build_many(square, range(100))
[0, 1, 4, 9, ..., 9604, 9801]
If the target function raises an exception in any of the workers,
``build_many`` raises that exception and all other results are discarded.
Any in-progress tasks may still be allowed to complete gracefully before
the exception is raised::
sage: def target(N):
....: import time, os, signal
....: if N == 4:
....: # Task 4 is a poison pill
....: 1 / 0
....: else:
....: time.sleep(float(0.5))
....: print('Processed task %s' % N)
Note: In practice this test might still show output from the other worker
processes before the poison-pill is executed. It may also display the
traceback from the failing process on stderr. However, due to how the
doctest runner works, the doctest will only expect the final exception::
sage: build_many(target, range(8), processes=8)
Traceback (most recent call last):
...
raise ZeroDivisionError("rational division by zero")
ZeroDivisionError: rational division by zero
...
raise worker_exc.original_exception
ZeroDivisionError: rational division by zero
Similarly, if one of the worker processes dies unexpectedly otherwise exits
non-zero (e.g. killed by a signal) any in-progress tasks will be completed
gracefully, but then a ``RuntimeError`` is raised and pending tasks are not
started::
sage: def target(N):
....: import time, os, signal
....: if N == 4:
....: # Task 4 is a poison pill
....: os.kill(os.getpid(), signal.SIGKILL)
....: else:
....: time.sleep(float(0.5))
....: print('Processed task %s' % N)
sage: build_many(target, range(8), processes=8)
Traceback (most recent call last):
...
WorkerDiedException: worker for 4 died with non-zero exit code -9
"""
from multiprocessing import Process, Queue, cpu_count, set_start_method
# With OS X, Python 3.8 defaults to use 'spawn' instead of 'fork'
# in multiprocessing, and Sage docbuilding doesn't work with
# 'spawn'. See trac #27754.
if os.uname().sysname == "Darwin":
set_start_method("fork", force=True)
from queue import Empty
if processes is None:
processes = cpu_count()
workers = [None] * processes
tasks = enumerate(args)
results = []
result_queue = Queue()
# Utility functions #
def run_worker(target, queue, idx, task):
try:
result = target(task)
except BaseException as exc:
queue.put((None, RemoteExceptionWrapper(exc)))
else:
queue.put((idx, result))
def bring_out_yer_dead(w, task, exitcode):
"""
Handle a dead / completed worker. Raises WorkerDiedException if it
returned with a non-zero exit code.
"""
if w is None or exitcode is None:
# I'm not dead yet! (or I haven't even been born yet)
return (w, task)
# Hack: If we wait()ed on this worker manually we have to tell it
# it's dead:
if w._popen.returncode is None:
w._popen.returncode = exitcode
if exitcode != 0:
raise WorkerDiedException(
f"worker for {task[1]} died with non-zero exit code {w.exitcode}"
)
# Get result from the queue; depending on ordering this may not be
# *the* result for this worker, but for each completed worker there
# should be *a* result so let's get it
try:
result = result_queue.get_nowait()
if result[0] is None:
# Indicates that an exception occurred in the target function
exception = result[1]
raise WorkerDiedException("", original_exception=exception)
else:
results.append(result)
except Empty:
# Generally shouldn't happen but could in case of a race condition;
# don't worry we'll collect any remaining results at the end.
pass
# Helps multiprocessing with some internal bookkeeping
w.join()
return None
def wait_for_one():
"""Wait for a single process and return its pid and exit code."""
try:
pid, sts = os.wait()
except OSError as exc:
# No more processes to wait on if ECHILD
if exc.errno != errno.ECHILD:
raise
else:
return None, None
if os.WIFSIGNALED(sts):
exitcode = -os.WTERMSIG(sts)
else:
exitcode = os.WEXITSTATUS(sts)
return pid, exitcode
def reap_workers(waited_pid=None, waited_exitcode=None):
"""
This is the main worker handling loop.
Checks if workers have completed their tasks and spawns new workers if
there are more tasks on the queue. Returns `False` if there is more
work to be done or `True` if the work is complete.
Raises a ``WorkerDiedException`` if a worker exits unexpectedly.
"""
all_done = True
for idx, w in enumerate(workers):
if w is not None:
w, task = w
if w.pid == waited_pid:
exitcode = waited_exitcode
else:
exitcode = w.exitcode
w = bring_out_yer_dead(w, task, exitcode)
# Worker w is dead/not started, so start a new worker
# in its place with the next document from the queue
if w is None:
try:
task = next(tasks)
except StopIteration:
pass
else:
w = Process(target=run_worker, args=((target, result_queue) + task))
w.start()
# Pair the new worker with the task it's performing (mostly
# for debugging purposes)
w = (w, task)
workers[idx] = w
if w is not None:
all_done = False
# If all workers are dead and there are no more items to
# process in the queue then we are done
return all_done
# Main loop #
waited_pid = None
# Set along with waited_exitcode by calls to wait_for_one()
waited_exitcode = None
worker_exc = None # Set to a WorkerDiedException if one occurs
try:
while True:
# Check the status of each worker and break out of the loop if
# all work is done.
# We'll check each worker process against the returned
# pid back at the top of the `while True` loop. We also
# check any other processes that may have exited in the
# meantime
try:
if reap_workers(waited_pid, waited_exitcode):
break
except WorkerDiedException as exc:
worker_exc = exc
break
waited_pid, waited_exitcode = wait_for_one()
finally:
try:
remaining_workers = [w for w in workers if w is not None]
for w, _ in remaining_workers:
# Give any remaining workers a chance to shut down gracefully
try:
w.terminate()
except OSError as exc:
if exc.errno != errno.ESRCH:
# Otherwise it was already dead so this was expected
raise
for w, _ in remaining_workers:
w.join()
finally:
if worker_exc is not None:
# Re-raise the RuntimeError from bring_out_yer_dead set if a
# worker died unexpectedly, or the original exception if it's
# wrapping one
if worker_exc.original_exception is not None:
raise worker_exc.original_exception
else:
raise worker_exc
# All workers should be shut down by now and should have completed without
# error. No new items will be added to the result queue, so we can get all
# the remaining results, if any.
while True:
try:
results.append(result_queue.get_nowait())
except Empty:
break
# Return the results sorted according to their original task order
return [r[1] for r in sorted(results, key=lambda r: r[0])]