Request that the threads stop. After this is called, calls to `should_stop()` will return `True`. Note: If an exception is being passed in, in must be in the context of handling the exception (i.e. `try: ... except Exception as ex: ...`) and not a newly created one. Args:
(self, ex=None)
| 185 | return ex |
| 186 | |
| 187 | def request_stop(self, ex=None): |
| 188 | """Request that the threads stop. |
| 189 | |
| 190 | After this is called, calls to `should_stop()` will return `True`. |
| 191 | |
| 192 | Note: If an exception is being passed in, in must be in the context of |
| 193 | handling the exception (i.e. `try: ... except Exception as ex: ...`) and not |
| 194 | a newly created one. |
| 195 | |
| 196 | Args: |
| 197 | ex: Optional `Exception`, or Python `exc_info` tuple as returned by |
| 198 | `sys.exc_info()`. If this is the first call to `request_stop()` the |
| 199 | corresponding exception is recorded and re-raised from `join()`. |
| 200 | """ |
| 201 | with self._lock: |
| 202 | ex = self._filter_exception(ex) |
| 203 | # If we have already joined the coordinator the exception will not have a |
| 204 | # chance to be reported, so just raise it normally. This can happen if |
| 205 | # you continue to use a session have having stopped and joined the |
| 206 | # coordinator threads. |
| 207 | if self._joined: |
| 208 | if isinstance(ex, tuple): |
| 209 | six.reraise(*ex) |
| 210 | elif ex is not None: |
| 211 | # NOTE(touts): This is bogus if request_stop() is not called |
| 212 | # from the exception handler that raised ex. |
| 213 | six.reraise(*sys.exc_info()) |
| 214 | if not self._stop_event.is_set(): |
| 215 | if ex and self._exc_info_to_raise is None: |
| 216 | if isinstance(ex, tuple): |
| 217 | logging.info("Error reported to Coordinator: %s", |
| 218 | compat.as_str_any(ex[1]), |
| 219 | exc_info=ex) |
| 220 | self._exc_info_to_raise = ex |
| 221 | else: |
| 222 | logging.info("Error reported to Coordinator: %s, %s", |
| 223 | type(ex), |
| 224 | compat.as_str_any(ex)) |
| 225 | self._exc_info_to_raise = sys.exc_info() |
| 226 | # self._exc_info_to_raise should contain a tuple containing exception |
| 227 | # (type, value, traceback) |
| 228 | if (len(self._exc_info_to_raise) != 3 or |
| 229 | not self._exc_info_to_raise[0] or |
| 230 | not self._exc_info_to_raise[1]): |
| 231 | # Raise, catch and record the exception here so that error happens |
| 232 | # where expected. |
| 233 | try: |
| 234 | raise ValueError( |
| 235 | "ex must be a tuple or sys.exc_info must return the current " |
| 236 | "exception: %s" |
| 237 | % self._exc_info_to_raise) |
| 238 | except ValueError: |
| 239 | # Record this error so it kills the coordinator properly. |
| 240 | # NOTE(touts): As above, this is bogus if request_stop() is not |
| 241 | # called from the exception handler that raised ex. |
| 242 | self._exc_info_to_raise = sys.exc_info() |
| 243 | |
| 244 | self._stop_event.set() |