美文网首页
异步协程

异步协程

作者: 一言不合就跑步 | 来源:发表于2018-11-24 20:26 被阅读0次
    import asyncio
    import aiohttp
    
    async def get(url):
        headers = {
            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
        }
        session = aiohttp.ClientSession()
        response = await session.get(url,headers=headers)
        result = await response.text()
        await session.close()
        return result
    
    async def request(url):
        result = await get(url)
    
    tasks = []
        for i in range(page, page + 19):
            url = 
            task_signal = asyncio.ensure_future(request(url))
            tasks.append(task_signal)
    
        print(tasks)
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.wait(tasks))

    相关文章

      网友评论

          本文标题:异步协程

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