分享

python queue模块 消息队列

 java_laq小馆 2014-01-07

python queue模块 消息队列

#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#神龙 QQ29295842
#blog http://hi.baidu.com/alalmn
import Queue
myqueue = Queue.Queue(maxsize =0)   #队列大小
import threading
import urllib2
import time
hosts = ["http://1","http://2","http://3","http://4","http://5"]
#hosts = ["http://yahoo.com","http://","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://www.oschina.net/code/snippet_782578_21873





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多