coordinator.py

Coordinator 是一个用于协调一组线程停止的简单机制。线程通过定期检查 `coord.should_stop()` 来配合停止请求。当 `coord.request_stop()` 被调用时,`coord.should_stop()` 立即返回 `True`。协调器还支持异常处理,线程可以通过 `coord.request_stop(ex)` 报告异常,异常将在 `coord.join()` 时重新抛出。此外,`stop_grace_period_secs` 参数允许设置一个宽限期,让线程有时间优雅地终止。
摘要由CSDN通过智能技术生成
"""Coordinator to help multiple threads stop when requested."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib
import sys
import threading
import time

import six

from tensorflow.python.framework import errors
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.util import compat


class Coordinator(object):
  """A coordinator for threads.
  线程协调器

  This class implements a simple mechanism to coordinate the termination of a
  set of threads.
  这个类实现了一个简单的机制来协调一组线程。
  
  #### Usage:

  ```python
  # 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)
  ```

  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.
  任何线程可以调用 `coord.request_stop()` 来要求所有线程停止。
  为了配合请求,每个线程必须定期检查`coord.should_stop()`。
  `coord.should_stop()` 一旦返回`True`, `coord.request_stop()`就会马上被调用。 

  A typical thread running with a coordinator will do something like:
  与协调器运行的典型线程将执行以下操作:
  
  ```python
  while not coord.should_stop():
    ...do some work...
  ```

  #### Exception handling: 
  #### 异常处理:

  A thread can report an exception to the coordinator as part of the `should_stop()` call. 
  The exception will be re-raised from the `coord.join()` call.
  线程可以向协调器报告异常,作为 `should_stop()` 调用的一部分。
  将从 `coord.join()` 调用中重新提出异常。

  Thread code:

  ```python
  try:
    while not coord.should_stop():
      ...do some work...
  except Exception as e:
    coord.request_stop(e)
  ```

  Main code:

  ```python
  try:
    ...
    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)
  except Exception as e:
    ...exception that was passed to coord.request_stop()
  ```

  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:
  为了简化线程实现,协调器提供了一个上下文处理程序 `stop_on_exception()` ,它会自动请求停止如果
  提出异常。使用上下文处理程序,上面的线程代码可以写成:

  ```python
  with coord.stop_on_exception():
    while not coord.should_stop():
      ...do some work...
  ```

  #### 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 RuntimeException reporting the laggards.
  一个线程调用 `coord.request_stop()` 后,其他线程有一个固定的时间停止,这被称为 '停止宽限期' ,默认为2分钟。
  如果任何线程在宽限期到期后仍然存在,那么 `coord.join()` 引发一个 RuntimeException 来报告未停止的线程。
  
  
  ```python
  try:
    ...
    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, give them 10s grace period
    coord.join(threads, stop_grace_period_secs=10)
  except RuntimeException:
    ...one of the threads took more than 10s to stop after request_stop()
    ...was called.
  except Exception:
    ...exception that was passed to coord.request_stop()
  ```
  """

  def __init__(self, clean_stop_exception_types=None):
    """Create a new Coo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值