#๐Ÿ”’ idiomatic asyncio task sharing

9 messages ยท Page 1 of 1 (latest)

coarse trail
#

I have the following code (advised by the LLM of the moment), to parallelize computation (it's CPU-bound, but since it's another process, it's IO for the script)

async def check_one_report(...) -> None:
    async with semaphore:
        # do stuff


async def async_check_reports(...) -> None:
    """Check the reports in the database"""

    semaphore = asyncio.Semaphore(MAX_CONCURRENCY)
    tasks = [
        asyncio.create_task(check_one_report(report, zulip, semaphore))
        for report in c
    ]
    await asyncio.gather(*tasks)
    log.info("All reports checked")

It works fine but I'm not sure if that's the most idiomatic way to do so, as it seems "wasteful" (again, in practice in does not matter, it's a script) to create so many throwaway tasks, and would not work if unchecked_reports was unbounded. I was more imagining push/sub with messages sent by a channel of some sort

olive pulsarBOT
#

@coarse trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

haughty jewel
coarse trail
#

I encourage you to read my post

haughty jewel
#

i have read it

quartz lintel
#

tasks are very lightweight

#

the problem with gather() though is that if something goes wrong, you only get the first exception and the rest are discarded

olive pulsarBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.