Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

53 lignes
1.4 KiB

  1. from threading import Thread
  2. import multiprocessing as mp
  3. import time
  4. import random
  5. from queue import Queue
  6. inputQueue = mp.JoinableQueue()
  7. inputQueue2 = mp.Queue(10)
  8. class ProducerThread(Thread):
  9. def __init__(self,name='',custom_id,daemon=False):
  10. super(ProducerThread, self).__init__()
  11. self.name = "{}-{}".format(name,custom_id)
  12. self.setDaemon(daemon)
  13. global inputQueue
  14. def run(self):
  15. numbers = range(40)
  16. counter = 0
  17. while counter < 20:
  18. num = random.choice(numbers)
  19. inputQueue.put(num)
  20. print("\nPut",num)
  21. time.sleep(.08)
  22. counter+=1
  23. class ConsumerProcess1(Thread):
  24. def __init__(self,name,custom_id,daemon=False):
  25. super(ConsumerProcess1,self).__init__()
  26. self.name = "{}-{}".format(name,custom_id)
  27. self.daemon = daemon
  28. global inputQueue
  29. def run(self):
  30. while (not inputQueue.empty()):
  31. num = inputQueue.get()
  32. inputQueue.task_done()
  33. print("\nGot", num)
  34. time.sleep(.1)
  35. if __name__ == "__main__":
  36. print("this is example 2")
  37. time.sleep(2)
  38. a=ProducerThread(name = 'producer thread',custom_id=1,daemon=False)
  39. b=ConsumerProcess1(name = 'consumer process',custom_id=1,daemon=True)
  40. a.start()
  41. b.start()
  42. print(a.ident,a.name,"\n")
  43. print(b.ident,b.name,"\n")