Skip to content

Instantly share code, notes, and snippets.

Last active November 21, 2015 13:07
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
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()
for i in range(len(vals)):
val = th.get()
print 'main:', val
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