• 多线程实例:


     
    # -*- 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()
     





  • 相关阅读:
    Dev gridControl 按回车增加一行
    Web自动化----常见组件操作
    Web自动化----切换(iframe,浏览器窗口,alret)
    Web自动化----元素等待方法(显式等待和隐式等待)
    Web自动化----模拟动作(鼠标,快捷键,拖拽)
    MySQL的20条基本优化 加参考资料
    国内IT技术博客对比
    博客客户端文章测试
    QQ登录网站接入
    微信公众号本地开发调试工具
  • 原文地址:https://www.cnblogs.com/tangbinghaochi/p/6293038.html
Copyright © 2020-2023  润新知