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 โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multip...
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ queue
queue | Python Standard Library โ€“ Real Python
The Python queue module provides reliable thread-safe implementations of the queue data structure.
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
Peek method in Priority Queue?
The heapq object is a list. So you can peek with standard indexing. I don't think there's a way to peek on a queue.Queue, just due to how they are implemented. More on reddit.com
๐ŸŒ r/learnpython
2
1
September 26, 2020
getting an error message in my python.

You should probably install that module. Dude. Google.

More on reddit.com
๐ŸŒ r/pokemongodev
5
0
March 7, 2018
cx_freeze no module named 'queue'
Which packages are you using? Try importing Queue from the multiprocessing class. More on reddit.com
๐ŸŒ r/learnpython
6
1
August 16, 2017
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_module_queue.asp
Python queue Module
Built-in Modules Random Module ... Python Bootcamp Python Training ... The queue module provides synchronized queue classes for multi-producer, multi-consumer scenarios....
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()
๐ŸŒ
AskPython
askpython.com โ€บ python-modules โ€บ python-queue
Python Queue Module - AskPython
February 26, 2020 - In Python, we can use the queue module to create a queue of objects.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ queue-in-python
Queue in Python - GeeksforGeeks
May 29, 2026 - Initial queue: deque(['a', 'b', ... without shifting, making deque ideal for queues. Pythonโ€™s queue module provides a thread-safe FIFO queue....
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-standard-library โ€บ 0596000960 โ€บ ch03s03.html
The Queue Module - Python Standard Library [Book]
May 10, 2001 - The Queue module provides a thread-safe queue implementation, shown in Example 3-2. It provides a convenient way of moving Python objects between different threads.
Author ย  Fredrik Lundh
Published ย  2001
Pages ย  304
Find elsewhere
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ Queue
Queue โ€“ A thread-safe FIFO implementation - Python Module of the Week
Some of the features described ... section of the site. Now available for Python 3! Buy the book! ... The Queue module provides a FIFO implementation suitable for multi-threaded programming....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ stack-queue-python-using-module-queue
Stack and Queue in Python using queue Module - GeeksforGeeks
August 1, 2022 - Initializes a variable to a maximum size of maxsize. A maxsize of zero '0' means a infinite queue. 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.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ queuelib
queuelib ยท PyPI
Queuelib provides collections for queues (FIFO), stacks (LIFO), queues sorted by priority and queues that are emptied in a round-robin fashion. ... Queuelib collections are not thread-safe. Queuelib supports Python 3.10+ and has no dependencies.
      ยป pip install queuelib
    
Published ย  Jan 29, 2026
Version ย  1.9.0
๐ŸŒ
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 - 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 built-in module called queue that implements different types of queue data structures.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-queues-in-python
Guide to Queues in Python
April 18, 2024 - The queue module in Python's standard library provides a more specialized approach to queue management, catering to various use cases:
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-queue
Python Queue Class | Usage Guide (With Examples)
February 5, 2024 - Pythonโ€™s queue module offers more than just the basic FIFO queue. It also provides other types of queues such as LifoQueue and PriorityQueue. LifoQueue, or Last-In-First-Out queue, operates as a stack where the last item added is the first one to be removed.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-queue-module
Python Queue Module - Javatpoint
Python Queue Module with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - Itโ€™s a basic priority queue ... corresponding value, which it then wraps in a tuple and pushes onto the heap using the heapq module....
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python queue
Python Queue
October 14, 2024 - Various classes come in the Queue module of Python. Out of all the classes, the Queue class is more important that helps in parallel computing and multiprogramming.
๐ŸŒ
Read the Docs
python.readthedocs.io โ€บ en โ€บ latest โ€บ library โ€บ queue.html
17.7. queue โ€” A synchronized queue class
November 15, 2017 - Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multip...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ python queue tutorial: queue module, deque & priority queue (2026)
Python Queue Tutorial: Queue Module, Deque & Priority Queue (2026)
May 7, 2026 - To implement a queue in Python, you can use the queue module. This provides two main classes โ€“ Queue and LifoQueue.