zoukankan      html  css  js  c++  java
  • python多线程 批量下补丁

    一个一个下载 要2个多小时。就直接起了个线程池。效果明显。
    import urllib2
    from urlparse import urlparse uri = 'http://******/patch****' d = urllib2.urlopen(uri) res = urlparse(uri) f = open('c:/' + res.path, 'wb') f.write(d.read()) f.close() print 'over'



    import urllib2
    from urlparse import urlparse
    import Queue
    import threading


    mf = open('rest.txt', 'r')
    urls = []
    while 1:
        line = mf.readline()
        if not line:
            break
        urls.append(line.replace(' ', ''))
    mf.close()

    queue = Queue.Queue()


    class ThreadUrl(threading.Thread):
        def __init__(self, queue, root):
            threading.Thread.__init__(self)
            self.queue = queue
            self.root = root
        
        def run(self):
            while True:
                uri = self.queue.get()
                print uri
                d = urllib2.urlopen(uri)
                res = urlparse(uri)
                f = open(self.root + res.path, 'wb')
                f.write(d.read())
                f.close()
                self.queue.task_done()
                print 'task done'
    rootdir = 'C:/pathes/'
    for i in range(4):
        t = ThreadUrl(queue, rootdir)
        t.setDaemon(True)
        t.start()

    for uri in urls:
        queue.put(uri)

    queue.join()
    print 'over'

  • 相关阅读:
    wget(转)
    852. Peak Index in a Mountain Array
    617. Merge Two Binary Trees
    814. Binary Tree Pruning
    657. Judge Route Circle
    861. Score After Flipping Matrix
    832. Flipping an Image
    461. Hamming Distance
    654. Maximum Binary Tree
    804. Unique Morse Code Words
  • 原文地址:https://www.cnblogs.com/i80386/p/3705114.html
Copyright © 2011-2022 走看看