class_docstr: Watch preemption signal and store it.
  
  Notice: Currently only support Borg TPU environment with TPUClusterResolver.
  
  This class provides a way to monitor the preemption signal during training on
  TPU. It will start a background thread to watch the training process, trying
  to fetch preemption message from the coordination service. When preemption
  happens, the preempted worker will write the preemption message to the
  coordination service. Thus getting a non-empty preemption message means there
  is a preemption happened.
  
  User can use the preemption message as a reliable preemption indicator, and
  then set the coordinator to reconnect to the TPU worker instead of a fully
  restart triggered by Borg. For example, a training process with
  preemption recovery will be like:
  
  ```python
  keep_running = True
  preemption_watcher = None
  while keep_running:
    try:
      # Initialize TPU cluster and stratygy.
      resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
      tf.config.experimental_connect_to_cluster(resolver)
      tf.tpu.experimental.initialize_tpu_system(resolver)
      strategy = tf.distribute.TPUStrategy(resolver)
  
      # PreemptionWatcher must be created after connected to cluster.
      preemption_watcher = tf.distribute.experimental.PreemptionWatcher()
      train_model(strategy)
      keep_running = False
    except Exception as e:
      if preemption_watcher and preemption_watcher.preemption_message:
        preemption_watcher.block_until_worker_exit()
        keep_running = True
      else:
        raise e
  ```
  
  Attributes:
    preemption_message: A variable to store the preemption message fetched from
      the coordination service. If it is not None, then there is a preemption
      happened.
    platform: A PlatformDevice to indicate the current job's platform. Refer to
      failure_handling_util.py for the definition of enum class PlatformDevice.
code: def __init__(self):
      # TODO(b/254321514): Integrate with GPU and cloud enviornmenmt.
      self._preemption_message = None
      self._platform = detect_platform()
      if self._platform != PlatformDevice.INTERNAL_TPU:
          logging.warning(
              "Preemption watcher does not support environment: %s",
              self._platform,
          )
      else:
          _preemption_watcher_initialization_counter.get_cell().increase_by(
              1
          )
          threading.Thread(
              target=self._watch_preemption_key, daemon=True
          ).start()
  
file_path: tensorflow/tensorflow/python/distribute/failure_handling/preemption_watcher.py
imports: import threading
  from absl import logging
  from tensorflow.python.distribute.failure_handling.failure_handling_util import detect_platform
  from tensorflow.python.distribute.failure_handling.failure_handling_util import PlatformDevice
  from tensorflow.python.eager import context
  from tensorflow.python.eager import monitoring
  from tensorflow.python.framework.errors import AbortedError
  from tensorflow.python.framework.errors import CancelledError
  from tensorflow.python.framework.errors import InternalError
  from tensorflow.python.framework.errors import UnavailableError
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 1 to create a new PreemptionWatcher object  with example: obj = PreemptionWatcher()