Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
February 23, 2026 - 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
import logging import queue import threading import time logging.basicConfig(level=logging.INFO, format="%(threadName)s %(message)s") def worker(tasks): while True: task = tasks.get() if task is None: tasks.task_done() break logging.info(f"processing {task}") time.sleep(1) tasks.task_done() tasks = queue.Queue() for task in ["task1", "task2", "task3", "task4", "task5"]: tasks.put(task) num_workers = 2 for _ in range(num_workers): tasks.put(None) threads = [ threading.Thread(target=worker, args=(tasks,)) for _ in range(num_workers) ] for thread in threads: thread.start() tasks.join() for thread in threads: thread.join() # Output: # Thread-1 (worker) processing task1 # Thread-2 (worker) processing task2 # Thread-1 (worker) processing task3 # Thread-2 (worker) processing task4 # Thread-1 (worker) processing task5
Videos
02:50
Python QUEUEs | Queue implementation example - YouTube
18:47
Queues in Python Explained [ Step-by-Step Guide to Data Structures ...
16:19
Queue - Data Structures in Python #3 - YouTube
26:12
Working with Queues in Python - An introductory guide - YouTube
Python QUEUEs | Queue implementation example
Python Queue
W3Schools
w3schools.com › python › ref_module_queue.asp
Python queue Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training · ❮ Standard Library Modules · Create a FIFO queue, put an item, then get it back: import queue q = queue.Queue() q.put('task1') print(q.get()) Try it Yourself » ·
W3Schools
w3schools.com › python › python_dsa_queues.asp
Queues with Python
Queues are often mentioned together with Stacks, which is a similar data structure described on the previous page. For Python lists (and arrays), a Queue can look and behave like this:
Real Python
realpython.com › queue-in-python
Python Stacks, Queues, and Priority Queues in Practice – Real Python
December 1, 2023 - To get the most out of this tutorial, you should be familiar with Python’s sequence types, such as lists and tuples, and the higher-level collections in the standard library. You can download the complete source code for this tutorial with the associated sample data by clicking the link in the box below: Get Source Code: Click here to get access to the source code and sample data that you’ll use to explore queues in Python.
O'Reilly
oreilly.com › library › view › python-standard-library › 0596000960 › ch03s03.html
The Queue Module - Python Standard Library [Book]
May 10, 2001 - File: queue-example-1.py import threading import Queue import time, random WORKERS = 2 class Worker(threading.Thread): def _ _init_ _(self, queue): self._ _queue = queue threading.Thread._ _init_ _(self) def run(self): while 1: item = self._ _queue.get() if item is None: break # reached end of queue # pretend we're doing something that takes 10—100 ms time.sleep(random.randint(10, 100) / 1000.0) print "task", item, "finished" # # try it queue = Queue.Queue(0) for i in range(WORKERS): Worker(queue).start() # start a worker for i in range(10): queue.put(i) for i in range(WORKERS): queue.put(None) # add end-of-queue markers task 1 finished task 0 finished task 3 finished task 2 finished task 4 finished task 5 finished task 7 finished task 6 finished task 9 finished task 8 finished
Author Fredrik Lundh
Published 2001
Pages 304
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
CodeLens 1 shows the Queue class in action as we perform the sequence of operations from Table 1. Activity: CodeLens Example Queue Operations (ququeuetest)
RQ
python-rq.org
RQ: Simple job queues for Python
from rq.repeat import Repeat # Repeat job 3 times after successful completion, with 60 second intervals job = queue.enqueue(say_hello, repeat=Repeat(times=3, interval=60)) # Use different intervals between repetitions job = queue.enqueue(say_hello, repeat=Repeat(times=3, interval=[10, 30, 60]))
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
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()
GitHub
github.com › scrapy › queuelib
GitHub - scrapy/queuelib: Collection of persistent (disk-based) and non-persistent (memory-based) queues for Python
Collection of persistent (disk-based) and non-persistent (memory-based) queues for Python - scrapy/queuelib
Starred by 289 users
Forked by 55 users
Languages Python 100.0% | Python 100.0%
GitHub
github.com › rq › rq
GitHub - rq/rq: Simple job queues for Python · GitHub
from rq import cron from myapp import send_newsletter, backup_database # Database backup every day at 3:00 AM cron.register( backup_database, queue_name='maintenance', cron_string='0 3 * * *' ) # Monthly report on the first day of each month at 8:00 AM cron.register( generate_monthly_report, queue_name='reports', cron_string='0 8 1 * *' ) ```python More details on functionality can be found in the [docs](https://python-rq.org/docs/cron/).
Starred by 10.6K users
Forked by 1.5K users
Languages Python