task_queue
Priority-based async task queue backed by a dispatch QThread + worker pool.
This module provides a generic priority-based task queue system. A single dispatch QThread dequeues tasks from a PriorityQueue and submits them to a ThreadPoolExecutor (default: 4 workers) so that independent fetches — for instance all children of simultaneously-expanded tree nodes — run in parallel rather than serially.
Key optimisations vs. the original single-worker design:
- No polling sleep - the dispatch loop blocks on a
threading.Eventthat is set by :meth:AsyncTaskQueue.enqueuethe instant a new task arrives, so there is zero idle wait between tree-expansion waves. - Parallel execution - up to
num_workersfetch tasks run at the same time, halving the effective latency when expanding N sibling nodes from O(N × round-trip) to O(round-trip / num_workers × N).
Each task carries an optional context_id label. External components can call :meth:AsyncTaskQueue.clear_context_tasks to remove all pending tasks for a given context when the associated state becomes stale (e.g. a selection change). Multiple distinct context IDs can coexist in the queue at the same time.
AsyncTask dataclass
Represents a task to be executed by the async task queue.
This class holds all the information needed to execute an asynchronous operation, including the work function, callback, priority, and context tracking for cancellation support.
Attributes:
| Name | Type | Description |
|---|---|---|
name | str | Descriptive name for the task (e.g., "fetch_activity_data"). |
function | Callable[[], Any] | Callable that performs the work. |
callback | Callable[[Any], None] | Function to call with the result when task completes. |
priority | int | Priority level (lower numbers = higher priority). 0 = Critical (UI blocking operations) 1 = High (User-initiated operations) 5 = Normal (Background fetches) 10 = Low (Prefetching) |
context_id | str | Optional label grouping related tasks. Pass the same id to :meth: |
cancellable | bool | If True, the task can be removed by :meth: |
Example
task = AsyncTask( name="fetch_activities", function=lambda: fetch_data(context), callback=on_data_ready, priority=1, context_id="project:item_id", cancellable=True )
Source code in client/ayon_ui_qt/components/task_queue.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 | |
__eq__(other)
Check equality based on priority and counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other | object | Object to compare with. | required |
Returns:
| Type | Description |
|---|---|
bool | True if tasks have equal priority and counter. |
Source code in client/ayon_ui_qt/components/task_queue.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
__lt__(other)
Compare tasks by priority, then by counter for FIFO ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other | AsyncTask | Another AsyncTask to compare with. | required |
Returns:
| Type | Description |
|---|---|
bool | True if this task should be processed before the other task. |
Source code in client/ayon_ui_qt/components/task_queue.py
112 113 114 115 116 117 118 119 120 121 122 123 | |
cancel()
Mark this task as cancelled.
Only affects the task if cancellable is True.
Source code in client/ayon_ui_qt/components/task_queue.py
96 97 98 99 100 101 102 | |
is_cancelled()
Check if this task has been cancelled.
Returns:
| Type | Description |
|---|---|
bool | True if the task has been cancelled, False otherwise. |
Source code in client/ayon_ui_qt/components/task_queue.py
104 105 106 107 108 109 110 | |
AsyncTaskQueue
Bases: QThread
Dispatch thread + pool that processes async tasks from a priority queue.
A single QThread (the dispatch loop) continuously dequeues the highest-priority task and submits it to an internal ThreadPoolExecutor so that multiple tasks can execute in parallel.
This removes two performance bottlenecks that made multi-level tree expansion slow in the original single-worker design:
-
No idle polling - instead of sleeping 50 ms when the queue is empty, the dispatch loop blocks on a
threading.Eventthat :meth:enqueuesets immediately, so there is zero dead time between a tree-level's results arriving on the main thread, newfetchMorecalls being enqueued, and those fetches starting. -
Parallel execution - up to
num_workersfetches run concurrently, so expanding a folder with N children takesceil(N / num_workers)round-trips instead of N.
Signals
task_completed: Emitted when a task finishes (task_name, result). task_failed: Emitted when a task raises exception (task_name, err). task_cancelled: Emitted when task is cancelled (task_name, ctx_id). queue_empty: Emitted when all tasks are processed.
Multiple :attr:~AsyncTask.context_id values can coexist in the queue. Call :meth:clear_context_tasks to remove all pending tasks for a given context when external state changes make them irrelevant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_workers | int | Number of parallel pool workers (default 4). | _DEFAULT_NUM_WORKERS |
parent | Any | Optional parent QObject. | None |
Example
queue = AsyncTaskQueue() queue.task_completed.connect(handle_completion) queue.start()
task = AsyncTask(...) queue.enqueue(task)
When the context is no longer relevant:
queue.clear_context_tasks(context_id)
Later...
queue.stop()
Source code in client/ayon_ui_qt/components/task_queue.py
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
__init__(parent=None, num_workers=_DEFAULT_NUM_WORKERS)
Initialise the dispatch thread and pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_workers | int | Number of parallel pool workers. Defaults to :data: | _DEFAULT_NUM_WORKERS |
parent | Any | Optional parent QObject. | None |
Source code in client/ayon_ui_qt/components/task_queue.py
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 232 233 234 235 236 237 238 239 | |
clear_context_tasks(context_id)
Completely remove tasks for a specific context from queue.
More aggressive than cancel - actually removes from queue rather than just marking as cancelled.
Note
Tasks already submitted to the pool executor (i.e. currently running or waiting for a free pool slot) are not removed here. Their results will be discarded by the model's stale- context check in the callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id | str | Context identifier to clear. | required |
Returns:
| Type | Description |
|---|---|
int | Number of tasks removed. |
Source code in client/ayon_ui_qt/components/task_queue.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
enqueue(task)
Add a task to the queue (thread-safe).
Assigns a monotonically increasing counter to the task to ensure FIFO ordering within the same priority level. Sets _task_available so the dispatch loop wakes immediately instead of waiting for its polling timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task | AsyncTask | The task to enqueue. | required |
Source code in client/ayon_ui_qt/components/task_queue.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
is_paused()
Check if the worker is currently paused.
Returns:
| Type | Description |
|---|---|
bool | True if paused, False otherwise. |
Source code in client/ayon_ui_qt/components/task_queue.py
528 529 530 531 532 533 534 | |
pause()
Pause task dispatching.
The dispatch loop finishes submitting the current task and then waits until resumed. Tasks can still be enqueued while paused.
Source code in client/ayon_ui_qt/components/task_queue.py
478 479 480 481 482 483 484 485 | |
queue_size()
Get the current number of tasks in the priority queue.
Returns:
| Type | Description |
|---|---|
int | Number of pending tasks (does not include tasks already |
int | submitted to the pool). |
Source code in client/ayon_ui_qt/components/task_queue.py
536 537 538 539 540 541 542 543 | |
resume()
Resume task dispatching.
Dispatching resumes from where it was paused.
Source code in client/ayon_ui_qt/components/task_queue.py
487 488 489 490 491 492 493 | |
run()
Dispatch loop - runs in the QThread context.
Dequeues the highest-priority pending task and submits it to the thread pool. Blocks on _task_available when the queue is empty so it wakes the instant :meth:enqueue adds a new task, eliminating the 50 ms polling delay of the previous single-worker design.
Source code in client/ayon_ui_qt/components/task_queue.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
stop()
Stop the dispatch thread and pool workers gracefully.
Sets _running = False, then wakes the dispatch loop so it can exit immediately. The loop's finally block shuts the executor down (cancelling queued-but-not-started futures; already-running ones complete normally). Waits up to 5 seconds for everything to finish.
Any pending (callback, result) pairs left in _callback_queue are discarded so that stale model callbacks from a previous test (or a discarded context) cannot fire after teardown.
Source code in client/ayon_ui_qt/components/task_queue.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
get_task_queue()
Return the shared AsyncTaskQueue, creating and starting it on first use.
The queue is a module-level singleton so all components (table models, tree models, etc.) share one dispatch thread and one pool.
On first creation the queue is automatically connected to QApplication.aboutToQuit so it stops cleanly when the application exits — no manual :func:shutdown_task_queue call is required.
Returns:
| Type | Description |
|---|---|
AsyncTaskQueue | The running shared :class: |
Source code in client/ayon_ui_qt/components/task_queue.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 | |
shutdown_task_queue()
Stop and discard the shared :class:AsyncTaskQueue.
Called automatically when the QApplication emits aboutToQuit (wired by :func:get_task_queue on first use). Safe to call manually before that if an early teardown is needed; subsequent calls are no-ops. After this call :func:get_task_queue will create a fresh queue on next access.
Source code in client/ayon_ui_qt/components/task_queue.py
677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |