Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- __author__ = 'Julio Tentor <jtentor@fi.unju.edu.ar>'
- '''
- Multithread Queue
- https://docs.python.org/3/library/queue.html
- '''
- import threading, queue
- q = queue.Queue()
- def consumer1():
- while True:
- item = q.get()
- print("C1 Consume", item)
- q.task_done()
- def producer1():
- for item in range(5):
- q.put("P1 " + str(item))
- def consumer2():
- while True:
- item = q.get()
- print("C2 Consume", item)
- q.task_done()
- def producer2():
- for item in range(5):
- q.put("P2 " + str(item))
- def producer3():
- for item in range(5):
- q.put("P3 " + str(item))
- # start threads
- threading.Thread(target=consumer1, daemon=True).start()
- threading.Thread(target=consumer2, daemon=True).start()
- threading.Thread(target=producer1, daemon=True).start()
- threading.Thread(target=producer2, daemon=True).start()
- threading.Thread(target=producer3, daemon=True).start()
- # wait
- q.join()
- print("la cola está vacía")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement