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()
Answer from Paul Seeb on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 2 โ€บ library โ€บ queue.html
8.10. Queue โ€” A synchronized queue class โ€” Python 2.7.18 documentation
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case). New in version 2.3: The timeout parameter.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ Queue
Queue โ€“ A thread-safe FIFO implementation - Python Module of the Week
It processes items in the queue one after another. These daemon threads go into an infinite loop, and only exit when the main thread ends. """ while True: print '%s: Looking for the next enclosure' % i url = q.get() print '%s: Downloading:' % i, url # instead of really downloading the URL, ...
Discussions

multithreading - Learning about Queue module in python (how to run it) - Stack Overflow
Was recently introduced to the queue design in regards to ability to defer processing as well as implementing a "FIFO" etc. Looked through the documentation in attempt to get a sample queue going... More on stackoverflow.com
๐ŸŒ stackoverflow.com
multithreading - Python 2.7 threading queue, reuse threads - Stack Overflow
I am looking for a way to have a background thread queue run for an unlimited amount of times. The code below is what I came up with from research, but I am being limited to the amount of threads I More on stackoverflow.com
๐ŸŒ stackoverflow.com
Huey task queue 2.0
Big fan of Huey, thanks for all the work! More on reddit.com
๐ŸŒ r/Python
20
31
April 2, 2019
How to merge two Queues?
I am not sure where your "queueTail" variable is coming from. But where your writing into queueData seems all wrong. In the first for loop, you likely want to be setting at index "i". In the second loop you likely want to be setting at index "j". Though your second for loop is not starting j at the right position. "j" should be based off the length of q.length (no +1 needed). The greater question is why are you not using a queue which is built into java? Look into ArrayDeque for example. More on reddit.com
๐ŸŒ r/javahelp
4
3
October 2, 2014
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ queue-in-python
Queue in Python - GeeksforGeeks
December 11, 2025 - Explanation: popleft() efficiently removes the first element without shifting, making deque ideal for queues. Pythonโ€™s queue module provides a thread-safe FIFO queue. You can specify a maxsize.
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()
๐ŸŒ
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 ...
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python queue: fifo, lifo example
Python Queue: FIFO, LIFO Example
August 12, 2024 - import queue q1 = queue.LifoQueue() for i in range(20): q1.put(i) # this will additem from 0 to 20 to the queue while not q1.empty(): print("The value is ", q1.get()) # get() will remove the item from the queue.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dsa_queues.asp
Queues with Python
Extra memory: Each queue element must contain the address to the next element (the next linked list node). Readability: The code might be harder to read and write for some because it is longer and more complex. ... 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 Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
Troy Fawkes
troyfawkes.com โ€บ home โ€บ blog โ€บ the basics of python multithreading and queues
The Basics of Python Multithreading and Queues - Troy Fawkes
May 13, 2024 - So hereโ€™s something for myself next time I need a refresher. Itโ€™s the bare-bones concepts of Queuing and Threading in Python. Before you do anything else, import Queue. ... my_list = [] my_list.append(1) my_list.append(2) my_list.append(3) print my_list.pop(0) # Outputs: 1
๐ŸŒ
BitDegree
bitdegree.org โ€บ learn โ€บ python-queue
The Four Types of Python Queue: Definitions and Examples
February 19, 2020 - Home| Python | Queue ยท Reading time 2 min ยท Published Feb 19, 2020 ยท Updated Feb 19, 2020 ยท TL;DR โ€“ A Python queue is a linear data structure, similar to a stack. 1. How to use a queue in Python ยท 2. Python queue examples: four types ยท 3. Python queue: useful tips ยท
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 35442782 โ€บ python-2-7-threading-queue-reuse-threads
multithreading - Python 2.7 threading queue, reuse threads - Stack Overflow
import urllib2, time from threading import Thread from Queue import Queue # Set up global variables num_threads = 5 queue = Queue() urlList = [ "http://google.com", "http://googleeeeeee1111.com" ] def notify(i, q): print "Thread %s: started" % i url = q.get() print "Thread %s: notification sent for site: %s" % (i, url) q.task_done() def make_requests(): while True: for url in urlList: try: request = urllib2.urlopen(url) responseCode = request.getcode() # If the response code was 200, do something if responseCode == 200: print "URL: %s - Success %d" % (url, responseCode) else: print "Bad respon
๐ŸŒ
Read the Docs
stackless.readthedocs.io โ€บ en โ€บ 2.7-slp โ€บ library โ€บ queue.html
8.10. Queue โ€” A synchronized queue class โ€” Stackless-Python 2.7.15 documentation
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case). New in version 2.3: The timeout parameter.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ python_data_structure โ€บ python queue data structure
Python Data Structure - Queue
February 13, 2026 - Learn about the Python Queue data structure, its implementation, and usage in programming. Understand how to manage data efficiently with queues in Python.
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - 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 ...
๐ŸŒ
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++', 'JavaScript']) # Add a new item to the front of the deque my_deque.appendleft('Go') # Add a new item to the back of the deque my_deque.append('Ruby') # Remove a item from the front of the deque first_item = my_deque.popleft() # Remove a item from the back of the deque last_item = my_deque.pop() print(my_deque) # deque(['Java', 'C++', 'JavaScript']) print(first_item) # 'Go' print(last_item) # 'Ruby'
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ stack-queue-python-using-module-queue
Stack and Queue in Python using queue Module - GeeksforGeeks
August 1, 2022 - This Queue follows FIFO rule. This module also has a LIFO Queue, which is basically a Stack. Data is inserted into Queue using put() and the end. get() takes data out from the front of the Queue.
๐ŸŒ
Python Central
pythoncentral.io โ€บ use-queue-beginners-guide
How to use Queue: A beginner's guide | Python Central
August 22, 2017 - class Queue: #Constructor def __init__(self): self.queue = list() self.maxSize = 8 self.head = 0 self.tail = 0 #Adding elements def enqueue(self,data): #Checking if the queue is full if self.size() >= self.maxSize: return ("Queue Full") self.queue.append(data) self.tail += 1 return True #Deleting elements def dequeue(self): #Checking if the queue is empty if self.size() <= 0: self.resetQueue() return ("Queue Empty") data = self.queue[self.head] self.head+=1 return data #Calculate size def size(self): return self.tail - self.head #Reset queue def resetQueue(self): self.tail = 0 self.head = 0 se
๐ŸŒ
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.