美文网首页
异步请求

异步请求

作者: barriers | 来源:发表于2019-12-26 20:28 被阅读0次

    先安装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))

    相关文章

      网友评论

          本文标题:异步请求

          本文链接:https://www.haomeiwen.com/subject/kcbloctx.html