๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dsa_queues.asp
Queues with Python
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_module_queue.asp
Python queue Module
Python Training ... The queue module provides synchronized queue classes for multi-producer, multi-consumer scenarios. Use it to safely pass work between threads using FIFO, LIFO, or priority ordering.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ queue-in-python
Queue in Python - GeeksforGeeks
December 11, 2025 - Queue is a linear data structure that stores items in a First In First Out (FIFO) manner. The item that is added first will be removed first.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
February 23, 2026 - The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved ...
๐ŸŒ
W3Schools
w3schools.com โ€บ dsa โ€บ dsa_data_queues.php
DSA Queues
But to explicitly create a data structure for queues, with basic operations, we should create a queue class instead. This way of creating queues in Python is also more similar to how queues can be created in other programming languages like C and Java.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ asyncio-queue.html
Queues โ€” Python 3.14.4 documentation
February 22, 2026 - Source code: Lib/asyncio/queues.py asyncio queues are designed to be similar to classes of the queue module. Although asyncio queues are not thread-safe, they are designed to be used specifically i...
๐ŸŒ
Python W3schools
pythonw3schools.com โ€บ home โ€บ queue in python
Queue in Python - Python W3schools
March 17, 2023 - Python W3schools ยท In Python, a queue can be implemented using the built-in queue module.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ oop โ€บ python-oop-exercise-10.php
Python OOP: Queue class with enqueue and dequeue methods
July 9, 2025 - Explore object-oriented programming (OOP) in Python by creating a queue class. Learn how to implement methods for adding elements to the queue (enqueue) and removing elements from the queue (dequeue).
Find elsewhere
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ queue in python: working with queue data structure in python
Queue in Python: Working With Queue Data Structure in Python
March 5, 2026 - A queue is a built-in module of python used in threaded programming. It stores items sequentially in a FIFO manner. Learn all about the queue in python now!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
W3Schools
w3schools.com โ€บ jquery โ€บ eff_queue.asp
jQuery queue() Method
Count the length of the queue + ... loop the queue. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-implement-a-queue-in-python
How to implement a queue in Python
Python offers multiple ways to implement queues, each suited to specific needs: lists for simplicity, the Queue module for thread-safe operations in concurrent programming, and the collections.deque module for efficient enqueue and dequeue operations.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ queue
queue | Python Standard Library โ€“ Real Python
In this tutorial, you'll take a deep dive into the theory and practice of queues in programming. Along the way, you'll get to know the different types of queues, implement them, and then learn about the higher-level queues in Python's standard library.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ python_data_structure โ€บ python queue data structure
Python Data Structure - Queue
February 13, 2026 - We are familiar with queue in our day to day life as we wait for a service. The queue data structure aslo means the same where the data elements are arranged in a queue. The uniqueness of queue lies in the way items are added and removed.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ pythonds โ€บ BasicDS โ€บ ImplementingaQueueinPython.html
4.12. Implementing a Queue in Python โ€” Problem Solving with Algorithms and Data Structures
It is again appropriate to create a new class for the implementation of the abstract data type queue. As before, we will use the power and simplicity of the list collection to build the internal representation of the queue ยท We need to decide which end of the list to use as the rear and which ...
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python queue
Python Queue
October 14, 2024 - In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs. ... As we just discussed what is a queue. It is the same in Python and works on the same methodology โ€œFirst in First Outโ€ (FIFO).
Top answer
1 of 2
23

The for loop is launching a number of worker threads to perform the function defined by "worker". Here is working code that should run on your system in python 2.7.

import Queue
import threading

# input queue to be processed by many threads
q_in = Queue.Queue(maxsize=0)

# output queue to be processed by one thread
q_out = Queue.Queue(maxsize=0)

# number of worker threads to complete the processing
num_worker_threads = 10

# process that each worker thread will execute until the Queue is empty
def worker():
    while True:
        # get item from queue, do work on it, let queue know processing is done for one item
        item = q_in.get()
        q_out.put(do_work(item))
        q_in.task_done()

# squares a number and returns the number and its square
def do_work(item):
    return (item,item*item)

