TensorFlow > link > tf.train.Coordinator() > Coordinator and QueueRunner @ tensorflow.org/.../api_docs/python / Threading and Queues

```txt:動作環境
Ubuntu 14.04 LTS desktop amd64
GeForce GTX 750 Ti
ASRock Z170M Pro4S [Intel Z170chipset]
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v7.5
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
```

TensorFlowを学習中。

TensorFlowを遊び倒す！ 4-1. Convolutional Neural Networks
http://blog.brainpad.co.jp/entry/2016/04/22/170000
https://www.tensorflow.org/versions/master/tutorials/deep_cnn/index.html

cifar10_eval.pyを見ている。

```cifar10_eval.py
  with tf.Session() as sess:
...
    # Start the queue runners.
    coord = tf.train.Coordinator()
```

上記に関連しそうなリンク。

### [Coordinator and QueueRunner](https://www.tensorflow.org/versions/r0.11/api_docs/python/train.html#coordinator-and-queuerunner)

- class tf.train.Coordinator
  - `tf.train.Coordinator.__init__(clean_stop_exception_types=None)`
  - tf.train.Coordinator.clear_stop()
  - tf.train.Coordinator.join(threads=None, stop_grace_period_secs=120)
  - tf.train.Coordinator.joined
  - tf.train.Coordinator.register_thread(thread)
  - tf.train.Coordinator.request_stop(ex=None)
  - tf.train.Coordinator.should_stop()
  - tf.train.Coordinator.stop_on_exception()
  - tf.train.Coordinator.wait_for_stop(timeout=None)
- class tf.train.QueueRunner
  - `tf.train.QueueRunner.__init__(queue=None, enqueue_ops=None, close_op=None, cancel_op=None, queue_closed_exception_types=None, queue_runner_def=None)`
  - tf.train.QueueRunner.cancel_op
  - tf.train.QueueRunner.close_op
  - tf.train.QueueRunner.create_threads(sess, coord=None, daemon=False, start=False)
  - tf.train.QueueRunner.enqueue_ops
  - tf.train.QueueRunner.exceptions_raised
  - tf.train.QueueRunner.from_proto(queue_runner_def)
  - tf.train.QueueRunner.name
  - tf.train.QueueRunner.queue
  - tf.train.QueueRunner.queue_closed_exception_types
  - tf.train.QueueRunner.to_proto()
  - tf.train.add_queue_runner(qr, collection='queue_runners')
  - tf.train.start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection='queue_runners')

### [Threading and Queues](https://www.tensorflow.org/versions/r0.11/how_tos/threading_and_queues/index.html)

気になった部分の抜粋

- 第1節
  - Like everything in TensorFlow, a queue is a node in a TensorFlow graph.
  - other nodes can modify its content.
  - We will create a "first in, first out" queue (FIFOQueue)
  - <font color=red>Enqueue, EnqueueMany, and Dequeue</font> are special nodes.
- Queue Use Overview節
  - such as <font color=red>FIFOQueue and RandomShuffleQueue</font>
  - ... is to use a RandomShuffleQueue to prepare inputs for training a model:
  - The TensorFlow Session object is multithreaded
  - However, it is not always easy to implement a Python program that drives threads as described above. 
  - <font color=red>The Coordinator class</font> helps multiple threads stop together and report exceptions to a program that waits for them to stop. 
  - <font color=red>The QueueRunner class</font> is used to create a number of threads cooperating to enqueue tensors in the same queue.
- Coordinator節
  - Its key methods are:
     - `should_stop()`: returns True if the threads should stop.
     - `request_stop(<exception>)`: requests that threads should stop.
     - `join(<list of threads>)`: waits until the specified threads have stopped.
         - (補足) POSIXスレッド?のjoinを思い出す
  - (補足) サンプルコードが掲載されている
  - The coordinator also has support to capture and report exceptions.
- QueueRunner節
  - These threads can use a coordinator to stop together. 
  - In addition, a queue runner runs a closer thread that automatically closes the queue if an exception is reported to the coordinator.
  - (補足) 学習への入力を用意するサンプルコードが掲載れている
  - (補足) Coordinatorを作成しqueue runnerを開始するサンプルコードが掲載されている
- Handling Exceptions節
  - <font color=red>OutOfRangeError</font> which is used to report that a queue was closed.
  - (補足) 上の方で掲載された学習ループの改良版サンプルコードが掲載されている