From: Michael Tremer Date: Mon, 29 May 2023 14:23:34 +0000 (+0000) Subject: builders: Replace asyncio.gather() with task groups X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3c0f0cc699dc9912a2b5d0f7bda3a5366a1fd84;p=pbs.git builders: Replace asyncio.gather() with task groups This is easier to write/read and we can run multiple actions concurrently. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builders.py b/src/buildservice/builders.py index c2dcb8c8..75eb15fc 100644 --- a/src/buildservice/builders.py +++ b/src/buildservice/builders.py @@ -81,7 +81,11 @@ class Builders(base.Object): log.info("Syncing state with AWS") # Sync all builders - await asyncio.gather(*(builder.sync() for builder in self)) + async with asyncio.TaskGroup() as tasks: + for builder in self: + tasks.create_task( + builder.sync(), + ) async def autoscale(self, wait=False): """ @@ -150,15 +154,18 @@ class Builders(base.Object): builder for builder in builders if not queue[builder] and builder.is_idle() ] - # Start all builders that have been allocated at least one job - await asyncio.gather( - *(builder.start(wait=wait) for builder in builders_to_be_launched), - ) + async with asyncio.TaskGroup() as tasks: + # Start all builders that have been allocated at least one job + for builder in builders_to_be_launched: + tasks.create_task( + builder.start(wait=wait), + ) - # Shutdown the rest - await asyncio.gather( - *(builder.stop(wait=wait) for builder in builders_to_be_shut_down), - ) + # Shutdown the rest + for builder in builders_to_be_shut_down: + tasks.create_task( + builder.stop(wait=wait), + ) # Stats @@ -239,9 +246,11 @@ class BuildersStats(base.Object): return # Send the stats to all connections - await asyncio.gather( - *(c.submit_stats(stats) for c in connections), - ) + async with asyncio.TaskGroup() as tasks: + for c in connections: + tasks.create_task( + c.submit_stats(stats), + ) class Builder(base.DataObject):