先安装nest_asyncio
pip install nest_asyncio
import asyncio
from aiohttp import ClientSession
import time
import nest_asyncio
nest_asyncio.apply()
async def hello():
url = 'https://www.jianshu.com/u'
data = {'slength':1.0,'swidth':1.0}
async with ClientSession() as session:
async with session.post(url=url,json=data) as response:
response = await response.read()
return response
#设置并发数量
tasks = [asyncio.ensure_future(hello()) for _ in range(1000)]
start = time.clock()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.clock()
print('耗时%d秒' %(end-start))
网友评论