python get item from queue
"""put and get items from queue"""
>>> from Queue import Queue
>>> q = Queue()
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
>>> print list(q.queue)
[1, 2, 3]
>>> q.get()
1
>>> print list(q.queue)
[2, 3]
python get item from queue
"""put and get items from queue"""
>>> from Queue import Queue
>>> q = Queue()
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
>>> print list(q.queue)
[1, 2, 3]
>>> q.get()
1
>>> print list(q.queue)
[2, 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()
# turn-on the worker thread
threading.Thread(target=worker, daemon=True).start()
# send thirty task requests to the worker
for item in range(30):
q.put(item)
print('All task requests sentn', end='')
# block until all tasks are done
q.join()
print('All work completed')
queues in python
class Queue:
def __init__(self, capacity):
self.front = self.size = 0
self.rear = capacity - 1
self.Q = [None] * capacity
self.capacity = capacity
def isFull(self):
return self.size == self.capacity
def isEmpty(self):
return self.size == 0
def EnQueue(self, item):
if self.isFull():
print("Full")
return
self.rear = (self.rear + 1) % (self.capacity)
self.Q[self.rear] = item
self.size = self.size + 1
print("%s enqueue to queue" %str(item))
def DeQueue(self):
if self.isEmpty():
return "Empty"
print("%s dequeued from queue" %str(self.Q[self.front]))
self.front = (self.front + 1) % (self.capacity)
self.size = self.size - 1
def que_front(self):
if self.isEmpty():
print("The Queue is empty")
print("Front item is ", self.Q[self.front])
def que_rear(self):
if self.isEmpty():
print("Queue is Empty")
print("The rear item is ", self.Q[self.rear])
queue = Queue(30)
queue.EnQueue(10)
queue.EnQueue(20)
queue.EnQueue(30)
queue.EnQueue(40)
queue.EnQueue(50)
queue.que_front()
print()
queue.DeQueue()
queue.que_front()
queue.que_rear()
print()
queue.DeQueue()
queue.que_front()
queue.que_rear()
python list as queue
# Demonstrate queue implementation using list
# Initializing a queue
queue = []
# Adding elements to the queue
queue.append('a')
queue.append('b')
print(queue)
# Removing elements from the queue
print("nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print("nQueue after removing elements")
print(queue)
# print(queue.pop(0)) will raise and IndexError as the queue is now empty
queue in python
#creating a queue using list
#defining a list
my_queue =[]
#inserting the items in the queue
my_queue.append(1)
my_queue.append(2)
my_queue.append(3)
my_queue.append(4)
my_queue.append(5)
print("The items in queue:")
print(my_queue)
#removing items from queue
print(my_queue.pop(0))
print(my_queue.pop(0))
print(my_queue.pop(0))
print(my_queue.pop(0))
#printing the queue after removing the elements
print("The items in queue:")
print(my_queue)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us