目錄
簡單的使用
相同和同步的對比
順下載的例子
1.簡單的使用
import asyncio
async def req1():
await asyncio.sleep(1)
print('執行 req1')
return 1
async def req2():
print('執行 req2')
return 2
async def main():
list = [req1(), req2()]
res = await asyncio.gather(*list)
'''
雖然,req2是先執行完的,
但是res返回值的順序, 還是跟list順序保持一致
'''
print(res)
asyncio.get_event_loop().run_until_complete(main())
'''
結果:
執行 req2
執行 req1
[1, 2]
'''
2.相似和同時的對比
測試2個請求,同時觸發請求執行的時間短,我是延遲讓請求延遲返回
import asyncio
import time
import requests
from aiohttp import ClientSession
async def fetch(session, param):
async with session.get as resp:
data = await resp.json()
return data
async def req_async():
async with ClientSession() as session:
return await asyncio.gather(fetch(session, 2), \
fetch(session, 1))
def req():
session = requests.Session()
p1, p2 = 2, 1
return [
session.get.json(),
session.get.json()
]
async def main():
s1 = time.time()
v1 = await req_async()
s2 = time.time()
v2 = req()
print(f'異步請求時間:{round(s2-s1,1)}s \
響應結果:{[v["args"]["a"] for v in v1]}')
print(f'同步請求時間:{round(time.time()-s2,1)}s \
響應結果:{[v["args"]["a"] for v in v2]}')
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
'''
結果:
異步請求時間:3.2s 響應結果:['2', '1']
同步請求時間:7.9s 響應結果:['2', '1']
'''
3.相關下載的例子
看似請求、極大的文件限制、意外保存
async def download(sem, page: int, folder: str, session: ClientSession):
async with sem:
async with session.get(url, params={'page': page}) as resp:
async with aiofiles.open(f'{folder}/{page}.json', mode='w') as f:
await f.write(await resp.text())
return page
async def main():
# 請求url參數列表
url_pages = range(1, 5)
# 創建文件夾data
folder_name = 'data'
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# 限制并發數量
sem = asyncio.Semaphore(50)
# 異步請求,并保存到文件
async with ClientSession(connector=TCPConnector(limit=5),
timeout=ClientTimeout(300)) as session:
tasks = []
for page in url_pages:
tasks.append(download(sem, page, folder_name, session))
result = await asyncio.gather(*tasks)
exp = set(url_pages)
act = set(result)
if exp == act:
print(f'全部下載完成')
else:
print(f'未成功:{exp - act},成功:{exp & act}')
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
審核編輯:劉清
-
python
+關注
關注
56文章
4797瀏覽量
84789
發布評論請先 登錄
相關推薦
評論