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 OverflowPython
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, ...
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
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
Huey task queue 2.0
Big fan of Huey, thanks for all the work! More on reddit.com
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
Videos
#13. ะะปะฐัั Queue ะผะพะดัะปั queue | The Python Standard Library
18:47
Queues in Python Explained [ Step-by-Step Guide to Data Structures ...
26:12
Working with Queues in Python - An introductory guide - YouTube
15:23
Implement Queue using Stacks - Leetcode 232 - Python - YouTube
06:02
#15. ะะตะปะฐะตะผ ะพัะตัะตะดั (queue) | ะะปะณะพัะธัะผั ะฝะฐ ...
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()
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
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.
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'
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