Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- from queue import Queue
- from threading import Thread
- def produce(queue):
- nums = range(5)
- while True:
- num = random.choice(nums)
- queue.put(num)
- print('Produced:', num)
- time.sleep(random.randint(0, 5))
- def consume(queue):
- while True:
- num = queue.get()
- queue.task_done()
- print('Consumed:', num)
- time.sleep(random.randint(0, 5))
- def main():
- q = Queue(10)
- producer = Thread(target=produce, args=(q,))
- producer.start()
- consumer = Thread(target=consume, args=(q,))
- consumer.start()
- producer.join()
- consumer.join()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment