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