Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou takatakamanbou/ex1101Q.py Secret
Last active Nov 21, 2015

Embed
What would you like to do?
import threading
import Queue
import time
import sys
# class for sub-thread
#
class MyThread(threading.Thread):
# qsize: maximum number of items that can be stored in the queue
# vals: values delivered from 'sub' to 'main'
# sleeptime: sleep time for each enqueuing
#
def __init__(self, qsize, vals, sleeptime):
super(MyThread, self).__init__()
self.q = Queue.Queue(maxsize = qsize)
self.vals = vals
self.sleeptime = sleeptime
def run(self):
for val in vals:
print ' sub:', val
time.sleep(self.sleeptime)
self.q.put(val) # enqueue
def get(self):
return self.q.get() # dequeue
if __name__ == '__main__':
qsize = 3
main_st = 0.2 # sleep time for 'main'
sub_st = 2 # sleep time for 'sub'
vals = range(10)
th = MyThread(qsize, vals, sub_st)
t_start = time.time()
th.start()
for i in range(len(vals)):
val = th.get()
print 'main:', val
time.sleep(main_st)
t_elapsed = time.time() - t_start
print '# elapsed time:', t_elapsed, '[sec]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.