python - Fetching multiple urls with aiohttp from a loopgenerator - Stack Overflow

All examples I saw for fetching multiple urls with aiohttp suggest to to the following:async def fetch

All examples I saw for fetching multiple urls with aiohttp suggest to to the following:

async def fetch(session, url):
    async with session.get(url, ssl=ssl.SSLContext()) as response:
        return await response.json()


async def fetch_all(urls, loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        results = await asyncio.gather(*[fetch(session, url) for url in urls], return_exceptions=True)
        return results


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    urls = url_list
    htmls = loop.run_until_complete(fetch_all(urls, loop))
    print(htmls)

()

In practice, however, I typically have a generator (can be also async) returning domain objects from db, one attribute of which is url, but I also need access to other attributes later in the loop:

async for domain_obj in generator:
    url = domain_obj.url
    response = xxx # need to fetch single url here in async manner
    # do something with response

Of course I can batch collect domain_objs in a list, and fetch all of them like in example, but this doesn't feel right.

All examples I saw for fetching multiple urls with aiohttp suggest to to the following:

async def fetch(session, url):
    async with session.get(url, ssl=ssl.SSLContext()) as response:
        return await response.json()


async def fetch_all(urls, loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        results = await asyncio.gather(*[fetch(session, url) for url in urls], return_exceptions=True)
        return results


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    urls = url_list
    htmls = loop.run_until_complete(fetch_all(urls, loop))
    print(htmls)

(https://stackoverflow/a/51728016/294103)

In practice, however, I typically have a generator (can be also async) returning domain objects from db, one attribute of which is url, but I also need access to other attributes later in the loop:

async for domain_obj in generator:
    url = domain_obj.url
    response = xxx # need to fetch single url here in async manner
    # do something with response

Of course I can batch collect domain_objs in a list, and fetch all of them like in example, but this doesn't feel right.

Share Improve this question asked Nov 21, 2024 at 6:26 dragoondragoon 5,7445 gold badges39 silver badges56 bronze badges 2
  • 1 As an aside, do not use the deprecated asyncio.get_event_loop method. – Booboo Commented Nov 21, 2024 at 13:51
  • yes, thank you, was copied from another question. – dragoon Commented Nov 21, 2024 at 18:04
Add a comment  | 

1 Answer 1

Reset to default 1

My first thought is that you probably want to use TaskGroup.

Something like:

    async with aiohttp.ClientSession() as session:
        tasks = []
        async with asyncio.TaskGroup() as tg:
            async for domain in generator:
                tasks.append(tg.create_task(fetch(session, domain)))
    return [t.result() for t in tasks]

See the linked reference for details on exception handling etc.

This will allow the tasks to start executing while waiting for more results from the generator, rather than needing to exhaust the generator before any tasks are started.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742309963a4419688.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信