Type Coordinator
Namespace tensorflow.train
Parent PythonObjectContainer
Interfaces ICoordinator
A coordinator for threads. This class implements a simple mechanism to coordinate the termination of a
set of threads. #### Usage:
Any of the threads can call `coord.request_stop()` to ask for all the threads
to stop. To cooperate with the requests, each thread must check for
`coord.should_stop()` on a regular basis. `coord.should_stop()` returns
`True` as soon as `coord.request_stop()` has been called. A typical thread running with a coordinator will do something like:
#### Exception handling: A thread can report an exception to the coordinator as part of the
`request_stop()` call. The exception will be re-raised from the
`coord.join()` call. Thread code:
Main code:
To simplify the thread implementation, the Coordinator provides a
context handler `stop_on_exception()` that automatically requests a stop if
an exception is raised. Using the context handler the thread code above
can be written as:
#### Grace period for stopping: After a thread has called `coord.request_stop()` the other threads have a
fixed time to stop, this is called the 'stop grace period' and defaults to 2
minutes. If any of the threads is still alive after the grace period expires
`coord.join()` raises a RuntimeError reporting the laggards.
Show Example
# Create a coordinator. coord = Coordinator() # Start a number of threads, passing the coordinator to each of them. ...start thread 1...(coord,...) ...start thread N...(coord,...) # Wait for all the threads to terminate. coord.join(threads)
Methods
- clear_stop_dyn
- join
- join
- join_dyn
- raise_requested_exception
- raise_requested_exception_dyn
- register_thread
- register_thread
- register_thread_dyn
- request_stop
- request_stop_dyn
- should_stop_dyn
- stop_on_exception_dyn
- wait_for_stop
- wait_for_stop_dyn
Properties
Public instance methods
object clear_stop_dyn()
Clears the stop flag. After this is called, calls to `should_stop()` will return `False`.
void join(IEnumerable<PythonClassContainer> threads, double stop_grace_period_secs, bool ignore_live_threads)
Wait for threads to terminate. This call blocks until a set of threads have terminated. The set of thread
is the union of the threads passed in the `threads` argument and the list
of threads that registered with the coordinator by calling
`Coordinator.register_thread()`. After the threads stop, if an `exc_info` was passed to `request_stop`, that
exception is re-raised. Grace period handling: When `request_stop()` is called, threads are given
'stop_grace_period_secs' seconds to terminate. If any of them is still
alive after that period expires, a `RuntimeError` is raised. Note that if
an `exc_info` was passed to `request_stop()` then it is raised instead of
that `RuntimeError`.
Parameters
-
IEnumerable<PythonClassContainer>
threads - List of `threading.Threads`. The started threads to join in addition to the registered threads.
-
double
stop_grace_period_secs - Number of seconds given to threads to stop after `request_stop()` has been called.
-
bool
ignore_live_threads - If `False`, raises an error if any of the threads are still alive after `stop_grace_period_secs`.
void join(IEnumerable<PythonClassContainer> threads, int stop_grace_period_secs, bool ignore_live_threads)
Wait for threads to terminate. This call blocks until a set of threads have terminated. The set of thread
is the union of the threads passed in the `threads` argument and the list
of threads that registered with the coordinator by calling
`Coordinator.register_thread()`. After the threads stop, if an `exc_info` was passed to `request_stop`, that
exception is re-raised. Grace period handling: When `request_stop()` is called, threads are given
'stop_grace_period_secs' seconds to terminate. If any of them is still
alive after that period expires, a `RuntimeError` is raised. Note that if
an `exc_info` was passed to `request_stop()` then it is raised instead of
that `RuntimeError`.
Parameters
-
IEnumerable<PythonClassContainer>
threads - List of `threading.Threads`. The started threads to join in addition to the registered threads.
-
int
stop_grace_period_secs - Number of seconds given to threads to stop after `request_stop()` has been called.
-
bool
ignore_live_threads - If `False`, raises an error if any of the threads are still alive after `stop_grace_period_secs`.
object join_dyn(object threads, ImplicitContainer<T> stop_grace_period_secs, ImplicitContainer<T> ignore_live_threads)
Wait for threads to terminate. This call blocks until a set of threads have terminated. The set of thread
is the union of the threads passed in the `threads` argument and the list
of threads that registered with the coordinator by calling
`Coordinator.register_thread()`. After the threads stop, if an `exc_info` was passed to `request_stop`, that
exception is re-raised. Grace period handling: When `request_stop()` is called, threads are given
'stop_grace_period_secs' seconds to terminate. If any of them is still
alive after that period expires, a `RuntimeError` is raised. Note that if
an `exc_info` was passed to `request_stop()` then it is raised instead of
that `RuntimeError`.
Parameters
-
object
threads - List of `threading.Threads`. The started threads to join in addition to the registered threads.
-
ImplicitContainer<T>
stop_grace_period_secs - Number of seconds given to threads to stop after `request_stop()` has been called.
-
ImplicitContainer<T>
ignore_live_threads - If `False`, raises an error if any of the threads are still alive after `stop_grace_period_secs`.
void raise_requested_exception()
If an exception has been passed to `request_stop`, this raises it.
object raise_requested_exception_dyn()
If an exception has been passed to `request_stop`, this raises it.
void register_thread(LooperThread thread)
Register a thread to join.
Parameters
-
LooperThread
thread - A Python thread to join.
void register_thread(PythonClassContainer thread)
Register a thread to join.
Parameters
-
PythonClassContainer
thread - A Python thread to join.
object register_thread_dyn(object thread)
Register a thread to join.
Parameters
-
object
thread - A Python thread to join.
void request_stop(Exception ex)
Request that the coordinator stop the threads. See `Coordinator.request_stop()`.
Parameters
-
Exception
ex - Optional `Exception`, or Python `exc_info` tuple as returned by `sys.exc_info()`. If this is the first call to `request_stop()` the corresponding exception is recorded and re-raised from `join()`.
object request_stop_dyn(object ex)
Request that the coordinator stop the threads. See `Coordinator.request_stop()`.
Parameters
-
object
ex - Optional `Exception`, or Python `exc_info` tuple as returned by `sys.exc_info()`. If this is the first call to `request_stop()` the corresponding exception is recorded and re-raised from `join()`.
object should_stop_dyn()
Check if the coordinator was told to stop. See `Coordinator.should_stop()`.
Returns
-
object
- True if the coordinator was told to stop, False otherwise.
object stop_on_exception_dyn()
Context handler to stop the supervisor when an exception is raised. See `Coordinator.stop_on_exception()`.
Returns
-
object
- A context handler.
bool wait_for_stop(Nullable<double> timeout)
Wait till the Coordinator is told to stop.
Parameters
-
Nullable<double>
timeout - Float. Sleep for up to that many seconds waiting for should_stop() to become True.
Returns
-
bool
- True if the Coordinator is told stop, False if the timeout expired.
object wait_for_stop_dyn(object timeout)
Wait till the Coordinator is told to stop.
Parameters
-
object
timeout - Float. Sleep for up to that many seconds waiting for should_stop() to become True.
Returns
-
object
- True if the Coordinator is told stop, False if the timeout expired.