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

45 lines
850 B

  1. from threading import Thread
  2. import multiprocessing as mp
  3. import time
  4. import random
  5. from queue import Queue
  6. inputQueue = mp.Queue(10)
  7. inputQueue2 = mp.Queue(10)
  8. class ProducerThread(Thread):
  9. global inputQueue
  10. def run(self):
  11. numbers = range(40)
  12. while True:
  13. num = random.choice(numbers)
  14. inputQueue.put(num)
  15. print("\nPut",num)
  16. time.sleep(.05)
  17. class ConsumerProcess1(mp.Process):
  18. global inputQueue
  19. def run(self):
  20. while True:
  21. num = inputQueue.get()
  22. print("\nGot", num)
  23. time.sleep(.1)
  24. class ConsumerProcess2(mp.Process):
  25. global
  26. if __name__ == "__main__":
  27. print("this is example 2")
  28. time.sleep(2)
  29. a=ProducerThread()
  30. b=ConsumerProcess1()
  31. a.start()
  32. b.start()
  33. a.join()
  34. b.join()