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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:10:16+00:00 2026-05-23T04:10:16+00:00

I am new to both python, and to threads. I have written python code

  • 0

I am new to both python, and to threads. I have written python code which acts as a web crawler and searches sites for a specific keyword. My question is, how can I use threads to run three different instances of my class at the same time. When one of the instances finds the keyword, all three must close and stop crawling the web. Here is some code.

class Crawler:
      def __init__(self):
            # the actual code for finding the keyword 

 def main():  
        Crawl = Crawler()

 if __name__ == "__main__":
        main()

How can I use threads to have Crawler do three different crawls at the same time?

  • 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-23T04:10:17+00:00Added an answer on May 23, 2026 at 4:10 am

    There doesn’t seem to be a (simple) way to terminate a thread in Python.

    Here is a simple example of running multiple HTTP requests in parallel:

    import threading
    
    def crawl():
        import urllib2
        data = urllib2.urlopen("http://www.google.com/").read()
    
        print "Read google.com"
    
    threads = []
    
    for n in range(10):
        thread = threading.Thread(target=crawl)
        thread.start()
    
        threads.append(thread)
    
    # to wait until all three functions are finished
    
    print "Waiting..."
    
    for thread in threads:
        thread.join()
    
    print "Complete."
    

    With additional overhead, you can use a multi-process aproach that’s more powerful and allows you to terminate thread-like processes.

    I’ve extended the example to use that. I hope this will be helpful to you:

    import multiprocessing
    
    def crawl(result_queue):
        import urllib2
        data = urllib2.urlopen("http://news.ycombinator.com/").read()
    
        print "Requested..."
    
        if "result found (for example)":
            result_queue.put("result!")
    
        print "Read site."
    
    processs = []
    result_queue = multiprocessing.Queue()
    
    for n in range(4): # start 4 processes crawling for the result
        process = multiprocessing.Process(target=crawl, args=[result_queue])
        process.start()
        processs.append(process)
    
    print "Waiting for result..."
    
    result = result_queue.get() # waits until any of the proccess have `.put()` a result
    
    for process in processs: # then kill them all off
        process.terminate()
    
    print "Got result:", result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to both Web Services and RMI and I wonder which is the
I'm kinda' new to python, but I have already written many programs including some
New to both Ruby and Rails but I'm book educated by now (which apparently
At a new job I started, we have both a Java application that handles
We have a large enterprise consisting of many apps both old and new backed
I am new to both django and python but I am beginning to get
I'm fairly new to both Django and Python. This is my first time using
In both Python and Java we have import to eliminate the repetition of fully-qualified
Ok say I have a string in python: str=martin added 1 new photo to
I have a new web app that is packaged as a WAR as part

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.