Free cookie consent management tool by TermsFeed full() method of Queue class in Python | Pythontic.com

Full() method of Queue class in Python

Method Name:

full

Method Signature:

full()

Paramaters:

None

Return Value:

Returns True, if the queue is full.

Returns False, if the queue is not full.

Overview:

  • The method full() does the opposite of what empty() does with a Queue instance.
  • It returns True, if the queue has reached its maximum capacity. Returns False otherwise.
  • Since a Queue instance is synchronized(not reentrant) and used in a producer(s)/consumer(s) scenario, the return value of full() method does not guarantee that a subsequent call to put() or get() will not block.

Example:

# Example Python program that uses the Queue.full()

# method before adding or removing elements from the queue

import queue

import time

import threading

 

# Constitutes the consumer thread

def consume(aQueue):

    while(True):

        if aQueue.full() is True:

            elem = aQueue.get();

            time.sleep(1);

            print("Function consume():Removed an element: %s"%elem);

 

# Constitutes the producer thread

def produce(aQueue):

    while(True):

        elem = time.time();

        aQueue.put(elem);

        time.sleep(1);

        print("Function produce():Added an element: %s"%elem);

 

# Create a queue with a maximum capacity of five elements

aQueue = queue.Queue(maxsize=5);

producerThread = threading.Thread(target=produce, args=(aQueue,));

consumerThread = threading.Thread(target=consume, args=(aQueue,));

 

# Start the threads

producerThread.start();

consumerThread.start();

 

# Wait for the threads to complete

producerThread.join();

consumerThread.join();

 

Output:

Function produce():Added an element: 1574661369.6284108

Function produce():Added an element: 1574661370.645338

Function produce():Added an element: 1574661371.678029

Function produce():Added an element: 1574661372.708281

Function produce():Added an element: 1574661373.725653

Function consume():Removed an element: 1574661369.6284108

Function produce():Added an element: 1574661374.730867

Function consume():Removed an element: 1574661370.645338

Function consume():Removed an element: 1574661371.678029

Function produce():Added an element: 1574661375.733293


Copyright 2025 © pythontic.com