class_docstr: ``PreemptContext`` gives access to preemption signals that originate from a user action, such as
  pausing an experiment using the WebUI or the CLI, from in the Determined scheduler.
class_docstr_tok: ["``","PreemptContext","``","gives","access","to","preemption","signals","that","originate","from","a","user","action",",","such","as","pausing","an","experiment","using","the","WebUI","or","the","CLI",",","from","in","the","Determined","scheduler","."]
docstr: ``should_preempt()`` returns ``True`` if the task should shut itself down, or ``False``
  otherwise.
  
  The requirements on the the caller and the synchronization between workers during a call
  to ``should_preempt()`` are defined by the ``preempt_mode`` argument passed to the
  ``PreemptContext`` constructor.
  
  Arguments:
      auto_ack (``bool``, optional): In order for the task to be restarted by the
          Determined master after shutting down due to preemption, the task must acknowledge
          the preemption signal to the Determined master.  When ``auto_ack`` is ``True`` this
          acknowledgement is automatically sent the first time that ``should_preempt()``
          returns ``True``.  If you might choose not to exit after receiving the preemption
          signal (but you still want to check the signal for some purpose), then you should
          set ``auto_ack`` to ``False``.  Then if you later do decide to comply with the
          preemption signal, it is your responsibility to call
          ``acknowledge_preemption_signal()`` manually any time before exiting.  Defaults to
          ``True``.
  
  .. note::
  
      Currently, only blocking behavior is supported when checking ``should_preempt()``, so it
      is not performant enough to call every batch.
docstr_tok: ["``","should_preempt","(",")","``","returns","``","True","``","if","the","task","should","shut","itself","down",",","or","``","False","``","otherwise",".","The","requirements","on","the","the","caller","and","the","synchronization","between","workers","during","a","call","to","``","should_preempt","(",")","``","are","defined","by","the","``","preempt_mode","``","argument","passed","to","the","``","PreemptContext","``","constructor",".","Arguments",":","auto_ack","(","``","bool","``",",","optional",")",":","In","order","for","the","task","to","be","restarted","by","the","Determined","master","after","shutting","down","due","to","preemption",",","the","task","must","acknowledge","the","preemption","signal","to","the","Determined","master",".","When","``","auto_ack","``","is","``","True","``","this","acknowledgement","is","automatically","sent","the","first","time","that","``","should_preempt","(",")","``","returns","``","True","``",".","If","you","might","choose","not","to","exit","after","receiving","the","preemption","signal","(","but","you","still","want","to","check","the","signal","for","some","purpose",")",",","then","you","should","set","``","auto_ack","``","to","``","False","``",".","Then","if","you","later","do","decide","to","comply","with","the","preemption","signal",",","it","is","your","responsibility","to","call","``","acknowledge_preemption_signal","(",")","``","manually","any","time","before","exiting",".","Defaults","to","``","True","``",".","..","note",":",":","Currently",",","only","blocking","behavior","is","supported","when","checking","``","should_preempt","(",")","``",",","so","it","is","not","performant","enough","to","call","every","batch","."]
code: def should_preempt(self, auto_ack: bool = True) -> bool:
      """
      ``should_preempt()`` returns ``True`` if the task should shut itself down, or ``False``
      otherwise.
  
      The requirements on the the caller and the synchronization between workers during a call
      to ``should_preempt()`` are defined by the ``preempt_mode`` argument passed to the
      ``PreemptContext`` constructor.
  
      Arguments:
          auto_ack (``bool``, optional): In order for the task to be restarted by the
              Determined master after shutting down due to preemption, the task must acknowledge
              the preemption signal to the Determined master.  When ``auto_ack`` is ``True`` this
              acknowledgement is automatically sent the first time that ``should_preempt()``
              returns ``True``.  If you might choose not to exit after receiving the preemption
              signal (but you still want to check the signal for some purpose), then you should
              set ``auto_ack`` to ``False``.  Then if you later do decide to comply with the
              preemption signal, it is your responsibility to call
              ``acknowledge_preemption_signal()`` manually any time before exiting.  Defaults to
              ``True``.
  
      .. note::
  
          Currently, only blocking behavior is supported when checking ``should_preempt()``, so it
          is not performant enough to call every batch.
      """
      if not self._started:
          # Calling should_preempt on the watcher without starting the watcher would hang.
          raise RuntimeError(
              "you cannot call PreemptContext.should_preempt() before PreemptContext.start()"
          )
      if self._watcher is not None:
          # Have watcher; either this is the chief or we are in WorkersAskMaster mode.
          out = self._watcher.should_preempt()
          if auto_ack and out and not self._ack_sent:
              # Tell the master that user code has received the preemption signal.
              self.acknowledge_preemption_signal()
              self._ack_sent = True
          if self._preempt_mode == PreemptMode.WorkersAskChief:
              _ = self._dist.broadcast(out)
      else:
          # No watcher; we should ask the chief or either we should not be here.
          if self._preempt_mode == PreemptMode.ChiefOnly:
              raise RuntimeError(
                  "PreemptContext was configured with preempt_mode=ChiefOnly but "
                  ".should_preempt() was called from non-chief worker of "
                  f"rank={self._dist.get_rank()}"
              )
          out = self._dist.broadcast(None)
          assert isinstance(out, bool)
  
      logger.debug(f"should_preempt() -> {out}")
      return out
  
imports: import enum
  import logging
  import threading
  import time
  from typing import Any, Optional
  import requests
  from determined import core
  from determined.common import api
label_desc: Use image node_id 6 for calling the PreemptContext obj's underlying member method code with example usage: obj.should_preempt(auto_ack) and returns: out