# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
import threading
import time
queue = tf.FIFOQueue(100, 'float')
enqueue_op = queue.enqueue([tf.random_normal([1])])
#表示需要启动5个线程,每个线程运行的是enqueue_op操作
qr = tf.train.QueueRunner(queue, [enqueue_op] * 5)
#将qr加入默认的tf.Graphkeys.QUEUE_RUNNERS集合
tf.train.add_queue_runner(qr)
out_tensor = queue.dequeue()
with tf.Session() as sess:
coord = tf.train.Coordinator()
#启动所有线程
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for _ in range(3):
print(sess.run(out_tensor)[0])
coord.request_stop()
# 等待所有线程退出
coord.join(threads)
运行结果
-0.3205996
1.9861126
0.0073759328