@tf_export('train.checkpoints_iterator')
def checkpoints_iterator(checkpoint_dir, min_interval_secs=0, timeout=None, timeout_fn=None):
    'Continuously yield new checkpoint files as they appear.\n\n  The iterator only checks for new checkpoints when control flow has been\n  reverted to it. This means it can miss checkpoints if your code takes longer\n  to run between iterations than `min_interval_secs` or the interval at which\n  new checkpoints are written.\n\n  The `timeout` argument is the maximum number of seconds to block waiting for\n  a new checkpoint.  It is used in combination with the `timeout_fn` as\n  follows:\n\n  * If the timeout expires and no `timeout_fn` was specified, the iterator\n    stops yielding.\n  * If a `timeout_fn` was specified, that function is called and if it returns\n    a true boolean value the iterator stops yielding.\n  * If the function returns a false boolean value then the iterator resumes the\n    wait for new checkpoints.  At this point the timeout logic applies again.\n\n  This behavior gives control to callers on what to do if checkpoints do not\n  come fast enough or stop being generated.  For example, if callers have a way\n  to detect that the training has stopped and know that no new checkpoints\n  will be generated, they can provide a `timeout_fn` that returns `True` when\n  the training has stopped.  If they know that the training is still going on\n  they return `False` instead.\n\n  Args:\n    checkpoint_dir: The directory in which checkpoints are saved.\n    min_interval_secs: The minimum number of seconds between yielding\n      checkpoints.\n    timeout: The maximum number of seconds to wait between checkpoints. If left\n      as `None`, then the process will wait indefinitely.\n    timeout_fn: Optional function to call after a timeout.  If the function\n      returns True, then it means that no new checkpoints will be generated and\n      the iterator will exit.  The function is called with no arguments.\n\n  Yields:\n    String paths to latest checkpoint files as they arrive.\n  '
    checkpoint_path = None
    while True:
        new_checkpoint_path = wait_for_new_checkpoint(checkpoint_dir, checkpoint_path, timeout=timeout)
        if (new_checkpoint_path is None):
            if (not timeout_fn):
                logging.info('Timed-out waiting for a checkpoint.')
                return
            if timeout_fn():
                return
            else:
                continue
        start = time.time()
        checkpoint_path = new_checkpoint_path
        (yield checkpoint_path)
        time_to_next_eval = ((start + min_interval_secs) - time.time())
        if (time_to_next_eval > 0):
            time.sleep(time_to_next_eval)
Continuously yield new checkpoint files as they appear.

The iterator only checks for new checkpoints when control flow has been
reverted to it. This means it can miss checkpoints if your code takes longer
to run between iterations than `min_interval_secs` or the interval at which
new checkpoints are written.

The `timeout` argument is the maximum number of seconds to block waiting for
a new checkpoint.  It is used in combination with the `timeout_fn` as
follows:

* If the timeout expires and no `timeout_fn` was specified, the iterator
  stops yielding.
* If a `timeout_fn` was specified, that function is called and if it returns
  a true boolean value the iterator stops yielding.
* If the function returns a false boolean value then the iterator resumes the
  wait for new checkpoints.  At this point the timeout logic applies again.

This behavior gives control to callers on what to do if checkpoints do not
come fast enough or stop being generated.  For example, if callers have a way
to detect that the training has stopped and know that no new checkpoints
will be generated, they can provide a `timeout_fn` that returns `True` when
the training has stopped.  If they know that the training is still going on
they return `False` instead.

Args:
  checkpoint_dir: The directory in which checkpoints are saved.
  min_interval_secs: The minimum number of seconds between yielding
    checkpoints.
  timeout: The maximum number of seconds to wait between checkpoints. If left
    as `None`, then the process will wait indefinitely.
  timeout_fn: Optional function to call after a timeout.  If the function
    returns True, then it means that no new checkpoints will be generated and
    the iterator will exit.  The function is called with no arguments.

Yields:
  String paths to latest checkpoint files as they arrive.
@tf_export('train.checkpoints_iterator')
def checkpoints_iterator(checkpoint_dir, min_interval_secs=0, timeout=None, timeout_fn=None):
    'Continuously yield new checkpoint files as they appear.\n\n  The iterator only checks for new checkpoints when control flow has been\n  reverted to it. This means it can miss checkpoints if your code takes longer\n  to run between iterations than `min_interval_secs` or the interval at which\n  new checkpoints are written.\n\n  The `timeout` argument is the maximum number of seconds to block waiting for\n  a new checkpoint.  It is used in combination with the `timeout_fn` as\n  follows:\n\n  * If the timeout expires and no `timeout_fn` was specified, the iterator\n    stops yielding.\n  * If a `timeout_fn` was specified, that function is called and if it returns\n    a true boolean value the iterator stops yielding.\n  * If the function returns a false boolean value then the iterator resumes the\n    wait for new checkpoints.  At this point the timeout logic applies again.\n\n  This behavior gives control to callers on what to do if checkpoints do not\n  come fast enough or stop being generated.  For example, if callers have a way\n  to detect that the training has stopped and know that no new checkpoints\n  will be generated, they can provide a `timeout_fn` that returns `True` when\n  the training has stopped.  If they know that the training is still going on\n  they return `False` instead.\n\n  Args:\n    checkpoint_dir: The directory in which checkpoints are saved.\n    min_interval_secs: The minimum number of seconds between yielding\n      checkpoints.\n    timeout: The maximum number of seconds to wait between checkpoints. If left\n      as `None`, then the process will wait indefinitely.\n    timeout_fn: Optional function to call after a timeout.  If the function\n      returns True, then it means that no new checkpoints will be generated and\n      the iterator will exit.  The function is called with no arguments.\n\n  Yields:\n    String paths to latest checkpoint files as they arrive.\n  '
    checkpoint_path = None
    while True:
        new_checkpoint_path = wait_for_new_checkpoint(checkpoint_dir, checkpoint_path, timeout=timeout)
        if (new_checkpoint_path is None):
            if (not timeout_fn):
                logging.info('Timed-out waiting for a checkpoint.')
                return
            if timeout_fn():
                return
            else:
                continue
        start = time.time()
        checkpoint_path = new_checkpoint_path
        (yield checkpoint_path)
        time_to_next_eval = ((start + min_interval_secs) - time.time())
        if (time_to_next_eval > 0):
            time.sleep(time_to_next_eval)