zoukankan      html  css  js  c++  java
  • 多线程实例:

     
    # -*- coding: utf-8 -*-
    import requests
    import urllib
    import os
    import threading
    import datetime
     
     
    gImageList = []
    gCondition = threading.Condition()
     
     
    class Producer(threading.Thread):
        def run(self):
            global gImageList
            global gCondition
     
            print('%s started' % threading.current_thread())
            imgs = download_wallpaper_list()
     
            gCondition.acquire()
            for i in imgs:
                if 'downloadUrl' in i:
                    gImageList.append(i['downloadUrl'])
            print('%s: produced finished. Left: %s' % (threading.current_thread(),  len(gImageList)))
            gCondition.notify_all()
            gCondition.release()
     
            
     
    class Consumer(threading.Thread):
        def run(self):   
            print('%s started' % threading.current_thread())
            while True:
                global gImageList
                global gCondition
                
                gCondition.acquire()
                print('%s: trying to download image. Queue length is %d' % (threading.current_thread(), len(gImageList)))
                while len(gImageList) == 0:
                    gCondition.wait()
                    print('%s: waken up. Queue length is %d' % (threading.current_thread(), len(gImageList)))
                url = gImageList.pop()
                gCondition.release()
                _download_image(url)
     
     
    def _download_image(url, folder='image'):
        if not os.path.isdir(folder):
            os.mkdir(folder)
        print('downloading %s' %  url)
        def _fname(s):
             return os.path.join(folder, os.path.split(url)[1])
     
        urllib.urlretrieve(url, _fname(url))
     
    def download_wallpaper_list():
        url = 'http://image.baidu.com/data/imgs'
        params = {
            'pn': 1,
            'rn': 20,
            'col': '壁纸',
            'tag': '国家地理',
            'tag3': '',
            'width': 1600,
            'height': 900,
            'ic': 0,
            'ie': 'utf8',
            'oe': 'utf-8',
            'image_id': '',
            'fr': 'channel',
            'p': 'channel',
            'from': 1,
            'app': 'img.browse.channel.wallpaper',
            't': '0.016929891658946872'
        }
        r = requests.get(url, params=params)
        imgs = r.json()['imgs']
        print('%s: totally %d images' % (threading.current_thread(), len(imgs)))
        return imgs
     
     
    if __name__ == '__main__':
        Producer().start()
     
        for i in range(1):                       ##改变这个1就能改变线程数
            Consumer().start()
     





  • 相关阅读:
    数据库信息 (表名 行数 堆 集群 非聚集)的查询
    jquerygalleryview2.0 漂亮多样化的图片特效(多项自定义)
    枚举 EnumDescription 位运算 权限 sql c#
    vs2010缓存类
    透明遮罩层
    app_offline.htm的作用
    XmlToJSON(c#)
    jquery性能最佳实践
    .net面试问答(大汇总)
    apk反编译
  • 原文地址:https://www.cnblogs.com/tangbinghaochi/p/6293038.html
Copyright © 2011-2022 走看看