0

I was trying to restructure my code,first version is here

What I want is to run two objects concurrently

from queue import Queue
from threading import Thread
from html.parser import HTMLParser
import urllib.request

NUMBER_OF_THREADS = 3

HOSTS = ["http://yahoo.com", "http://google.com", "http://ibm.com"]


class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Start tag:", tag)
        for attr in attrs:
            print("\tattr:", attr)


class ProducerThread(Thread):
    def __init__(self,queue):
        super(ProducerThread, self).__init__()
        self.queue = queue

        def run(self):
            while True:
                for host in HOSTS:
                    url = urllib.request.urlopen(host)
                    content = str(url.read(4096))                    
                    queue.put(content)


class ConsumerThread(Thread):
    def __init__(self,queue):
        super(ConsumerThread, self).__init__()
        self.queue = queue


        def run(self):
            while True:
                item = queue.get()
                parser = MyHTMLParser()
                new_con = parser.feed(item)
                print(new_con)
                queue.task_done()


if __name__ == '__main__':
    queue = Queue()

    p = ProducerThread(queue)
    c = ConsumerThread(queue)
    p.start()
    c.start()

When I run code from terminal there is no output.What should I change?

1 Answer 1

2

Unindent the run methods so that they are not inside the __init__ methods.

Note however you almost certainly don't want those to loop forever; remove the while True.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.