使用线程和队列进行Python重构

时间:2017-09-26 13:49:52

标签: python multithreading

我正在尝试重构我的代码,第一个版本是here

我想要的是同时运行两个对象

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()

当我从终端运行代码时没有输出。我应该更改什么?

1 个答案:

答案 0 :(得分:2)

取消run方法,使它们不在__init__方法中。

但是请注意,你几乎肯定不希望那些永远循环;删除while True