Task
Task definition and configuration for DAG nodes.
Task definitions for DAG execution, including sync, async, stream, and process tasks.
AsyncFunctionShutdownTask
Bases: AsyncFunctionTask, AsyncShutdownTask
Async function task with an async shutdown hook.
Source code in shutils/dag/task.py
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
__init__(shutdown_callable, config=None, name='')
Initialize with an async-shutdown-callable function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shutdown_callable
|
AsyncShutdownCallableProtocol
|
An async callable that also has a shutdown() method. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
715 716 717 718 719 720 721 722 723 724 725 726 727 | |
shutdown()
async
Delegate shutdown to the wrapped async callable.
Source code in shutils/dag/task.py
729 730 731 | |
AsyncFunctionTask
Bases: AsyncTask
Async task that wraps a simple async function.
Source code in shutils/dag/task.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
__init__(func, config=None, name='')
Initialize an async function task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[AsyncContext], Coroutine[Any, Any, list[AsyncContext] | AsyncContext | None]]
|
The async function to execute. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
call(async_ctx, env)
async
Execute the wrapped async function and return output contexts.
Source code in shutils/dag/task.py
640 641 642 643 644 645 646 647 | |
AsyncLoopTask
Bases: AsyncStreamTask
Async stream task that automatically re-enqueues until the generator exhausts.
Source code in shutils/dag/task.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
call(async_ctx, env)
async
Execute one iteration and append a LoopContext to continue the loop.
Source code in shutils/dag/task.py
605 606 607 608 609 610 611 612 613 614 615 616 | |
AsyncRouterTask
Bases: AsyncTask
Async task that routes contexts to specific downstream tasks.
Source code in shutils/dag/task.py
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
__init__(func, config=None, name='')
Initialize an async router task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[AsyncContext], Coroutine[Any, Any, str | TaskBase | list[str | TaskBase]]]
|
An async function returning task name(s) or task object(s) to route to. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
call(async_ctx, env)
async
Route the context to specified downstream tasks and mask bypass tasks.
Source code in shutils/dag/task.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
AsyncServiceTask
Bases: AsyncTask, LongRunningTask, AsyncShutdownTask
Async task backed by a long-running coroutine service.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize an async service task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Queue[tuple[AsyncContext, Future]]], Coroutine[Any, Any, None]]
|
An async function that consumes from a queue of (context, future) pairs. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
call(async_ctx, env)
async
Send context to the service coroutine and await the result.
Source code in shutils/dag/task.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
shutdown()
async
Send a stop signal to the service coroutine and await its completion.
Source code in shutils/dag/task.py
543 544 545 546 547 548 | |
AsyncShutdownCallableProtocol
Bases: Protocol
Protocol for async callables that support a shutdown hook.
Source code in shutils/dag/task.py
64 65 66 67 68 | |
AsyncShutdownTask
Bases: ABC
Abstract base for tasks that require an async shutdown hook.
Source code in shutils/dag/task.py
187 188 189 190 191 192 | |
shutdown()
abstractmethod
async
Clean up resources asynchronously when the executor stops.
Source code in shutils/dag/task.py
190 191 192 | |
AsyncStreamTask
Bases: AsyncTask, AsyncShutdownTask
Async task backed by an async generator that streams multiple outputs.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize an async stream task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[], AsyncGenerator[AsyncContext | list[AsyncContext] | None, AsyncContext]]
|
An async generator function that yields output contexts. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If func does not return an async generator. |
Source code in shutils/dag/task.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | |
call(async_ctx, env)
async
Send the context to the async generator and return yielded outputs.
Source code in shutils/dag/task.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
shutdown()
async
Close the underlying async generator.
Source code in shutils/dag/task.py
596 597 598 599 | |
AsyncTask
Bases: TaskBase
Base class for asynchronous tasks.
Source code in shutils/dag/task.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
call(async_ctx, env)
abstractmethod
async
Execute the task asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
async_ctx
|
AsyncContext
|
The async context for this execution. |
required |
env
|
Environment
|
The runtime environment. |
required |
Returns:
| Type | Description |
|---|---|
list[AsyncContext]
|
List of output async contexts. |
Source code in shutils/dag/task.py
223 224 225 226 227 228 229 230 231 232 233 | |
Environment
dataclass
Runtime environment passed to tasks during execution.
Attributes:
| Name | Type | Description |
|---|---|---|
runtime |
Runtime
|
The runtime counter for tracking active contexts. |
process_pool |
ProcessPoolExecutor | None
|
Optional process pool for CPU-bound task execution. |
dag |
DAG
|
The DAG this environment belongs to. |
Source code in shutils/dag/task.py
71 72 73 74 75 76 77 78 79 80 81 82 | |
ForegroundSyncFunctionTask
Bases: SyncFunctionTask, ForegroundTask
Foreground variant of SyncFunctionTask.
Source code in shutils/dag/task.py
740 741 742 743 | |
ForegroundSyncLoopTask
Bases: SyncLoopTask, ForegroundTask
Foreground variant of SyncLoopTask.
Source code in shutils/dag/task.py
746 747 748 749 | |
ForegroundSyncStreamTask
Bases: SyncStreamTask, ForegroundTask
Foreground variant of SyncStreamTask.
Source code in shutils/dag/task.py
734 735 736 737 | |
ForegroundTask
Bases: ABC
Marker class for tasks that must run on the main event loop thread.
Source code in shutils/dag/task.py
165 166 167 168 169 | |
LongRunningTask
Bases: ABC
Marker class for long-running tasks that maintain their own thread or coroutine.
Source code in shutils/dag/task.py
172 173 174 175 176 | |
ProcessTask
Bases: AsyncTask
Async task that offloads execution to a process pool.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize a process task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[SyncContext], list[SyncContext] | SyncContext]
|
The sync function to run in a process pool. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
call(async_ctx, env)
async
Execute the function in a process pool and return output contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
async_ctx
|
AsyncContext
|
The async context for this execution. |
required |
env
|
Environment
|
The runtime environment providing the process pool. |
required |
Returns:
| Type | Description |
|---|---|
list[AsyncContext]
|
List of output async contexts. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no process pool is configured. |
Source code in shutils/dag/task.py
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 | |
ShutdownCallableProtocol
Bases: Protocol
Protocol for sync callables that support a shutdown hook.
Source code in shutils/dag/task.py
57 58 59 60 61 | |
ShutdownTask
Bases: ABC
Abstract base for tasks that require a sync shutdown hook.
Source code in shutils/dag/task.py
179 180 181 182 183 184 | |
shutdown()
abstractmethod
Clean up resources when the executor stops.
Source code in shutils/dag/task.py
182 183 184 | |
SinkNode
Bases: AsyncTask
Sink node that converts contexts to OutputContext for final results.
Source code in shutils/dag/task.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | |
__init__(name='#SinkNode')
Initialize the sink node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Node name, defaults to "#SinkNode". |
'#SinkNode'
|
Source code in shutils/dag/task.py
775 776 777 778 779 780 781 | |
call(async_ctx, env)
async
Convert the context to an OutputContext if not already one.
Source code in shutils/dag/task.py
783 784 785 786 787 788 789 790 791 | |
SourceNode
Bases: AsyncTask
Source node that passes through input contexts unchanged.
Source code in shutils/dag/task.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 | |
__init__(name='#SourceNode')
Initialize the source node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Node name, defaults to "#SourceNode". |
'#SourceNode'
|
Source code in shutils/dag/task.py
755 756 757 758 759 760 761 | |
call(async_ctx, env)
async
Pass through the context unchanged.
Source code in shutils/dag/task.py
763 764 765 | |
SyncFunctionShutdownTask
Bases: SyncFunctionTask, ShutdownTask
Sync function task with a shutdown hook.
Source code in shutils/dag/task.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
__init__(shutdown_callable, config=None, name='')
Initialize with a shutdown-callable function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shutdown_callable
|
ShutdownCallableProtocol
|
A callable that also has a shutdown() method. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
402 403 404 405 406 407 408 409 410 411 412 | |
shutdown()
Delegate shutdown to the wrapped callable.
Source code in shutils/dag/task.py
414 415 416 | |
SyncFunctionTask
Bases: SyncTask
Sync task that wraps a simple function.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize a sync function task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[SyncContext], list[SyncContext] | SyncContext | None]
|
The sync function to execute. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
call(sync_ctx, env)
Execute the wrapped function and return output contexts.
Source code in shutils/dag/task.py
381 382 383 384 385 386 387 388 389 390 391 392 393 | |
SyncLoopTask
Bases: SyncStreamTask
Sync stream task that automatically re-enqueues until the generator exhausts.
Source code in shutils/dag/task.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
call(sync_ctx, env)
Execute one iteration and append a LoopContext to continue the loop.
Source code in shutils/dag/task.py
422 423 424 425 426 427 428 429 430 431 432 433 434 | |
SyncStreamTask
Bases: SyncTask, ShutdownTask
Sync task backed by a generator that streams multiple outputs.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize a sync stream task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[], Generator[SyncContext | list[SyncContext] | None, SyncContext]]
|
A generator function that yields output contexts. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If func does not return a generator. |
Source code in shutils/dag/task.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
call(sync_ctx, env)
Send the context to the generator and return yielded outputs.
Source code in shutils/dag/task.py
339 340 341 342 343 344 345 346 347 348 349 | |
shutdown()
Close the underlying generator.
Source code in shutils/dag/task.py
354 355 356 357 | |
SyncTask
Bases: TaskBase
Base class for synchronous tasks.
Source code in shutils/dag/task.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
call(sync_ctx, env)
abstractmethod
Execute the task synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sync_ctx
|
SyncContext
|
The sync context for this execution. |
required |
env
|
Environment
|
The runtime environment. |
required |
Returns:
| Type | Description |
|---|---|
list[SyncContext]
|
List of output sync contexts. |
Source code in shutils/dag/task.py
198 199 200 201 202 203 204 205 206 207 208 | |
SyncThreadTask
Bases: SyncTask, LongRunningTask, ShutdownTask
Sync task that runs in a dedicated background thread.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize a sync thread task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Queue[tuple[Context, Queue[list[Context] | Context | None]]]], None]
|
A function that consumes from a queue of (context, response_queue) pairs. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name. |
''
|
Source code in shutils/dag/task.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
call(sync_ctx, env)
Send context to the background thread and wait for the result.
Source code in shutils/dag/task.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
shutdown()
Send a stop signal to the background thread and join it.
Source code in shutils/dag/task.py
480 481 482 483 484 | |
TaskBase
Bases: ABC
Abstract base class for all DAG tasks.
Source code in shutils/dag/task.py
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 | |
__init__(func, config=None, name='')
Initialize a task with optional function, config, and name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable | None
|
The callable this task wraps. |
required |
config
|
TaskConfig | None
|
Task execution configuration. |
None
|
name
|
str
|
Optional task name; defaults to a UUID. |
''
|
Source code in shutils/dag/task.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
add_upstream(task)
Add an upstream dependency to this task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
TaskBase
|
The upstream task to depend on. |
required |
Source code in shutils/dag/task.py
120 121 122 123 124 125 126 127 | |
call_after(context_list)
Post-execution hook to release rate-limit tokens and update parallel count.
Source code in shutils/dag/task.py
147 148 149 150 151 | |
call_before(context)
Pre-execution hook for parallel and rate-limit control.
Returns a context if the task should be skipped, or None to proceed.
Source code in shutils/dag/task.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
TaskConfig
dataclass
Configuration for task execution behavior.
Attributes:
| Name | Type | Description |
|---|---|---|
retry_times |
int
|
Maximum number of retry attempts on failure. |
retry_interval |
int | float | Callable[[Context], float | int]
|
Seconds between retries, or a callable returning the interval. |
parallel_num |
int
|
Maximum concurrent executions (0 means unlimited). |
limiter |
Limiter | None
|
Optional rate limiter for QPS/concurrency control. |
Source code in shutils/dag/task.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |