SHARE
TWEET

threading workers printer not complete

DeaD_EyE Jun 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from argparse import ArgumentParser
  2. import time
  3. from random import random
  4. from threading import Thread, Event
  5. from queue import Queue, Empty
  6.  
  7.  
  8. class MyThread(Thread):
  9.    
  10.     def __init__(self, thread_id, name, counter, message, result_queue, event):
  11.         super().__init__()
  12.         self.thread_id = thread_id
  13.         self.name = name
  14.         self.counter = counter
  15.         self.message = message
  16.         self.result_queue = result_queue
  17.         self.event = event
  18.    
  19.     def run(self):
  20.         print (f"{self.name} started")
  21.         for i in range(self.counter):
  22.             # each iteration simulates a big_calculation
  23.             # looking, if self.event is set
  24.             if self.event.is_set():
  25.                 break
  26.                 # thread ends here
  27.                 # if event is_set
  28.             time.sleep(random() * 5)
  29.             # simulates random time for computation
  30.             self.result_queue.put(self.message)
  31.         # for-loop is finite
  32.  
  33.  
  34. class Printer(Thread):
  35.     def __init__(self, result_queue, event):
  36.         super().__init__()
  37.         self.result_queue = result_queue
  38.         self.event = event
  39.     def run(self):
  40.         while not self.event.is_set():
  41.             try:
  42.                 result = self.result_queue.get_nowait()
  43.             except Empty:
  44.                 time.sleep(0.5)
  45.             else:
  46.                 self.result_queue.task_done()
  47.                 print(result)
  48.            
  49.  
  50. def main(workers):
  51.     result_queue = Queue()
  52.     event = Event()
  53.     threads = [
  54.         MyThread(
  55.             thread_id=n,
  56.             name=f'Thread {n}',
  57.             counter=15,
  58.             message=f'Do something {n}',
  59.             result_queue=result_queue,
  60.             event=event,
  61.         )
  62.         for n in range(1, workers + 1)
  63.     ]
  64.     [t.start() for t in threads]
  65.     print("Threads started, adding printer")
  66.     printer = Printer(result_queue, event)
  67.     printer.start()
  68.     print("Sleeping for 10 seconds")
  69.     time.sleep(10)
  70.     print("Set event, which will end all threads after the next iteration of all threads")
  71.     event.set()
  72.  
  73.    
  74. if __name__ == '__main__':
  75.     parser = ArgumentParser(description='Example program for threads')
  76.     parser.add_argument('threads', type=int, help='Amount of worker threads')
  77.     args = parser.parse_args()
  78.     main(args.threads)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top