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'

  • 相关阅读:
    Linux机器学习软件配置
    安装linux14.04
    Navicat无法连接SqlServer数据库
    linux命令行安装teamviewer
    Ubuntu14.04+Dell 7060安装无线/有线网络驱动
    启动一个SpringBoot的maven项目
    HTML5新增特性
    HTML 表格|表单
    HTML 基础
    初识 wijmo-grid
  • 原文地址:https://www.cnblogs.com/i80386/p/3705114.html
Copyright © 2011-2022 走看看