python---aiohttp的使用 python asyncio 獲取協程返回值和使用callback 500併發測試

aiohttp教程

https://www.cnblogs.com/ssyfj/p/9222342.html#14.clientsession-用於在多個連接之間同一網站共享cookie,請求頭等

參考教程

https://www.jianshu.com/p/0efdc952e8ca

https://www.cnblogs.com/callyblog/p/11216961.html

import asyncio

from aiohttp import web

async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b'<h1>Index</h1>')

async def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>hello, %s!</h1>' % request.match_info['name']
    return web.Response(body=text.encode('utf-8'))

async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/hello/{name}', hello)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000)
    print('Server started at http://127.0.0.1:8000...')
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

500併發測試 

import asyncio
import aiohttp
import time
url_lst_failed=[]
url_lst_successed=[]
async def get_info(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url,timeout=5) as resp:
            if resp.status != 200:
                url_lst_failed.append(url)
            else:
                url_lst_successed.append(url)
            r = await resp.text()

start = time.time()
#創建一個循環
loop = asyncio.get_event_loop()
#創建一個任務盒子tasks,包含了3個需要完成的任務

tasks =[get_info('http://39.108.128.123:80/v1/select_user') for i in range(500)]
#tasks接入loop中開始運行

loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end-start)
print(len(url_lst_successed))
import asyncio

import aiohttp
# pip install readability-lxml以安裝
from readability import Document


def title_summary(fut):
    res = fut.result()  # 回調中調用result()纔是上個函數的真實返回值
    if res:
        content, url = res
        doc = Document(content, url)
        print(doc.short_title(), doc.summary())


async def read_one(id_: int, url: str):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
    async with aiohttp.ClientSession() as session:
        try:
            async with session.get(
                    url, headers=headers, timeout=1, verify_ssl=False) as r:
                await asyncio.sleep(1 + random())
                return await r.read(), await r.text(encoding=None, errors='ignore')
        except:
            pass


def read_many(links: list):
    loop = asyncio.get_event_loop()
    for id_, url in links:
        task = asyncio.ensure_future(read_one(id_, url))
        # 注意參數問題,這裏不能傳遞多個參數,要麼用functool的partial,要麼乾脆傳遞元組解包,也可以用lambda,官方比較推薦functool這裏就不寫了
        task.add_done_callback(title_summary)
        loop.run_until_complete(task)
    loop.close()


def main():
    links = [...]  # 要跑的所有鏈接列表
    read_many(links)


if __name__ == '__main__':
    main()

 

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