queue.queue python code example
Example 1: queue python
from queue import Queue
q = Queue()
q.size()
q.empty()
q.put(item)
q.get()
Example 2: python list as queue
queue = []
queue.append('a')
queue.append('b')
print(queue)
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements")
print(queue)
Example 3: python threading queue
import threading, queue
q = queue.Queue()
def worker():
while True:
item = q.get()
print(f'Working on {item}')
print(f'Finished {item}')
q.task_done()
threading.Thread(target=worker, daemon=True).start()
for item in range(30):
q.put(item)
print('All task requests sent\n', end='')
q.join()
print('All work completed')