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 7024373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:49:53+00:00 2026-05-27T23:49:53+00:00

I read up about threading in the IBM developer sources and found the following

  • 0

I read up about threading in the IBM developer sources and found the following example.

In general I understand what happens here, except for one important thing. The work seems to be done in the run() function. In this example run() only prints a line and signals to the queue, that the job is done.

What if I had to return some processed data? I thought about caching it in a global variable, and to access this one later, but this seems not the right way to go.

Any advice?

Perhaps I should clearify: My intuition tells me to add return processed_data to run() right after self.queue.task_done(), but I can’t figure out where to catch that return, since it is not obvious to me where run() is called.

#!/usr/bin/env python
import Queue
import threading
import urllib2
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):
    """Threaded Url Grab"""
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()

            #grabs urls of hosts and prints first 1024 bytes of page
            url = urllib2.urlopen(host)
            print url.read(1024)

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

start = time.time()
def main():

    #spawn a pool of threads, and pass them queue instance 
    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()

main()
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-05-27T23:49:54+00:00Added an answer on May 27, 2026 at 11:49 pm

    You can’t return a value from run, and in any case there is normally more than one item to process in each thread, so you don’t want to return at all after processing one value (see the while loop in each thread).

    I would either use another queue to return the results:

    queue = Queue.Queue()
    out_queue = Queue.Queue()
    
    class ThreadUrl(threading.Thread):
        ...
        def run(self):
            while True:
                #grabs host from queue
                host = self.queue.get()
    
                #grabs urls of hosts and saves first 1024 bytes of page
                url = urllib2.urlopen(host)
                out_queue.put(url.read(1024))
    
                #signals to queue job is done
                self.queue.task_done()
    
    ...
    
    def main():
        ...
        #populate queue with data   
        for host in hosts:
            queue.put(host)
    
        #don't have to wait until everything has been processed if we don't want to
    
        for _ in range(len(hosts)):
            first_1k = out_queue.get()
            print first_1k
    

    or store the result in the same queue:

    class WorkItem(object):
        def __init__(self, host):
            self.host = host
    
    class ThreadUrl(threading.Thread):
        ...
        def run(self):
            while True:
                #grabs host from queue
                work_item = self.queue.get()
                host = work_item.host
    
                #grabs urls of hosts and saves first 1024 bytes of page
                url = urllib2.urlopen(host)
                work_item.first_1k = url.read(1024)
    
                #signals to queue job is done
                self.queue.task_done()
    
    ...
    
    def main():
        ...
        #populate queue with data   
        work_items = [WorkItem(host) for host in hosts]
        for item in work_items:
            queue.put(item)
    
        #wait on the queue until everything has been processed     
        queue.join()
    
        for item in work_items:
            print item.first_1k
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read about Server push here . I want to push data to client from
I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm
I read about Big-O Notation from here and had few questions on calculating the
I am only somewhat familiar with multi-threading in that I've read about it but
I've read about Preemptive Multithreading here and here . Is there a way to
I read about the Dispatcher class in .NET. But curiously the System.Windows.Threading namespace does
I read about the Conditional attribute today. According to MSDN: Applying ConditionalAttribute to a
Ive read about it and to be honest it all seems like a bunch
I read about small talk being completely object oriented.. is C++ also completely object
I read about using <context:component-scan base-package=tld.mydomain.business> <context:include-filter type=annotation expression=org.springframework.stereotype.Service/> </context:component-scan> and annotate my service

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.