Created
February 15, 2018 09:58
-
-
Save solaris33/91f4e786068b28049bc776356d94d71e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# original source: | |
# https://github.com/Hezi-Resheff/Oreilly-Learning-TensorFlow/blob/master/08__queues_threads/queue_basic.py | |
from __future__ import print_function | |
import tensorflow as tf | |
import threading | |
import time | |
# 세션을 실행한다. | |
sess = tf.InteractiveSession() | |
# 사이즈 100 큐를 생성하고 eqneue 노드를 정의한다. | |
gen_random_normal = tf.random_normal(shape=()) | |
queue = tf.FIFOQueue(capacity=100, dtypes=[tf.float32], shapes=()) | |
enque = queue.enqueue(gen_random_normal) | |
# 10개의 임의의 값을 enqueue하는 add 함수를 정의한다. | |
def add(): | |
for i in range(10): | |
sess.run(enque) | |
# 10개의 쓰레드를 만들고 각각의 쓰레드가 병렬로(parallel) add 함수를 비동기적으로(asynchronous) 실행한다. | |
threads = [threading.Thread(target=add, args=()) for i in range(10)] | |
for t in threads: | |
t.start() | |
# 10개의 쓰레드가 병렬적으로 연산을 수행한다. | |
# 아웃풋 예시 : | |
# 25 | |
# 77 | |
# 100 | |
print(sess.run(queue.size())) | |
time.sleep(0.001) | |
print(sess.run(queue.size())) | |
time.sleep(0.001) | |
print(sess.run(queue.size())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment