title: How can I solve this: "RuntimeError: Attempted to use a closed Session."
body: <p>When I run the following Tensorflow code, I receive a RuntimeError that says "Attempted to use a closed Session." Can someone tell me how to get around this error? Here is the code:</p>
  
  <pre><code># coding=utf-8
  # (...imports omitted...)
  
  # (...some constant declarations and helper functions omitted:
  #  max_steps, batch_size, log_dir, variable_with_weight_loss, variable_summaries,
  #  layer1, full_layer1, full_layer2, full_layer3, loss
  #  ...)
  
  def run():
      image, label = read_and_decode('train.tfrecords')
      batch_image, batch_label = get_batch(image, label, batch_size=128, crop_size=56) 
  
      test_image, test_label = read_and_decode('val.tfrecords')
      test_images, test_labels = get_test_batch(test_image, test_label, batch_size=128, crop_size=56)  # batch 生成测试
  
      def feed_dict(train):
          if train:
              x=image_batch
              y=label_batch
          else:
              x=img_batch
              y=lab_batch
          return {image_holder:x,label_holder:y}
  
      saver=tf.train.Saver()
      num_examples = 10000
      num_iter = int(math.ceil(num_examples / batch_size))
      true_count = 0
      total_sample_count = num_iter * batch_size
  
      init = tf.global_variables_initializer()
      with tf.Session() as sess:
          sess.run(init)
          merged = tf.summary.merge_all()
          train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph)
          test_writer = tf.summary.FileWriter(log_dir + '/test')
          coord = tf.train.Coordinator()
          threads = tf.train.start_queue_runners(coord=coord)
  
      for step in range(max_steps):
          start_time = time.time()
          image_batch, label_batch = sess.run([batch_image, batch_label])
  
      # (...rest of function omitted...)
  
  
  if __name__=='__main__':
      run()
  </code></pre>
  
  <p>Here is the exception that occurs when the code is run:</p>
  
  <pre><code>File "/home/vrview/tensorflow/example/char/tfrecords/color2_board.py", line 238, in &lt;module&gt;
      run()
    File "/home/vrview/tensorflow/example/char/tfrecords/color2_board.py", line 207, in run
      image_batch, label_batch = sess.run([batch_image, batch_label])
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
      run_metadata_ptr)
    File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 903, in _run
      raise RuntimeError('Attempted to use a closed Session.')
  RuntimeError: Attempted to use a closed Session.
  </code></pre>
  
  <p>Thanks for your help!</p>