Context manager to request stop when an Exception is raised. Code that uses a coordinator must catch exceptions and pass them to the `request_stop()` method to stop the other threads managed by the coordinator. This context handler simplifies the exception handling. Use it as f
(self)
| 264 | |
| 265 | @contextlib.contextmanager |
| 266 | def stop_on_exception(self): |
| 267 | """Context manager to request stop when an Exception is raised. |
| 268 | |
| 269 | Code that uses a coordinator must catch exceptions and pass |
| 270 | them to the `request_stop()` method to stop the other threads |
| 271 | managed by the coordinator. |
| 272 | |
| 273 | This context handler simplifies the exception handling. |
| 274 | Use it as follows: |
| 275 | |
| 276 | ```python |
| 277 | with coord.stop_on_exception(): |
| 278 | # Any exception raised in the body of the with |
| 279 | # clause is reported to the coordinator before terminating |
| 280 | # the execution of the body. |
| 281 | ...body... |
| 282 | ``` |
| 283 | |
| 284 | This is completely equivalent to the slightly longer code: |
| 285 | |
| 286 | ```python |
| 287 | try: |
| 288 | ...body... |
| 289 | except: |
| 290 | coord.request_stop(sys.exc_info()) |
| 291 | ``` |
| 292 | |
| 293 | Yields: |
| 294 | nothing. |
| 295 | """ |
| 296 | try: |
| 297 | yield |
| 298 | except: # pylint: disable=bare-except |
| 299 | self.request_stop(ex=sys.exc_info()) |
| 300 | |
| 301 | def wait_for_stop(self, timeout=None): |
| 302 | """Wait till the Coordinator is told to stop. |