Guest User

Untitled

a guest
Apr 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. from queue import Queue
  5. from threading import Thread
  6.  
  7.  
  8. def produce(queue):
  9. nums = range(5)
  10. while True:
  11. num = random.choice(nums)
  12. queue.put(num)
  13. print('Produced:', num)
  14. time.sleep(random.randint(0, 5))
  15.  
  16. def consume(queue):
  17. while True:
  18. num = queue.get()
  19. queue.task_done()
  20. print('Consumed:', num)
  21. time.sleep(random.randint(0, 5))
  22.  
  23. def main():
  24. q = Queue(10)
  25.  
  26. producer = Thread(target=produce, args=(q,))
  27. producer.start()
  28.  
  29. consumer = Thread(target=consume, args=(q,))
  30. consumer.start()
  31.  
  32. producer.join()
  33. consumer.join()
  34.  
  35. if __name__ == '__main__':
  36. main()
Add Comment
Please, Sign In to add comment