Core Components
The core components provide the fundamental building blocks for async cancelation in Python.
Cancelable
The main context manager for cancelable operations.
hother.cancelable.core.cancelable.Cancelable
Main cancelation helper with composable cancelation sources.
Provides a unified interface for handling cancelation from multiple sources including timeouts, tokens, signals, and conditions.
Source code in src/hother/cancelable/core/cancelable.py
46 47 48 49 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 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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 | |
token
property
token: LinkedCancelationToken
Get the cancellation token for this operation.
Returns:
| Type | Description |
|---|---|
LinkedCancelationToken
|
The LinkedCancelationToken managing this operation's cancellation state. |
parent
property
parent: Cancelable | None
Get parent cancelable, returning None if garbage collected.
add_source
add_source(source: CancelationSource) -> Cancelable
Add a cancelation source to this operation.
This allows adding custom or composite sources (like AllOfSource) to an existing Cancelable instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
CancelationSource
|
The cancelation source to add |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Example
Source code in src/hother/cancelable/core/cancelable.py
with_timeout
classmethod
with_timeout(
timeout: float | timedelta,
operation_id: str | None = None,
name: str | None = None,
**kwargs: Any,
) -> Cancelable
Create cancelable with timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | timedelta
|
Timeout duration in seconds or timedelta |
required |
operation_id
|
str | None
|
Optional operation ID |
None
|
name
|
str | None
|
Optional operation name |
None
|
**kwargs
|
Any
|
Additional arguments for Cancelable |
{}
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
Configured Cancelable instance |
Source code in src/hother/cancelable/core/cancelable.py
with_token
classmethod
with_token(
token: CancelationToken,
operation_id: str | None = None,
name: str | None = None,
**kwargs: Any,
) -> Cancelable
Create a Cancelable operation using an existing cancellation token.
This factory method allows you to create a cancellable operation that shares a cancellation token with other operations, enabling coordinated cancellation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
CancelationToken
|
The CancelationToken to use for this operation |
required |
operation_id
|
str | None
|
Optional custom operation identifier |
None
|
name
|
str | None
|
Optional operation name (defaults to "token_based") |
None
|
**kwargs
|
Any
|
Additional arguments passed to Cancelable constructor |
{}
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
A configured Cancelable instance using the provided token |
Example
# Share a token between multiple operations
shared_token = CancelationToken()
async with Cancelable.with_token(shared_token, name="task1") as cancel1:
# ... operation 1 ...
async with Cancelable.with_token(shared_token, name="task2") as cancel2:
# ... operation 2 ...
# Cancel both operations via the shared token
await shared_token.cancel()
Source code in src/hother/cancelable/core/cancelable.py
with_signal
classmethod
with_signal(
*signals: int,
operation_id: str | None = None,
name: str | None = None,
**kwargs: Any,
) -> Cancelable
Create cancelable with signal handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*signals
|
int
|
Signal numbers to handle |
()
|
operation_id
|
str | None
|
Optional operation ID |
None
|
name
|
str | None
|
Optional operation name |
None
|
**kwargs
|
Any
|
Additional arguments for Cancelable |
{}
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
Configured Cancelable instance |
Source code in src/hother/cancelable/core/cancelable.py
with_condition
classmethod
with_condition(
condition: Callable[[], bool | Awaitable[bool]],
check_interval: float = 0.1,
condition_name: str | None = None,
operation_id: str | None = None,
name: str | None = None,
**kwargs: Any,
) -> Cancelable
Create cancelable with condition checking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
Callable[[], bool | Awaitable[bool]]
|
Callable that returns True when cancelation should occur |
required |
check_interval
|
float
|
How often to check the condition (seconds) |
0.1
|
condition_name
|
str | None
|
Name for the condition (for logging) |
None
|
operation_id
|
str | None
|
Optional operation ID |
None
|
name
|
str | None
|
Optional operation name |
None
|
**kwargs
|
Any
|
Additional arguments for Cancelable |
{}
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
Configured Cancelable instance |
Source code in src/hother/cancelable/core/cancelable.py
combine
combine(*others: Cancelable) -> Cancelable
Combine multiple Cancelable operations into a single coordinated operation.
Creates a new Cancelable that will be cancelled if ANY of the combined operations is cancelled. All cancellation sources from the combined operations are merged together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Cancelable
|
One or more Cancelable instances to combine with this one |
()
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
A new Cancelable instance that coordinates cancellation across all |
Cancelable
|
combined operations. When entered, all operations' tokens are linked. |
Example
Note
The combined Cancelable preserves the cancellation reason from whichever source triggers first.
Source code in src/hother/cancelable/core/cancelable.py
on_progress
on_progress(callback: ProgressCallbackType) -> Cancelable
Register a callback to be invoked when progress is reported.
The callback will be called whenever report_progress() is invoked
on this operation. Both sync and async callbacks are supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
ProgressCallbackType
|
Function to call on progress updates. Receives: - operation_id (str): The ID of the operation - message (Any): The progress message - metadata (dict[str, Any] | None): Optional metadata |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Example
Source code in src/hother/cancelable/core/cancelable.py
on_start
on_start(callback: StatusCallbackType) -> Cancelable
Register a callback to be invoked when the operation starts.
The callback is triggered when entering the async context (on __aenter__).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
StatusCallbackType
|
Function receiving the OperationContext. Can be sync or async. |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Source code in src/hother/cancelable/core/cancelable.py
on_complete
on_complete(callback: StatusCallbackType) -> Cancelable
Register a callback to be invoked when the operation completes successfully.
The callback is triggered when exiting the context without cancellation or error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
StatusCallbackType
|
Function receiving the OperationContext. Can be sync or async. |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Source code in src/hother/cancelable/core/cancelable.py
on_cancel
on_cancel(callback: StatusCallbackType) -> Cancelable
Register a callback to be invoked when the operation is cancelled.
The callback is triggered when the operation is cancelled by any source (timeout, signal, token, condition, or parent cancellation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
StatusCallbackType
|
Function receiving the OperationContext. Can be sync or async. |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Source code in src/hother/cancelable/core/cancelable.py
on_error
on_error(callback: ErrorCallbackType) -> Cancelable
Register a callback to be invoked when the operation encounters an error.
The callback is triggered when an exception (other than CancelledError) is raised within the operation context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
ErrorCallbackType
|
Function receiving the OperationContext and Exception. Can be sync or async. |
required |
Returns:
| Type | Description |
|---|---|
Cancelable
|
Self for method chaining |
Source code in src/hother/cancelable/core/cancelable.py
report_progress
async
Report progress to all registered callbacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Any
|
Progress message |
required |
metadata
|
dict[str, Any] | None
|
Optional metadata dict |
None
|
Source code in src/hother/cancelable/core/cancelable.py
check_cancelation
async
Check if operation is cancelled and raise if so.
This is a public API for checking cancellation state.
Use this instead of accessing _token directly.
Raises:
| Type | Description |
|---|---|
CancelledError
|
If operation is cancelled |
Source code in src/hother/cancelable/core/cancelable.py
run_in_thread
async
Run function in thread with proper context propagation.
This method solves the context variable thread safety issue by ensuring that context variables (including _current_operation) are properly propagated to OS threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to run in thread |
required |
*args
|
Any
|
Positional arguments for func |
()
|
**kwargs
|
Any
|
Keyword arguments for func |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
Result of func execution |
Example
Source code in src/hother/cancelable/core/cancelable.py
stream
async
stream(
async_iter: AsyncIterator[T],
report_interval: int | None = None,
buffer_partial: bool = True,
) -> AsyncIterator[T]
Wrap async iterator with cancelation support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
async_iter
|
AsyncIterator[T]
|
Async iterator to wrap |
required |
report_interval
|
int | None
|
Report progress every N items |
None
|
buffer_partial
|
bool
|
Whether to buffer items for partial results |
True
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[T]
|
Items from the wrapped iterator |
Source code in src/hother/cancelable/core/cancelable.py
wrap
Wrap an async operation to automatically check for cancelation before execution.
This is useful for retry loops and other patterns where you want automatic cancelation checking without manually accessing the token.
Note: This assumes the cancelable context is already active (you're inside
an async with block). It does NOT create a new context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
Callable[..., Awaitable[R]]
|
Async callable to wrap |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Awaitable[R]]
|
Wrapped callable that checks cancelation before executing |
Example
Source code in src/hother/cancelable/core/cancelable.py
wrapping
async
wrapping() -> AsyncIterator[Callable[..., Awaitable[R]]]
Async context manager that yields a wrap function for scoped operation wrapping.
The yielded wrap function checks cancelation before executing any operation. This is useful for retry loops where you want all operations in a scope to be automatically wrapped with cancelation checking.
Yields:
| Type | Description |
|---|---|
AsyncIterator[Callable[..., Awaitable[R]]]
|
A wrap function that checks cancelation before executing operations |
Example
Source code in src/hother/cancelable/core/cancelable.py
shield
async
shield() -> AsyncIterator[Cancelable]
Shield a section from cancelation.
Creates a child operation that is protected from cancelation but still participates in the operation hierarchy for monitoring and tracking.
Yields:
| Type | Description |
|---|---|
AsyncIterator[Cancelable]
|
A new Cancelable for the shielded section |
Source code in src/hother/cancelable/core/cancelable.py
cancel
async
cancel(
reason: CancelationReason = MANUAL,
message: str | None = None,
propagate_to_children: bool = True,
) -> None
Cancel the operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
propagate_to_children
|
bool
|
Whether to cancel child operations |
True
|
Source code in src/hother/cancelable/core/cancelable.py
Cancelation Tokens
CancelationToken
Thread-safe token for manual cancelation.
hother.cancelable.core.token.CancelationToken
Bases: BaseModel
Thread-safe cancelation token that can be shared across tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique token identifier |
is_cancelled |
bool
|
Whether the token has been cancelled |
reason |
CancelationReason | None
|
Reason for cancelation |
message |
str | None
|
Optional cancelation message |
cancelled_at |
datetime | None
|
When the token was cancelled |
Source code in src/hother/cancelable/core/token.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 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 | |
model_config
class-attribute
instance-attribute
model_config = ConfigDict(arbitrary_types_allowed=True)
cancel
async
cancel(
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> bool
Cancel the token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if token was cancelled, False if already cancelled |
Source code in src/hother/cancelable/core/token.py
cancel_sync
cancel_sync(
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> bool
Thread-safe synchronous cancelation from any thread.
This method can be called from regular Python threads (pynput, signal handlers, etc.) and will safely cancel the token and notify async waiters via the anyio bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if token was cancelled, False if already cancelled |
Example
Source code in src/hother/cancelable/core/token.py
wait_for_cancel
async
check
Check if cancelled and raise exception if so.
Raises:
| Type | Description |
|---|---|
ManualCancelation
|
If token is cancelled |
Source code in src/hother/cancelable/core/token.py
check_async
async
Async version of check that allows for proper async cancelation.
Raises:
| Type | Description |
|---|---|
CancelledError
|
If token is cancelled |
Source code in src/hother/cancelable/core/token.py
is_cancelation_requested
is_cancelation_requested() -> bool
Non-throwing check for cancelation.
Returns:
| Type | Description |
|---|---|
bool
|
True if cancelation has been requested |
register_callback
async
register_callback(
callback: Callable[[CancelationToken], Awaitable[None]],
) -> None
Register a callback to be called on cancelation.
The callback should accept the token as its only argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
Callable[[CancelationToken], Awaitable[None]]
|
Async callable that accepts the token |
required |
Source code in src/hother/cancelable/core/token.py
LinkedCancelationToken
Token that combines multiple cancelation tokens.
hother.cancelable.core.token.LinkedCancelationToken
Bases: CancelationToken
Cancelation token that can be linked to other tokens.
When any linked token is cancelled, this token is also cancelled.
Source code in src/hother/cancelable/core/token.py
link
async
link(
token: CancelationToken, preserve_reason: bool = False
) -> None
Link this token to another token.
When the linked token is cancelled, this token will also be cancelled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
CancelationToken
|
Token to link to |
required |
preserve_reason
|
bool
|
Whether to preserve the original cancelation reason |
False
|
Source code in src/hother/cancelable/core/token.py
Models and Status
OperationContext
Tracks the state and metadata of a cancelable operation.
hother.cancelable.core.models.OperationContext
Bases: BaseModel
Complete operation context with metadata and status tracking.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique operation identifier |
name |
str | None
|
Human-readable operation name |
status |
OperationStatus
|
Current operation status |
start_time |
datetime
|
When the operation started |
end_time |
datetime | None
|
When the operation ended (if applicable) |
cancel_reason |
CancelationReason | None
|
Reason for cancelation (if cancelled) |
cancel_message |
str | None
|
Additional cancelation message |
error |
str | None
|
Error message (if failed) |
partial_result |
Any | None
|
Any partial results before cancelation |
metadata |
dict[str, Any]
|
Additional operation metadata |
parent_id |
str | None
|
Parent operation ID (for nested operations) |
child_ids |
set[str]
|
Set of child operation IDs |
Source code in src/hother/cancelable/core/models.py
38 39 40 41 42 43 44 45 46 47 48 49 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 140 141 | |
model_config
class-attribute
instance-attribute
model_config = ConfigDict(arbitrary_types_allowed=True)
start_time
class-attribute
instance-attribute
log_context
Get context dict for structured logging.
Source code in src/hother/cancelable/core/models.py
update_status
update_status(status: OperationStatus) -> None
Update operation status with appropriate logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
OperationStatus
|
New operation status |
required |
Source code in src/hother/cancelable/core/models.py
OperationStatus
Enumeration of possible operation states.
hother.cancelable.core.models.OperationStatus
Operation lifecycle status.
Source code in src/hother/cancelable/core/models.py
CancelationReason
Enumeration of cancelation reason categories.
hother.cancelable.core.models.CancelationReason
Reason for cancelation.
Source code in src/hother/cancelable/core/models.py
Registry
OperationRegistry
Global registry for tracking active cancelable operations.
hother.cancelable.core.registry.OperationRegistry
Singleton registry for tracking all cancelable operations.
Provides centralized management and monitoring of operations across the application.
Source code in src/hother/cancelable/core/registry.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 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 | |
get_instance
classmethod
get_instance() -> OperationRegistry
Get singleton instance of the registry.
Returns:
| Type | Description |
|---|---|
OperationRegistry
|
The global OperationRegistry instance |
Source code in src/hother/cancelable/core/registry.py
register
async
register(operation: Cancelable) -> None
Register an operation with the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
Cancelable
|
Cancelable operation to register |
required |
Source code in src/hother/cancelable/core/registry.py
unregister
async
unregister(operation_id: str) -> None
Unregister an operation and add to history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
ID of operation to unregister |
required |
Source code in src/hother/cancelable/core/registry.py
get_operation
async
get_operation(operation_id: str) -> Optional[Cancelable]
Get operation by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
Operation ID to look up |
required |
Returns:
| Type | Description |
|---|---|
Optional[Cancelable]
|
Cancelable operation or None if not found |
Source code in src/hother/cancelable/core/registry.py
list_operations
async
list_operations(
status: OperationStatus | None = None,
parent_id: str | None = None,
name_pattern: str | None = None,
) -> list[OperationContext]
List operations with optional filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
OperationStatus | None
|
Filter by operation status |
None
|
parent_id
|
str | None
|
Filter by parent operation ID |
None
|
name_pattern
|
str | None
|
Filter by name (substring match) |
None
|
Returns:
| Type | Description |
|---|---|
list[OperationContext]
|
List of matching operation contexts |
Source code in src/hother/cancelable/core/registry.py
cancel_operation
async
cancel_operation(
operation_id: str,
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> bool
Cancel a specific operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
ID of operation to cancel |
required |
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if operation was cancelled, False if not found |
Source code in src/hother/cancelable/core/registry.py
cancel_all
async
cancel_all(
status: OperationStatus | None = None,
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> int
Cancel all operations with optional status filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
OperationStatus | None
|
Only cancel operations with this status |
None
|
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of operations cancelled |
Source code in src/hother/cancelable/core/registry.py
get_history
async
get_history(
limit: int | None = None,
status: OperationStatus | None = None,
since: datetime | None = None,
) -> list[OperationContext]
Get operation history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Maximum number of operations to return |
None
|
status
|
OperationStatus | None
|
Filter by final status |
None
|
since
|
datetime | None
|
Only return operations completed after this time |
None
|
Returns:
| Type | Description |
|---|---|
list[OperationContext]
|
List of historical operation contexts |
Source code in src/hother/cancelable/core/registry.py
cleanup_completed
async
Clean up completed operations from active tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
older_than
|
timedelta | None
|
Only cleanup operations older than this |
None
|
keep_failed
|
bool
|
Whether to keep failed operations |
True
|
Returns:
| Type | Description |
|---|---|
int
|
Number of operations cleaned up |
Source code in src/hother/cancelable/core/registry.py
get_statistics
async
Get registry statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with operation statistics |
Source code in src/hother/cancelable/core/registry.py
clear_all
async
Clear all operations and history (for testing).
Source code in src/hother/cancelable/core/registry.py
get_operation_sync
get_operation_sync(
operation_id: str,
) -> Optional[Cancelable]
Get operation by ID (thread-safe, synchronous).
This method can be called from any thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
Operation ID to look up |
required |
Returns:
| Type | Description |
|---|---|
Optional[Cancelable]
|
Cancelable operation or None if not found |
Source code in src/hother/cancelable/core/registry.py
list_operations_sync
list_operations_sync(
status: OperationStatus | None = None,
parent_id: str | None = None,
name_pattern: str | None = None,
) -> list[OperationContext]
List operations with optional filtering (thread-safe, synchronous).
This method can be called from any thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
OperationStatus | None
|
Filter by operation status |
None
|
parent_id
|
str | None
|
Filter by parent operation ID |
None
|
name_pattern
|
str | None
|
Filter by name (substring match) |
None
|
Returns:
| Type | Description |
|---|---|
list[OperationContext]
|
List of matching operation contexts |
Source code in src/hother/cancelable/core/registry.py
get_statistics_sync
Get registry statistics (thread-safe, synchronous).
This method can be called from any thread.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with operation statistics |
Source code in src/hother/cancelable/core/registry.py
get_history_sync
get_history_sync(
limit: int | None = None,
status: OperationStatus | None = None,
since: datetime | None = None,
) -> list[OperationContext]
Get operation history (thread-safe, synchronous).
This method can be called from any thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int | None
|
Maximum number of operations to return |
None
|
status
|
OperationStatus | None
|
Filter by final status |
None
|
since
|
datetime | None
|
Only return operations completed after this time |
None
|
Returns:
| Type | Description |
|---|---|
list[OperationContext]
|
List of historical operation contexts |
Source code in src/hother/cancelable/core/registry.py
cancel_operation_sync
cancel_operation_sync(
operation_id: str,
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> None
Cancel a specific operation (thread-safe, asynchronous execution).
This method can be called from any thread. It schedules the cancelation to be executed asynchronously and returns immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_id
|
str
|
ID of operation to cancel |
required |
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Note
The cancelation is scheduled via AnyioBridge and executes asynchronously. This method returns immediately without waiting for completion.
Source code in src/hother/cancelable/core/registry.py
cancel_all_sync
cancel_all_sync(
status: OperationStatus | None = None,
reason: CancelationReason = MANUAL,
message: str | None = None,
) -> None
Cancel all operations (thread-safe, asynchronous execution).
This method can be called from any thread. It schedules the cancelation to be executed asynchronously and returns immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
OperationStatus | None
|
Only cancel operations with this status |
None
|
reason
|
CancelationReason
|
Reason for cancelation |
MANUAL
|
message
|
str | None
|
Optional cancelation message |
None
|
Note
The cancelation is scheduled via AnyioBridge and executes asynchronously. This method returns immediately without waiting for completion.
Source code in src/hother/cancelable/core/registry.py
Exceptions
All exception classes used by the cancelation system.
hother.cancelable.core.exceptions
Custom exceptions for the async cancelation system.
CancelationError
Bases: Exception
Base exception for cancelation-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
reason |
The reason for cancelation |
|
message |
Optional cancelation message |
|
context |
Optional operation context |
Source code in src/hother/cancelable/core/exceptions.py
TimeoutCancelation
Bases: CancelationError
Operation cancelled due to timeout.
Source code in src/hother/cancelable/core/exceptions.py
ManualCancelation
Bases: CancelationError
Operation cancelled manually via token or API.
Source code in src/hother/cancelable/core/exceptions.py
SignalCancelation
Bases: CancelationError
Operation cancelled by system signal.
Source code in src/hother/cancelable/core/exceptions.py
ConditionCancelation
Bases: CancelationError
Operation cancelled by condition check.
Source code in src/hother/cancelable/core/exceptions.py
ParentCancelation
Bases: CancelationError
Operation cancelled because parent was cancelled.