Commit 0fa93f9
Performance [P1]: Bound async worker task creation (#200)
* Bound async worker task creation to the concurrency limit
`_AsyncWorkerManager._consume_queue()` created an `asyncio.Task` for every
dequeued work item and only acquired the concurrency semaphore later, inside
`_process_work_item()`. The semaphore therefore throttled execution but not
allocation, so during a burst with slow handlers the number of pending task
objects grew with the queue depth rather than with the configured concurrency.
The loop also rescanned the entire `running_tasks` set on every iteration.
The consumer now acquires a semaphore permit *before* dequeuing an item and
hands that permit off to the task it creates, which releases it once the work
item is done. The queue stays the bounded backlog and in-flight work item tasks
never exceed the configured `ConcurrencyOptions` limits. Completed tasks are
removed from `running_tasks` via `task.add_done_callback(set.discard)` instead
of an O(n) sweep.
Semantics are preserved: there is still no await between `queue.get()`
returning and `asyncio.create_task()`, so an item can never be dropped by
cancellation; the permit is released if the dequeue times out or raises; and
`queue.task_done()` is still called exactly once per item, before the permit is
released, on both the success and failure paths.
Fixes #193
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 010b6832-802b-425d-957c-cf29b7143392
* Clarify that the queue-readiness helper returns a coroutine
The second element of the tuple returned by _start_manager_and_wait_for_queues is an already-created coroutine object, not a callable. Naming the local wait_for_queues made �wait wait_for_queues read like a missing call -- it misled an automated reviewer and would mislead a human too. Rename the local to queues_ready and say so in the docstring. No behavior change.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 010b6832-802b-425d-957c-cf29b7143392
---------
Co-authored-by: Bernd Verst <beverst@microsoft.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 010b6832-802b-425d-957c-cf29b71433921 parent fc288ad commit 0fa93f9
3 files changed
Lines changed: 172 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3440 | 3440 | | |
3441 | 3441 | | |
3442 | 3442 | | |
3443 | | - | |
| 3443 | + | |
| 3444 | + | |
3444 | 3445 | | |
3445 | 3446 | | |
3446 | 3447 | | |
3447 | | - | |
3448 | | - | |
3449 | | - | |
3450 | | - | |
3451 | 3448 | | |
3452 | 3449 | | |
3453 | 3450 | | |
3454 | 3451 | | |
| 3452 | + | |
| 3453 | + | |
| 3454 | + | |
| 3455 | + | |
| 3456 | + | |
| 3457 | + | |
| 3458 | + | |
3455 | 3459 | | |
3456 | | - | |
3457 | | - | |
3458 | | - | |
| 3460 | + | |
| 3461 | + | |
| 3462 | + | |
| 3463 | + | |
3459 | 3464 | | |
3460 | | - | |
3461 | | - | |
3462 | | - | |
3463 | | - | |
3464 | | - | |
3465 | | - | |
| 3465 | + | |
| 3466 | + | |
| 3467 | + | |
| 3468 | + | |
| 3469 | + | |
| 3470 | + | |
| 3471 | + | |
| 3472 | + | |
| 3473 | + | |
| 3474 | + | |
| 3475 | + | |
3466 | 3476 | | |
3467 | 3477 | | |
3468 | 3478 | | |
3469 | 3479 | | |
3470 | 3480 | | |
3471 | 3481 | | |
3472 | | - | |
| 3482 | + | |
| 3483 | + | |
| 3484 | + | |
| 3485 | + | |
| 3486 | + | |
| 3487 | + | |
| 3488 | + | |
| 3489 | + | |
3473 | 3490 | | |
3474 | | - | |
3475 | | - | |
3476 | | - | |
3477 | | - | |
3478 | | - | |
3479 | 3491 | | |
| 3492 | + | |
| 3493 | + | |
3480 | 3494 | | |
3481 | 3495 | | |
3482 | 3496 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
0 commit comments