python queue模块 消息队列,pythonqueue,#!/usr/local


#!/usr/local/bin/python#-*- coding: UTF-8 -*-#神龙 QQ29295842#blog http://hi.baidu.com/alalmnimport Queuemyqueue = Queue.Queue(maxsize =0)   #队列大小import threadingimport urllib2import timehosts = ["http://1","http://2","http://3","http://4","http://5"]#hosts = ["http://yahoo.com","http://google.com.hk","http://amazon.com","http://ibm.com","http://apple.com"]queue = Queue.Queue()class ThreadUrl(threading.Thread):    """Threaded Url Grab"""    def __init__(self, queue,htint):        threading.Thread.__init__(self)        self.queue = queue        self.Ht=htint  #线程ID    def run(self):        while True:        #grabs host from queue            host = self.queue.get()  #get()方法从队头删除并返回一个项目            print u"线程ID %d---%s"%(self.Ht,host)            print self.queue.qsize() #返回队列的大小,近似值            if self.queue.empty():  #如果队列为空                print u"队列为空"            #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(12):        t = ThreadUrl(queue,i)        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)#该片段来自于http://byrx.net

评论关闭