Context manager for callbacks. Takes several callbacks and applies them only in the enclosed context. Callbacks can either be represented as a ``Callback`` object, or as a tuple of length 4. Examples -------- >>> def pretask(key, dsk, state): ... print("Now running
| 118 | |
| 119 | |
| 120 | class add_callbacks: |
| 121 | """Context manager for callbacks. |
| 122 | |
| 123 | Takes several callbacks and applies them only in the enclosed context. |
| 124 | Callbacks can either be represented as a ``Callback`` object, or as a tuple |
| 125 | of length 4. |
| 126 | |
| 127 | Examples |
| 128 | -------- |
| 129 | >>> def pretask(key, dsk, state): |
| 130 | ... print("Now running {0}").format(key) |
| 131 | >>> callbacks = (None, pretask, None, None) |
| 132 | >>> with add_callbacks(callbacks): # doctest: +SKIP |
| 133 | ... res.compute() |
| 134 | """ |
| 135 | |
| 136 | def __init__(self, *callbacks): |
| 137 | self.callbacks = [normalize_callback(c) for c in callbacks] |
| 138 | Callback.active.update(self.callbacks) |
| 139 | |
| 140 | def __enter__(self): |
| 141 | return |
| 142 | |
| 143 | def __exit__(self, type, value, traceback): |
| 144 | for c in self.callbacks: |
| 145 | Callback.active.discard(c) |