API Reference
docket
docket - A distributed background task system for Python functions.
docket focuses on scheduling future work as seamlessly and efficiently as immediate work.
Docket
A Docket represents a collection of tasks that may be scheduled for later execution. With a Docket, you can add, replace, and cancel tasks. Example:
@task
async def my_task(greeting: str, recipient: str) -> None:
print(f"{greeting}, {recipient}!")
async with Docket() as docket:
docket.add(my_task)("Hello", recipient="world")
Source code in src/docket/docket.py
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 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 632 633 634 635 636 637 638 639 640 641 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 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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
|
__init__(name='docket', url='redis://localhost:6379/0', heartbeat_interval=timedelta(seconds=2), missed_heartbeats=5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the docket. |
'docket'
|
url
|
str
|
The URL of the Redis server. For example: - "redis://localhost:6379/0" - "redis://user:password@localhost:6379/0" - "redis://user:password@localhost:6379/0?ssl=true" - "rediss://localhost:6379/0" - "unix:///path/to/redis.sock" |
'redis://localhost:6379/0'
|
heartbeat_interval
|
timedelta
|
How often workers send heartbeat messages to the docket. |
timedelta(seconds=2)
|
missed_heartbeats
|
int
|
How many heartbeats a worker can miss before it is considered dead. |
5
|
Source code in src/docket/docket.py
add(function, when=None, key=None)
Add a task to the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable[P, Awaitable[R]] | str
|
The task to add. |
required |
when
|
datetime | None
|
The time to schedule the task. |
None
|
key
|
str | None
|
The key to schedule the task under. |
None
|
Source code in src/docket/docket.py
cancel(key)
async
Cancel a previously scheduled task on the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key of the task to cancel. |
required |
Source code in src/docket/docket.py
register(function)
Register a task with the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
TaskFunction
|
The task to register. |
required |
Source code in src/docket/docket.py
register_collection(collection_path)
Register a collection of tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection_path
|
str
|
A path in the format "module:collection". |
required |
Source code in src/docket/docket.py
replace(function, when, key)
Replace a previously scheduled task on the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable[P, Awaitable[R]] | str
|
The task to replace. |
required |
when
|
datetime
|
The time to schedule the task. |
required |
key
|
str
|
The key to schedule the task under. |
required |
Source code in src/docket/docket.py
restore(function=None, parameter=None, operator='==', value=None)
async
Restore a previously stricken task to the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable[P, Awaitable[R]] | str | None
|
The task to restore. |
None
|
parameter
|
str | None
|
The parameter to restore on. |
None
|
operator
|
Operator | LiteralOperator
|
The operator to use. |
'=='
|
value
|
Hashable | None
|
The value to restore on. |
None
|
Source code in src/docket/docket.py
snapshot()
async
Get a snapshot of the Docket, including which tasks are scheduled or currently running, as well as which workers are active.
Returns:
Type | Description |
---|---|
DocketSnapshot
|
A snapshot of the Docket. |
Source code in src/docket/docket.py
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 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
|
strike(function=None, parameter=None, operator='==', value=None)
async
Strike a task from the Docket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
Callable[P, Awaitable[R]] | str | None
|
The task to strike. |
None
|
parameter
|
str | None
|
The parameter to strike on. |
None
|
operator
|
Operator | LiteralOperator
|
The operator to use. |
'=='
|
value
|
Hashable | None
|
The value to strike on. |
None
|
Source code in src/docket/docket.py
task_workers(task_name)
async
Get a list of all workers that are able to execute a given task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_name
|
str
|
The name of the task. |
required |
Returns:
Type | Description |
---|---|
Collection[WorkerInfo]
|
A list of all workers that are able to execute the given task. |
Source code in src/docket/docket.py
workers()
async
Get a list of all workers that have sent heartbeats to the Docket.
Returns:
Type | Description |
---|---|
Collection[WorkerInfo]
|
A list of all workers that have sent heartbeats to the Docket. |
Source code in src/docket/docket.py
ExponentialRetry
Bases: Retry
Configures exponential retries for a task. You can specify the total number
of attempts (or None
to retry indefinitely), and the minimum and maximum delays
between attempts.
Example:
Source code in src/docket/dependencies.py
__init__(attempts=1, minimum_delay=timedelta(seconds=1), maximum_delay=timedelta(seconds=64))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attempts
|
int | None
|
The total number of attempts to make. If |
1
|
minimum_delay
|
timedelta
|
The minimum delay between attempts. |
timedelta(seconds=1)
|
maximum_delay
|
timedelta
|
The maximum delay between attempts. |
timedelta(seconds=64)
|
Source code in src/docket/dependencies.py
Logged
Bases: Annotation
Instructs docket to include arguments to this parameter in the log.
If length_only
is True
, only the length of the argument will be included in
the log.
Example:
@task
def setup_new_customer(
customer_id: Annotated[int, Logged],
addresses: Annotated[list[Address], Logged(length_only=True)],
password: str,
) -> None:
...
In the logs, you's see the task referenced as:
Source code in src/docket/annotations.py
Perpetual
Bases: Dependency
Declare a task that should be run perpetually. Perpetual tasks are automatically
rescheduled for the future after they finish (whether they succeed or fail). A
perpetual task can be scheduled at worker startup with the automatic=True
.
Example:
Source code in src/docket/dependencies.py
__init__(every=timedelta(0), automatic=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
every
|
timedelta
|
The target interval between task executions. |
timedelta(0)
|
automatic
|
bool
|
If set, this task will be automatically scheduled during worker startup and continually through the worker's lifespan. This ensures that the task will always be scheduled despite crashes and other adverse conditions. Automatic tasks must not require any arguments. |
False
|
Source code in src/docket/dependencies.py
Retry
Bases: Dependency
Configures linear retries for a task. You can specify the total number of
attempts (or None
to retry indefinitely), and the delay between attempts.
Example:
Source code in src/docket/dependencies.py
__init__(attempts=1, delay=timedelta(0))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attempts
|
int | None
|
The total number of attempts to make. If |
1
|
delay
|
timedelta
|
The delay between attempts. |
timedelta(0)
|
Source code in src/docket/dependencies.py
Timeout
Bases: Dependency
Configures a timeout for a task. You can specify the base timeout, and the task will be cancelled if it exceeds this duration. The timeout may be extended within the context of a single running task.
Example:
Source code in src/docket/dependencies.py
__init__(base)
extend(by=None)
Extend the timeout by a given duration. If no duration is provided, the base timeout will be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by
|
timedelta | None
|
The duration to extend the timeout by. |
None
|
Source code in src/docket/dependencies.py
Worker
A Worker executes tasks on a Docket. You may run as many workers as you like to work a single Docket.
Example:
Source code in src/docket/worker.py
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 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 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 632 633 634 635 636 637 638 639 640 641 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 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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
|
run_at_most(iterations_by_key)
async
Run the worker until there are no more tasks to process, but limit specified task keys to a maximum number of iterations.
This is particularly useful for testing self-perpetuating tasks that would otherwise run indefinitely.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations_by_key
|
Mapping[str, int]
|
Maps task keys to their maximum allowed executions |
required |
Source code in src/docket/worker.py
run_forever()
async
CurrentDocket()
CurrentExecution()
CurrentWorker()
Depends(dependency)
Include a user-defined function as a dependency. Dependencies may either return a value or an async context manager. If it returns a context manager, the dependency will be entered and exited around the task, giving an opportunity to control the lifetime of a resource, like a database connection.
Example:
async def my_dependency() -> str:
return "Hello, world!"
@task async def my_task(dependency: str = Depends(my_dependency)) -> None:
print(dependency)
Source code in src/docket/dependencies.py
TaskArgument(parameter=None, optional=False)
A dependency to access a argument of the currently executing task. This is often useful in dependency functions so they can access the arguments of the task they are injected into.
Example:
async def customer_name(customer_id: int = TaskArgument()) -> str:
...look up the customer's name by ID...
return "John Doe"
@task
async def greet_customer(customer_id: int, name: str = Depends(customer_name)) -> None:
print(f"Hello, {name}!")
Source code in src/docket/dependencies.py
TaskKey()
A dependency to access the key of the currently executing task.
Example:
Source code in src/docket/dependencies.py
TaskLogger()
A dependency to access a logger for the currently executing task. The logger will automatically inject contextual information such as the worker and docket name, the task key, and the current execution attempt number.
Example:
@task
async def my_task(logger: LoggerAdapter[Logger] = TaskLogger()) -> None:
logger.info("Hello, world!")