No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

67 líneas
1.7 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='producer thread',custom_id=0,daemon=False):
  10. super(ProducerThread, self).__init__()
  11. self.name = "{}-{}".format(name,custom_id)
  12. self.setDaemon(daemon)
  13. #def threadjoin(self):
  14. # super(ProducerThread,self).join()
  15. global inputQueue
  16. def run(self):
  17. numbers = range(10)
  18. counter = 0
  19. while counter < 20:
  20. num = random.choice(numbers)
  21. inputQueue.put(num)
  22. print("\nPut",num)
  23. time.sleep(0.5)
  24. counter+=1
  25. class ConsumerProcess1(mp.Process):
  26. def __init__(self,name='consumer thread',custom_id=0,daemon=False):
  27. super(ConsumerProcess1,self).__init__()
  28. self.name = "{}-{}".format(name,custom_id)
  29. self.daemon = daemon
  30. #def consumerjoin(self):
  31. # super(ConsumerProcess1,self).join()
  32. # return
  33. global inputQueue
  34. def run(self):
  35. while (not inputQueue.empty()):
  36. num = inputQueue.get()
  37. inputQueue.task_done()
  38. print("\nGot", num)
  39. time.sleep(1)
  40. if __name__ == "__main__":
  41. print("this is example 2")
  42. time.sleep(2)
  43. a=ProducerThread(name = 'producer thread',custom_id=1,daemon=False)
  44. b=ConsumerProcess1(name = 'consumer process',custom_id=1,daemon=True)
  45. a.start()
  46. b.start()
  47. print(a.ident,a.name,"\n")
  48. print(b.ident,b.name,"\n")
  49. a.join()
  50. b.join()
  51. print('end of program mark')