Advertisement
Guest User

ThreadCount2.py

a guest
Feb 19th, 2016
205
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import threading
  2. import time
  3. import Queue
  4. import random
  5. import collections
  6. import console
  7.  
  8.  
  9. class WorkerCounterThread(threading.Thread):
  10.    
  11.     def __init__(self, stopnum, outputQueue, delay_val):
  12.         threading.Thread.__init__(self)
  13.         print"\nThread delay = %1.2f\n" % delay_val
  14.         self.count = 1
  15.         self.finished = False
  16.         self.stopnum = stopnum
  17.         self.output = outputQueue
  18.         self.delay_val = delay_val
  19.        
  20.     def run(self):
  21.        
  22.         try:
  23.             while not self.finished:
  24.            
  25.                 self.output.put(self.count)
  26.            
  27.                 if self.stopnum == self.count:
  28.                     self.stop()
  29.                 else:
  30.                     self.count += 1
  31.                     time.sleep(self.delay_val)
  32.         except KeyboardInterrupt:
  33.             print"THREAD BREAK\n"
  34.             self.stop()
  35.             #raise
  36.        
  37.     def stop(self):
  38.         self.finished = True
  39.        
  40. #################################################
  41.  
  42. def main():
  43.    
  44.     console.clear()
  45.    
  46.     results = Queue.Queue()
  47.     thread_delay = random.choice([.2, .4, .1, .19, .26, .3])
  48.     #thread_delay = .1
  49.     wc_thread = WorkerCounterThread(20, results, thread_delay)
  50.     wc_thread.start()
  51.    
  52.     loops = 0
  53.     accum =[]
  54.     accum_loops = 0
  55.     count_stats = collections.Counter()
  56.    
  57.     while 1:
  58.        
  59.         try:
  60.             while not results.empty():
  61.                 accum.append(results.get())
  62.            
  63.             if accum:
  64.                 accum_len = len(accum)
  65.                 for count in accum:
  66.                     print"Count=%2d, len(accum) = %d" % (count, accum_len)
  67.                     count_stats[accum_len] += 1
  68.                
  69.                 accum_loops += 1
  70.                 accum = []
  71.        
  72.             if not wc_thread.isAlive():
  73.                 break
  74.            
  75.             time.sleep(random.random())
  76.             loops += 1
  77.         except KeyboardInterrupt:
  78.             print"MAIN BREAK\n"
  79.             if wc_thread.isAlive():
  80.                 wc_thread.stop()
  81.                 print"Terminating thread in main\n"
  82.             break
  83.        
  84.     print"\nDONE: mainloops= {:,},accum_loops=%d\n".format(loops) % accum_loops
  85.    
  86.     for val, count in count_stats.iteritems():
  87.         print"Val = %2d, count = %2d" %(val, count)
  88.        
  89.    
  90. if __name__ == '__main__':
  91.     main()
Advertisement
RAW Paste Data Copied
Advertisement