多线程实例:

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





原文地址:https://www.cnblogs.com/tangbinghaochi/p/6293038.html