Advertisement
jtentor

DemoQueue5 - DemoQueue5.py

May 18th, 2020
1,146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. __author__ = 'Julio Tentor <jtentor@fi.unju.edu.ar>'
  4.  
  5. '''
  6. Multithread Queue
  7. https://docs.python.org/3/library/queue.html
  8. '''
  9.  
  10. import threading, queue
  11.  
  12.  
  13. q = queue.Queue()
  14.  
  15. def consumer1():
  16.     while True:
  17.         item = q.get()
  18.         print("C1 Consume", item)
  19.         q.task_done()
  20.  
  21. def producer1():
  22.     for item in range(5):
  23.         q.put("P1 " + str(item))
  24.  
  25. def consumer2():
  26.     while True:
  27.         item = q.get()
  28.         print("C2 Consume", item)
  29.         q.task_done()
  30.  
  31. def producer2():
  32.     for item in range(5):
  33.         q.put("P2 " + str(item))
  34.  
  35. def producer3():
  36.     for item in range(5):
  37.         q.put("P3 " + str(item))
  38.  
  39. # start threads
  40. threading.Thread(target=consumer1, daemon=True).start()
  41. threading.Thread(target=consumer2, daemon=True).start()
  42.  
  43. threading.Thread(target=producer1, daemon=True).start()
  44. threading.Thread(target=producer2, daemon=True).start()
  45. threading.Thread(target=producer3, daemon=True).start()
  46.  
  47.  
  48. # wait
  49. q.join()
  50.  
  51. print("la cola está vacía")
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement