Import tensorflow AS TF # 1. Create a queue and operating elements inside. tf.FIFOQueue = Q (2, " Int32 " ) the init = q.enqueue_many (([0, 10 ],)) X = q.dequeue () Y = X +. 1 q_inc = q.enqueue ([Y]) with tf.Session () AS Sess: init.run () for _ in Range (. 5 ): V, _ = sess.run ([X, q_inc]) Print (V)
Import Time Import Threading Import numpy AS NP # 2. This program every second to determine whether you need to stop and print your own ID. DEF myLoop (coord, worker_id): the while Not coord.should_stop (): IF np.random.rand () <0.1 : Print ( " STOPPING ID from: D% \ n- " % worker_id, coord.request_stop ()) the else : Print ( " Working ON the above mentioned id:% d \ the n- " % worker_id, the time.sleep (1 )) # 3. create, start and exit the thread. = coord tf.train.Coordinator () Threads= [threading.Thread(target=MyLoop, args=(coord, i, )) for i in range(6)] for t in threads: t.start() coord.join(threads)