Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:32:04+00:00 2026-06-15T10:32:04+00:00

I was reading a article on Python multi threading using Queues and have a

  • 0

I was reading a article on Python multi threading using Queues and have a basic question.

Based on the print stmt, 5 threads are started as expected. So, how does the queue works?

1.The thread is started initially and when the queue is populated with a item does it gets restarted and starts processing that item?
2.If we use the queue system and threads process each item by item in the queue, how there is a improvement in performance..Is it not similar to serial processing ie; 1 by 1.

import Queue
import threading
import urllib2
import datetime
import time

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]

queue = Queue.Queue()

class ThreadUrl(threading.Thread):

  def __init__(self, queue):
    threading.Thread.__init__(self)
    print 'threads are created'
    self.queue = queue

  def run(self):
    while True:
      #grabs host from queue
      print 'thread startting to run'
      now = datetime.datetime.now()

      host = self.queue.get()

      #grabs urls of hosts and prints first 1024 bytes of page
      url = urllib2.urlopen(host)
      print 'host=%s ,threadname=%s' % (host,self.getName())
      print url.read(20)

      #signals to queue job is done
      self.queue.task_done()

start = time.time()
if __name__ == '__main__':

  #spawn a pool of threads, and pass them queue instance 
    print 'program start'
    for i in range(5):

        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

 #populate queue with data   
    for host in hosts:
        queue.put(host)

 #wait on the queue until everything has been processed     
    queue.join()


    print "Elapsed Time: %s" % (time.time() - start)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:32:05+00:00Added an answer on June 15, 2026 at 10:32 am

    A queue is similar to a list container, but with internal locking to make it a thread-safe way to communicate data.

    What happens when you start all of your threads is that they all block on the self.queue.get() call, waiting to pull an item from the queue. When an item is put into the queue from your main thread, one of the threads will become unblocked and receive the item. It can then continue to process it until it finishes and returns to a blocking state.

    All of your threads can run concurrently because they all are able to receive items from the queue. This is where you would see your improvement in performance. If the urlopen and read take time in one thread and it is waiting on IO, that means another thread can do work. The queue objects job is simply to manage the locking access, and popping off items to the callers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
After reading this article: Using Interfaces in C++ I have decided to use the
After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
I was reading this article by Brian Goetz. Under the section Don't start threads
I am reading an article in IEEE Computer magazine about using data mining on
I am reading a file (disk images and /dev/sda and likes) in python using
After reading this article , I'm thinking it's not possible. However, I have an
I have been reading this article to understand the gcroot template. I understand the
After reading this article http://camendesign.com/code/developpeurs_sans_frontieres I have decided to follow what it says and
I'm working with a sqlite database, using python/django. I would need to have my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.