Two observations:
-
Your approach with
for chunk in chunks
doesn't keepmaxConcurrent
tasks in flight at all times. Rather, it will wait for all tasks in the first chunk to finish before starting with the second chunk, and so on. Is this what you want?Here's an alternative approach from @Jon_Shier to set an upper bound on the number of concurrent child tasks in a task group: Trying to implement basic concurrent code with actors - #12 by Jon_Shier
-
Your code silently ignores any errors thrown by
try await download(task: task)
. Because you don't explicitly await the results produced by the child tasks, the task group will implicitly await its child tasks when it goes out of scope. Any errors thrown by the child tasks will be thrown away.The compiler gives you a tiny hint that this is happening by allowing you to write
await withThrowingTaskGroup
and not forcing you to writetry await
here.