25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930
  1. from threading import Thread
  2. import time
  3. import random
  4. from queue import Queue
  5. queue = Queue(10)
  6. class ProducerThread(Thread):
  7. def run(self):
  8. nums = range(40)
  9. global queue
  10. while True:
  11. num = random.choice(nums)
  12. queue.put(num)
  13. print ("Produced", num)
  14. time.sleep(random.random())
  15. class ConsumerThread(Thread):
  16. def run(self):
  17. global queue
  18. while True:
  19. num = queue.get()
  20. queue.task_done()
  21. print ("Consumed", num)
  22. time.sleep(random.random())
  23. ProducerThread().start()
  24. ConsumerThread().start()