网上常见的解决方式是
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
但是要注意的是 queue 的启动要放在 queue 的定义之后
如:
sess = tf.Session(config=config)
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
coord = tf.train.Coordinator()
# queue 的启动
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# queue 的定义
tmp = tf.train.string_input_producer(['./test.tfrecords'], shuffle=True)
reader = tf.TFRecordReader(options = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.ZLIB))
_, serialized_example = reader.read(tmp)
sess.run(serialized_example)
上面的代码会卡在sess.run()
如果修改成:
sess = tf.Session(config=config)
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
coord = tf.train.Coordinator()
# queue 的定义
tmp = tf.train.string_input_producer(['./test.tfrecords'], shuffle=True)
reader = tf.TFRecordReader(options = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.ZLIB))
_, serialized_example = reader.read(tmp)
# queue 的启动
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
sess.run(serialized_example)
就不会卡住了
(其实如果仔细读 log, 会发现 tensorflow 早就 warn 过了 WARNING:tensorflow:tf.train.start_queue_runners()
was called when no queue runners were defined. You can safely remove the call to this deprecated function.)