- 參考
- 整體流程如下圖
- 代碼
import tensorflow as tf
"""
模擬: 子線程不停的取數據放入隊列中, 主線程從隊列中取數據執行
包含: 作用域的命名、把程序的圖結構寫入事件、多線程
"""
with tf.variable_scope("init_sub_threads"):
Q = tf.FIFOQueue(1000, tf.float32)
with tf.variable_scope("add_one"):
var = tf.Variable(0.0, tf.float32)
data = tf.assign_add(var, tf.constant(1.0))
en_q = Q.enqueue(data)
with tf.variable_scope("init_queue_runner"):
qr = tf.train.QueueRunner(Q, enqueue_ops=[en_q] * 2)
with tf.variable_scope("init_var"):
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = qr.create_threads(sess, start = True)
for i in range(300):
print(sess.run(Q.dequeue()))
filewriter = tf.summary.FileWriter("./summary/", graph=sess.graph)
coord.request_stop()
coord.join(threads)