- 参考
- 整体流程如下图
- 代码
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)