# another queued thread we will use to print output
def printer():
    while True:
        # get an item processed by worker threads and print the result. Let queue know item has been processed
        item = q_out.get()
        print "%d squared is : %d" % item
        q_out.task_done()

# launch all of our queued processes
def main():
    # Launches a number of worker threads to perform operations using the queue of inputs
    for i in range(num_worker_threads):
         t = threading.Thread(target=worker)
         t.daemon = True
         t.start()

    # launches a single "printer" thread to output the result (makes things neater)
    t = threading.Thread(target=printer)
    t.daemon = True
    t.start()

    # put items on the input queue (numbers to be squared)
    for item in range(10):
        q_in.put(item)

    # wait for two queues to be emptied (and workers to close)   
    q_in.join()       # block until all tasks are done
    q_out.join()

    print "Processing Complete"

main()

Python 3 version per @handle

import queue 
import threading

# input queue to be processed by many threads
q_in = queue.Queue(maxsize=0) 

# output queue to be processed by one thread
q_out = queue.Queue(maxsize=0) 

# number of worker threads to complete the processing
num_worker_threads = 10

# process that each worker thread will execute until the Queue is empty
def worker():
    while True:
        # get item from queue, do work on it, let queue know processing is done for one item
        item = q_in.get()
        q_out.put(do_work(item))
        q_in.task_done()

# squares a number and returns the number and its square
def do_work(item):
    return (item,item*item)

# another queued thread we will use to print output
def printer():
    while True:
        # get an item processed by worker threads and print the result. Let queue know item has been processed
        item = q_out.get()
        print("{0[0]} squared is : {0[1]}".format(item) )
        q_out.task_done()

# launch all of our queued processes
def main():
    # Launches a number of worker threads to perform operations using the queue of inputs
    for i in range(num_worker_threads):
         t = threading.Thread(target=worker)
         t.daemon = True
         t.start()

    # launches a single "printer" thread to output the result (makes things neater)
    t = threading.Thread(target=printer)
    t.daemon = True
    t.start()

    # put items on the input queue (numbers to be squared)
    for item in range(10):
        q_in.put(item)

    # wait for two queues to be emptied (and workers to close)   
    q_in.join()       # block until all tasks are done
    q_out.join()

    print( "Processing Complete" )

main()
2 of 2
3

You can think of the number of worker threads as the number of bank tellers at a bank. So people (your items) stand in line (your queue) to be processed by a bank teller (your worker thread). Queues are actually an easy and well understood mechanism to manage complexities in threads.

I have adjusted your code a bit to show how it works.

import queue
import time
from threading import Thread

def do_work(item):
    print("processing", item)

def source():
    item = 1
    while True:
        print("starting", item)
        yield item
        time.sleep(0.2)
        item += 1

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = queue.Queue(maxsize=0)
def main():
    for i in range(2):
        t = Thread(target=worker)
        t.daemon = True
        t.start()

    for item in source():
        q.put(item)

    q.join()       # block until all tasks are done

main()
๐ŸŒ
RQ
python-rq.org
RQ: Simple job queues for Python
Queue: contains a list of Job instances to be executed in a FIFO manner.
๐ŸŒ
Medium
basillica.medium.com โ€บ working-with-queues-in-python-a-complete-guide-aa112d310542
Working with Queues in Python โ€” A Complete Guide | by Basillica | Medium
March 27, 2024 - Working with Queues in Python โ€” A Complete Guide Queues are a useful data structure in programming that allow you to add and remove elements in a first in, first out (FIFO) order. Python provides a โ€ฆ
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ queue in python with examples
Queue in Python with Examples - Spark By {Examples}
May 31, 2024 - It allows you to add and remove elements from the front and back of the queue in constant time, making it an efficient data structure for many applications. from collections import deque # Create a deque my_deque = deque(['Python', 'Java', 'C++', ...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ queue in python โ€“ implementation explained
Queue in Python: How to Implement Queue in Python
October 14, 2025 - Queue in Python is an important concept in Data Structure. In this tutorial, you will learn about python queue โœ”๏ธ python priority queue โœ”๏ธ circular queue in python โœ”๏ธ and more.