美文网首页
爬虫学习(四)多线程

爬虫学习(四)多线程

作者: 拼了老命在学习 | 来源:发表于2020-07-15 22:42 被阅读0次

    1.多线程

    简单说就是在一个任务进程中,采用多个线程来分别完成子任务,从而提高运行速度及效率,需要用到的模块为threading模块。
    实例:

    import threading
    import time
    
    def coding():
        for x in range(1,4):
            print('正在写%s' %x)
            time.sleep(1)
    
    def drawing():
        for x in  range(1,4):
            print("正在画%s" %x)
            time.sleep(1)
    
    def main():
        t1 = threading.Thread(target=coding)
        t2 = threading.Thread(target=drawing)
        t1.start()
        t2.start()
    
    if __name__ == '__main__':
        main()
    

    常用方法

    threading.enumerate() 查看线程数
    threading.current_thread() 查看线程的名字
    

    继承自threading.Thread类
    实例:

    import threading
    import time
    
    class coding(threading.Thread):
        def run(self):
            for x in range(1,4):
                print('正在写%s' %x)
                time.sleep(1)
    
    class drawing(threading.Thread):
        def run(self):
            for x in  range(1,4):
                print("正在画%s" %x)
                time.sleep(1)
    
    def main():
        t1 = coding()
        t2 = drawing()
        t1.start()
        t2.start()
    
    if __name__ == '__main__':
        main()
    

    2.多线程全局变量冲突的解决

    import threading
    value = 0
    glock = threading.Lock()
    def add_value():
        global value
        glock.acquire() #上锁,其余线程等待中
        for x in range(1000000):
            value += 1
        glock.release()#释放
        print(value)
    def main():
        for x in range(2):
            t = threading.Thread(target=add_value)
            t.start()
    if __name__ == '__main__':
        main()
    

    lock版生产者与消费者

    import threading
    import time
    import random
    
    gmoney = 1000
    glock = threading.Lock()
    totaltimes = 10
    times = 0
    
    class producer(threading.Thread):
        def run(self):
            global gmoney,totaltimes,times
            while True:
                money = random.randint(100,1000)
                glock.acquire()
                if times>=totaltimes:
                    glock.release()
                    break
                gmoney = gmoney+money
                print('%s生产者生产了%d元,剩余%d元' %(threading.current_thread(),money,gmoney))
                times = times+1
                glock.release()
                time.sleep(0.5)
    class customer(threading.Thread):
        def run(self):
            global gmoney
            while True:
                money = random.randint(100,1000)
                glock.acquire()
                if gmoney>=money:
                    gmoney -= money
                    print("%s消费者消费了%d元,还剩%d元" %(threading.current_thread(),money,gmoney))
                else:
                    if times>=totaltimes:
                        glock.release()
                        break
                    print("%s消费者准备消费%d元,还剩%d元,不足" %(threading.current_thread(),money,gmoney))
                glock.release()
                time.sleep(0.5)
    
    def main():
        for x in range(3):
            t = customer(name="消费者线程%d" %x)
            t.start()
        for x in range(5):
            t = producer(name='生产者线程%d' %x)
            t.start()
    
    if __name__ == '__main__':
        main()
    

    condition版生产者与消费者(节省CPU资源)

    import threading
    import time
    import random
    
    gmoney = 1000
    gcondition = threading.Condition()
    totaltimes = 10
    times = 0
    
    class producer(threading.Thread):
        def run(self):
            global gmoney,totaltimes,times
            while True:
                money = random.randint(100,1000)
                gcondition.acquire()
                if times>=totaltimes:
                    gcondition.release()
                    break
                gmoney = gmoney+money
                print('%s生产者生产了%d元,剩余%d元' %(threading.current_thread(),money,gmoney))
                times = times+1
                gcondition.notify_all() #唤醒所有等待的线程
                gcondition.release()
                time.sleep(0.5)
    
    class customer(threading.Thread):
        def run(self):
            global gmoney
            while True:
                money = random.randint(100,1000)
                gcondition.acquire()
                while gmoney<money: #判断钱是否足够
                    if times>=totaltimes: #判断生产者是否生产完
                        gcondition.release()
                        return
                    print("%s消费者准备消费%d元,还剩%d元,不足" % (threading.current_thread(), money, gmoney))
                    gcondition.wait() #钱不足时将线程处于等待状态,生产后唤醒
                gmoney = gmoney-money
                print("%s消费者消费了%d元,还剩%d元" % (threading.current_thread(), money, gmoney))
                gcondition.release()
                time.sleep(0.5)
    
    def main():
        for x in range(3):
            t = customer(name="消费者线程%d" %x)
            t.start()
        for x in range(5):
            t = producer(name='生产者线程%d' %x)
            t.start()
    
    if __name__ == '__main__':
        main()
    

    3.Queue线程安全队列

    先进先出队列(Queue),后进先出队列(LifoQueue)
    常用操作

    from queue import Queue
    #创建一个先进先出队列
    Queue(maxsize)
    #判断队列是否为空
    Queue.empty()
    #判断队列是否满了
    Queue.full()
    #获取队列最后一个数据,即最先进入队列的数据
    Queue.get()
    #将一个数据放到队列中
    Queue.Put()
    

    实例:Queue多线程爬取斗图吧表情

    import requests
    from lxml import etree
    from urllib import request
    from queue import Queue
    import os
    import re
    import threading
    
    class Producer(threading.Thread):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
        }
        def __init__(self,page_queue,img_queue,*args,**kwargs): #初始化父类属性并新建属性page_queue,img_queue
            super(Producer,self).__init__(*args,**kwargs) #使producer包含父类所有属性
            self.page_queue = page_queue
            self.img_queue = img_queue
        def run(self):
            while True:
                if self.page_queue.empty():
                    break
                url = self.page_queue.get()
                self.page_parse(url)
        def page_parse(self,url):
            proxy = {
                'http':'113.195.18.53:9999',
                'http':'114.99.23.137:1133',
                'http':'163.204.244.247:9999',
                'http':'123.207.57.145:1080'
    
            }
            resp = requests.get(url,headers=self.headers,proxies=proxy)
            text = resp.text
            html = etree.HTML(text)
            imgs = html.xpath("//div[@class='page-content text-center']//img[@class!='gif']") #过滤GIF类型
            for img in imgs:
                img_url = img.get("data-original")
                alt = img.get("alt") #获取图片名
                alt = re.sub(r'[\??\.,,。!!“”\*]','',alt) #去除文字中的特殊符号
                houzhui = os.path.splitext(img_url)[1]#获取后缀
                filename = alt + houzhui #获取完整文件名
                self.img_queue.put((img_url,filename)) #添加到下载队列
    
    class Customer(threading.Thread):
        def __init__(self,page_queue,img_queue,*args,**kwargs):
            super(Customer,self).__init__(*args,**kwargs)
            self.page_queue = page_queue
            self.img_queue = img_queue
        def run(self):
            while True:
                if self.img_queue.empty() and self.page_queue.empty():
                    break
                img_url,filename = self.img_queue.get()
                request.urlretrieve(img_url,'imgs/'+filename) #下载图片
                print(filename+"打印成功")
    
    def main():
        page_queue = Queue(100)
        img_queue = Queue(100)
        base_url = 'https://www.doutula.com/photo/list/?page={}'
        for x in range(1,3):
            url = base_url.format(x)
            page_queue.put(url) #添加到解析队列
        for x in range(5): #创建5个生产者,即5个线程
            t = Producer(page_queue,img_queue)
            t.start()
        for x in range(5):
            t = Customer(page_queue,img_queue)
            t.start()
    
    if __name__ == '__main__':
        main()
    

    相关文章

      网友评论

          本文标题:爬虫学习(四)多线程

          本文链接:https://www.haomeiwen.com/subject/sduxhktx.html