gevent協程 和多線程,以及單線程的速度差別

運行環境一致的情況下,協程,線程,和單線程的運行速度差別。
協程:
import gevent
import requests
from guanjianzi import keylist as keys
from gevent import monkey
monkey.patch_all()  #猴子補丁,改變python的庫,改成非阻塞的
import re
import time
s=requests.Session()
def getlist(url):
    html=s.get(url).content.decode()
    res=r'<li  class="li"><font class="date">(.*?)</font><a href="(.*?)" target="_blank">(.*?)</a><span class="new"></span></li>'
    li=re.findall(res,html)
    #jix(li)
    sps=[]
    for x,y,z in li:
        y=url+str(y)
        y=re.sub(r"\/index\.html\.",'',y)
        sps.append(gevent.spawn(jix,x,y,z))
    gevent.joinall(sps)

def jix(x,y,z):
        html1=s.get(y).content.decode()
        text=str(html1)
        guanjianzi = {}
        num = 0
        for key in keys:  # 循環遍歷關鍵字列表,查詢關鍵字出現的次數
            count = text.count(key)  # count 關鍵字在本文中出現的次數
            if count > 0:
                guanjianzi.update({key: count})  # 把關鍵字和出現的次數添加到字典中
                num += count
        print(y,z,guanjianzi,num,x)

if __name__ == '__main__':
    urls = ['http://www.ndrc.gov.cn/xwzx/xwfb/index.html','http://www.ndrc.gov.cn/zwfwzx/zxgg/index.htmm','http://www.ndrc.gov.cn/zwfwzx/xzxknew/index.html','http://www.ndrc.gov.cn/zcfb/zcfbl/index.html','http://www.ndrc.gov.cn/zcfb/gfxwj/index.html','http://www.ndrc.gov.cn/zcfb/zcfbgg/index.html','http://www.ndrc.gov.cn/zcfb/zcfbghwb/index.html','http://www.ndrc.gov.cn/zcfb/zcfbtz/index.html','http://www.ndrc.gov.cn/zcfb/jd/index.html','http://www.ndrc.gov.cn/yjzq/index.html']
    li=[]
    start = time.time()
    for url in urls:
        li.append(gevent.spawn(getlist,url))
    gevent.joinall(li)
    end=time.time()
    print('運行時間',end-start)




下邊是使用線程池,開了五個線程
import requests
from guanjianzi import keylist as keys
import re
from conMySql import ConDb
from multiprocessing.dummy import Pool as ThreadPool
import time
start = time.time()
s=requests.Session()
con=ConDb()
def getlist(url):
    html=s.get(url).content.decode()
    res=r'<li  class="li"><font class="date">(.*?)</font><a href="(.*?)" target="_blank">(.*?)</a><span class="new"></span></li>'
    li=re.findall(res,html)
    title=re.findall(r"<title>(.*?)</title>",html)[0]
    for x,y,z in li:
        y=url+str(y)
        y=re.sub(r"\/index\.html\.",'',y)
        html1=s.get(y).content.decode()
        text=str(html1)
        guanjianzi = {}
        num = 0
        for key in keys:  # 循環遍歷關鍵字列表,查詢關鍵字出現的次數
            count = text.count(key)  # count 關鍵字在本文中出現的次數
            if count > 0:
                guanjianzi.update({key: count})  # 把關鍵字和出現的次數添加到字典中
                num += count
        print(title,y,z,guanjianzi,num,x)
        #sql='''  insert into urllist(source,urls,titles,keyname,keysum,date1) values('{}','{}','{}',"{}",'{}','{}') '''.format(title,y,z,guanjianzi,num,x)
        #con.runSql(sql)
if __name__ == '__main__':
    urls = ['http://www.ndrc.gov.cn/xwzx/xwfb/index.html','http://www.ndrc.gov.cn/zwfwzx/zxgg/index.htmm','http://www.ndrc.gov.cn/zwfwzx/xzxknew/index.html','http://www.ndrc.gov.cn/zcfb/zcfbl/index.html','http://www.ndrc.gov.cn/zcfb/gfxwj/index.html','http://www.ndrc.gov.cn/zcfb/zcfbgg/index.html','http://www.ndrc.gov.cn/zcfb/zcfbghwb/index.html','http://www.ndrc.gov.cn/zcfb/zcfbtz/index.html','http://www.ndrc.gov.cn/zcfb/jd/index.html','http://www.ndrc.gov.cn/yjzq/index.html']
    t=ThreadPool(5)

    for url in urls:
        t.apply_async(getlist,args=(url,))
    t.close()
    t.join()
    end=time.time()
    print('',end-start)
    #sql1='select max(bat) from urllist limit 1'
    #bat=con.runSql(sql1)[0][0]
    #bat=int(bat)+1
    # print(bat)
    #sql2="update urllist set bat='{}'".format(bat)
    #con.runSql(sql2)

運行時間如下:


多線程分配任務給線程的方式和協程一樣的速度,但是協程進行二次封裝之後速度比線程快了一倍

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章