0%

python 异步协程

Python_异步协程:

Python_异步协程:

基本使用方法:

async def func():
await asyncio.sleep(3) # 等待io
return '值'

async def main():
task_list = [asyncio.create_task(func())]
done, pending = await asyncio.wait(task_list) # 定义并发协程 返回值在done中
asyncio.run(main()) 执行


异步爬虫:

import aiohttp
import asyncio


async def func(session, url):
print('发送请求:', url)
async with session.get(url) as response:
text = await response.text()
print('得到结果:', url, len(text))
print('-'*100)

async def main(data):
async with aiohttp.ClientSession() as session:
tasks = [asyncio.create_task(func(session, url)) for url in data]
await asyncio.wait(tasks)

if __name__ == '__main__':
a = ['https://www.biqusa.com/143_143300/', 'https://www.biqusa.com/143_143386/',
'https://www.biqusa.com/143_143694/', 'https://www.biqusa.com/42_42546/',
'https://www.biqusa.com/143_143728/']

asyncio.run(main(a))

异步协程和不支持协程的模块 - requests:

import aiohttp
import asyncio


async def func(session, url):
print('发送请求:', url)
async with session.get(url) as response:
text = await response.text()
print('得到结果:', url, len(text))
print('-'*100)

async def main(data):
async with aiohttp.ClientSession() as session:
tasks = [asyncio.create_task(func(session, url)) for url in data]
await asyncio.wait(tasks)

if __name__ == '__main__':
a = ['https://www.biqusa.com/143_143300/', 'https://www.biqusa.com/143_143386/',
'https://www.biqusa.com/143_143694/', 'https://www.biqusa.com/42_42546/', 'https://www.biqusa.com/143_143806/',
'https://www.biqusa.com/143_143728/']

asyncio.run(main(a))