Base class for using the callback mechanism Create a callback with functions of the following signatures: >>> def start(dsk): ... pass >>> def start_state(dsk, state): ... pass >>> def pretask(key, dsk, state): ... pass >>> def posttask(key, result, dsk,
| 8 | |
| 9 | |
| 10 | class Callback: |
| 11 | """Base class for using the callback mechanism |
| 12 | |
| 13 | Create a callback with functions of the following signatures: |
| 14 | |
| 15 | >>> def start(dsk): |
| 16 | ... pass |
| 17 | >>> def start_state(dsk, state): |
| 18 | ... pass |
| 19 | >>> def pretask(key, dsk, state): |
| 20 | ... pass |
| 21 | >>> def posttask(key, result, dsk, state, worker_id): |
| 22 | ... pass |
| 23 | >>> def finish(dsk, state, failed): |
| 24 | ... pass |
| 25 | |
| 26 | You may then construct a callback object with any number of them |
| 27 | |
| 28 | >>> cb = Callback(pretask=pretask, finish=finish) |
| 29 | |
| 30 | And use it either as a context manager over a compute/get call |
| 31 | |
| 32 | >>> with cb: # doctest: +SKIP |
| 33 | ... x.compute() |
| 34 | |
| 35 | Or globally with the ``register`` method |
| 36 | |
| 37 | >>> cb.register() |
| 38 | >>> cb.unregister() |
| 39 | |
| 40 | Alternatively subclass the ``Callback`` class with your own methods. |
| 41 | |
| 42 | >>> class PrintKeys(Callback): |
| 43 | ... def _pretask(self, key, dask, state): |
| 44 | ... print("Computing: {0}!".format(repr(key))) |
| 45 | |
| 46 | >>> with PrintKeys(): # doctest: +SKIP |
| 47 | ... x.compute() |
| 48 | """ |
| 49 | |
| 50 | active: ClassVar[set[tuple[Callable | None, ...]]] = set() |
| 51 | |
| 52 | def __init__( |
| 53 | self, start=None, start_state=None, pretask=None, posttask=None, finish=None |
| 54 | ): |
| 55 | if start: |
| 56 | self._start = start |
| 57 | if start_state: |
| 58 | self._start_state = start_state |
| 59 | if pretask: |
| 60 | self._pretask = pretask |
| 61 | if posttask: |
| 62 | self._posttask = posttask |
| 63 | if finish: |
| 64 | self._finish = finish |
| 65 | |
| 66 | @property |
| 67 | def _callback(self) -> tuple[Callable | None, ...]: |
searching dependent graphs…