asyncio
使用 requests_async 库(一个支持异步的 HTTP 库)进行异步网络请求:
import requests_async as requests
async def main(args):
# 你可以通过这种方式获取url
url = args.params['url']
# 使用await,这将异步等待网络请求的完成
response = await requests.get(url)
ret = {
'code': response.status_code,
'res': response.text,
}
return retaiohttp
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://python.org')
print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())