Skip to content

Commit 2a58abb

Browse files
committed
add control of error propagation in task runner
more tests check task types early
1 parent 4208e27 commit 2a58abb

File tree

13 files changed

+1245
-986
lines changed

13 files changed

+1245
-986
lines changed

coverage.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
============================= test session starts ==============================
2-
platform darwin -- Python 3.9.16, pytest-7.1.2, pluggy-1.0.0
2+
platform darwin -- Python 3.11.2, pytest-7.1.2, pluggy-1.0.0
33
rootdir: /Users/johnmount/Documents/work/wvpy/pkg
4-
plugins: anyio-3.5.0, cov-3.0.0
5-
collected 8 items
4+
plugins: anyio-3.5.0, cov-4.0.0
5+
collected 10 items
66

7-
tests/test_nb_fns.py ........ [100%]
7+
tests/test_nb_fns.py .......... [100%]
88

9-
---------- coverage: platform darwin, python 3.9.16-final-0 ----------
9+
---------- coverage: platform darwin, python 3.11.2-final-0 ----------
1010
Name Stmts Miss Cover
1111
---------------------------------------------
1212
wvpy/__init__.py 3 0 100%
13-
wvpy/jtools.py 262 80 69%
13+
wvpy/jtools.py 290 95 67%
1414
wvpy/pysheet.py 99 99 0%
1515
wvpy/render_workbook.py 54 54 0%
1616
---------------------------------------------
17-
TOTAL 418 233 44%
17+
TOTAL 446 248 44%
1818

1919

20-
============================== 8 passed in 21.18s ==============================
20+
============================= 10 passed in 30.12s ==============================

pkg/build/lib/wvpy/jtools.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import os
77
import nbformat
88
import nbconvert.preprocessors
9+
from multiprocessing import Pool
10+
import sys
911

10-
from typing import Optional
12+
from typing import Iterable, Optional
1113
from functools import total_ordering
1214

1315
have_pdf_kit = False
@@ -454,7 +456,7 @@ def __repr__(self) -> str:
454456
return self.__str__()
455457

456458

457-
def job_fn(arg: JTask):
459+
def job_fn(arg: JTask) -> None:
458460
"""
459461
Function to run a JTask job
460462
"""
@@ -463,7 +465,7 @@ def job_fn(arg: JTask):
463465
arg.render_as_html()
464466

465467

466-
def job_fn_eat_exception(arg: JTask):
468+
def job_fn_eat_exception(arg: JTask) -> None:
467469
"""
468470
Function to run a JTask job, eating any exception
469471
"""
@@ -473,3 +475,50 @@ def job_fn_eat_exception(arg: JTask):
473475
arg.render_as_html()
474476
except Exception as e:
475477
print(f"{arg} caught {e}")
478+
479+
480+
def run_pool(
481+
tasks: Iterable,
482+
*,
483+
njobs: int = 4,
484+
verbose: bool = True,
485+
stop_on_error: bool = True,
486+
) -> None:
487+
"""
488+
Run a pool of tasks.
489+
490+
:param tasks: iterable of tasks
491+
:param njobs: degree of parallelism
492+
:param verbose: if True, print on failure
493+
:param stop_on_error: if True, stop pool on error
494+
"""
495+
tasks = list(tasks)
496+
assert isinstance(njobs, int)
497+
assert njobs > 0
498+
assert isinstance(verbose, bool)
499+
assert isinstance(stop_on_error, bool)
500+
if len(tasks) <= 0:
501+
return
502+
for task in tasks:
503+
assert isinstance(task, JTask)
504+
if stop_on_error:
505+
# # complex way, allowing a stop on job failure
506+
# https://stackoverflow.com/a/25791961/6901725
507+
with Pool(njobs) as pool:
508+
try:
509+
list(pool.imap_unordered(job_fn, tasks)) # list is forcing iteration over tasks for side-effects
510+
except Exception:
511+
if verbose:
512+
sys.stdout.flush()
513+
print("!!! run_pool: a worker raised an Exception, aborting...")
514+
sys.stdout.flush()
515+
pool.close()
516+
pool.terminate()
517+
raise # re-raise Exception
518+
else:
519+
pool.close()
520+
pool.join()
521+
else:
522+
# simple way, but doesn't exit until all jobs succeed or fail
523+
with Pool(njobs) as pool:
524+
pool.map(job_fn_eat_exception, tasks)
472 Bytes
Binary file not shown.

pkg/dist/wvpy-0.4.0.tar.gz

459 Bytes
Binary file not shown.

pkg/docs/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/docs/wvpy.html

Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.

pkg/docs/wvpy/jtools.html

Lines changed: 1070 additions & 945 deletions
Large diffs are not rendered by default.

pkg/docs/wvpy/pysheet.html

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

pkg/docs/wvpy/render_workbook.html

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

pkg/tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example_good_notebook.html

0 commit comments

Comments
 (0)