caller
Classes:
-
PendingGroup–An asynchronous context manager for tracking
Pendingthat are created in it's context. -
Caller–A thread-local class that facilitates inter-thread function and coroutine scheduling in asynchronous backends (asyncio or trio).
PendingGroup
¶
Bases: PendingTracker, AsyncContextManagerMixin
An asynchronous context manager for tracking Pending that are created in it's context.
Usage
Enter the async context and create new pending.
async with PendingGroup() as pg:
assert pg.caller.to_thread(lambda: None) in pg.pending
- Reference
-
Reference
caller
Callercreate_pending_group
Methods:
-
__init__–An async context to capture all pending (that opt in) created in the context.
-
cancel–Cancel the pending group (internally synchronised).
-
cancelled–Returns: If the pending group is marked as cancelled.
Attributes:
-
cancellation_timeout–The maximum time to wait for cancelled pending to be done.
-
caller(Fixed[Self, Caller]) –The caller where the pending group was instantiated.
Source code in src/async_kernel/pending.py
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 | |
cancellation_timeout
class-attribute
instance-attribute
¶
cancellation_timeout = 10
The maximum time to wait for cancelled pending to be done.
caller
class-attribute
instance-attribute
¶
The caller where the pending group was instantiated.
__init__
¶
The pending group will only exit once all pending in the group are done.
Pending can be added to and removed from the group manually.
Parameters:
-
(shield¶bool, default:False) –Passed to the cancel scope.
-
(mode¶int, default:0) –The mode. - 0: Ignore cancellation of pending. - 1: Cancel if any pending is cancelled - raise PendingCancelled on exit. - 2: Cancel if any pending is cancelled - exit quietly.
Source code in src/async_kernel/pending.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
cancel
¶
Cancel the pending group (internally synchronised).
Source code in src/async_kernel/pending.py
295 296 297 298 299 300 301 302 | |
Caller
¶
Bases: AsyncContextManagerMixin
A thread-local class that facilitates inter-thread function and coroutine scheduling in asynchronous backends (asyncio or trio).
- CPython: there is only one caller instance per thread.
- Pyodide: Multiple caller instances can exist in the same thread, but is limited to one instance per context.
Multi-eventloop management is supported including:
- zero or one host gui event loop.
- one or two backends.
Code execution is always done within the context of an asynchronous backend.
High level methods
- Caller.call_soon: Schedule a function call in the caller's thread.
- Caller.call_later: Schedule a function call in the caller's thread after a delay.
- Caller.to_thread: Schedule a function call using a worker caller (separate thread).
- Caller.call_using_backend: Schedule a function call using the backend in the caller's thread.
- Caller.as_completed: An async iterator to access pending as they complete.
- Caller.wait: A method to wait for pending to complete with a timeout.
- Caller.create_pending_group: Create a new pending group to use as an asynchronous context.
- Caller.get: Get a new caller instance (child).
Low level methods
- Caller.schedule_call: Schedule a function call in the caller's thread - configurable (used by high level methods).
- Caller.call_direct: Call a function directly in the scheduler in the caller's thread.
- Caller.queue_call: Execute a function in the caller's thread using a queue (sequential).
- Caller.queue_get: Get the pending associated with the queue call.
- Caller.queue_close: Close the queue associated with the function.
Class methods
- Caller.current_pending: Get the active pending in the current context.
- Caller.id_current: Get the id of the caller in the current thread/context.
- Caller.get_existing: Get the caller by id.
- Caller.all_callers: Get a list of all callers.
- Reference
- Reference
- Reference
Methods:
-
__new__–Create or retrieve a Caller instance.
-
start_sync–Start synchronously.
-
stop–Stop the caller cancelling all incomplete tasks.
-
__asynccontextmanager__–The asynchronous context for caller.
-
id_current–The id that is used for a caller for the current thread in CPython or context in Pyodide.
-
get_existing–A classmethod to get the caller instance from the corresponding thread if it exists.
-
current_pending–A classmethod that returns the current pending when called from inside a function scheduled by Caller.
-
all_callers–A classmethod to get a list of the callers.
-
get–Retrieves an existing child caller by name and backend, or creates a new one if not found.
-
schedule_call–Schedule
functo be called inside a task running in the caller's thread. -
call_later–Schedule func to be called in caller's event loop copying the current context.
-
call_soon–Schedule func to be executed.
-
call_using_backend–Schedule func to be executed using the specified backend.
-
call_direct–Schedule
functo be called in caller's event loop directly. -
to_thread–Call func in a worker thread using the same backend as the current instance.
-
queue_get–Returns
Pendinginstance forfuncwhere the queue is running. -
queue_call–A low-level function to queue the execution of
funcin a queue unique to it and the caller instance. -
queue_close–Close the execution queue associated with
func. -
as_completed–An iterator to get result as they complete.
-
wait–Wait for the results given by items to complete.
-
create_pending_group–Create a new PendingGroup instance.
Attributes:
-
MAX_IDLE_POOL_INSTANCES–The number of
poolinstances to leave idle (See also to_thread). -
IDLE_WORKER_SHUTDOWN_DURATION–The minimum duration in seconds for a worker to remain in the worker pool before it is shutdown.
-
stopped–An event that is set when the caller has stopped.
-
log(LoggerAdapter[Any]) – -
iopub_sockets(dict[int, Socket]) – -
iopub_url(ClassVar) – -
name(str) –The name of the thread when the caller was created.
-
id(int) –The id for the caller.
-
backend(Backend) –The backend used by caller.
-
backend_options(dict | None) –Options used to create the backend.
-
host(Hosts | None) –The name of the gui event loop if there is one.
-
host_options(dict | None) –Options used to create the gui event loop.
-
protected(bool) –Returns
Trueif the caller is protected from stopping. -
zmq_context(Context | None) –A zmq socket, which if present indicates that an iopub socket is loaded.
-
running(bool) –Returns
Truewhen the caller is available to run requests. -
children(frozenset[Self]) –A frozenset copy of the instances that were created by the caller.
-
thread(Thread) –The thread where the caller is running.
-
parent(Self | None) –The parent caller if it exists.
Source code in src/async_kernel/caller.py
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 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 | |
MAX_IDLE_POOL_INSTANCES
class-attribute
instance-attribute
¶
MAX_IDLE_POOL_INSTANCES = 10
The number of pool instances to leave idle (See also to_thread).
IDLE_WORKER_SHUTDOWN_DURATION
class-attribute
instance-attribute
¶
IDLE_WORKER_SHUTDOWN_DURATION = 0 if 'pytest' in modules else 60
The minimum duration in seconds for a worker to remain in the worker pool before it is shutdown.
Set to 0 to disable (default when running tests).
stopped
class-attribute
instance-attribute
¶
An event that is set when the caller has stopped.
zmq_context
property
¶
zmq_context: Context | None
A zmq socket, which if present indicates that an iopub socket is loaded.
children
property
¶
A frozenset copy of the instances that were created by the caller.
Notes
- When the parent is stopped, all children are stopped.
- All children are stopped prior to the parent exiting its async context.
__new__
¶
__new__(
modifier: Literal[
"CurrentThread", "MainThread", "NewThread", "manual"
] = "CurrentThread",
/,
**kwargs: Unpack[CallerCreateOptions],
) -> Self
Create or retrieve a Caller instance.
Parameters:
-
(modifier¶Literal['CurrentThread', 'MainThread', 'NewThread', 'manual'], default:'CurrentThread') –Specifies how the Caller instance should be created or retrieved.
- "CurrentThread": Automatically create or retrieve the instance.
- "MainThread": Use the main thread for the Caller.
- "NewThread": Create a new thread.
- "manual": Manually create a new instance for the current thread.
-
(**kwargs¶Unpack[CallerCreateOptions], default:{}) –Additional options for Caller creation, such as: - name: The name to use. - backend: The async backend to use. - backend_options: Options for the backend. - protected: Whether the Caller is protected. - zmq_context: ZeroMQ context. - log: Logger instance.
Returns:
-
Self(Self) –The created or retrieved Caller instance.
Raises:
-
RuntimeError–If the backend is not provided and backend can't be determined.
-
ValueError–If the thread and caller's name do not match.
Source code in src/async_kernel/caller.py
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 | |
start_sync
¶
Start synchronously.
Parameters:
Source code in src/async_kernel/caller.py
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 | |
stop
¶
stop(*, force: bool = False) -> CallerState
Stop the caller cancelling all incomplete tasks.
Parameters:
Source code in src/async_kernel/caller.py
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 | |
__asynccontextmanager__
async
¶
__asynccontextmanager__() -> AsyncGenerator[Self]
The asynchronous context for caller.
Source code in src/async_kernel/caller.py
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 | |
id_current
classmethod
¶
id_current() -> int
The id that is used for a caller for the current thread in CPython or context in Pyodide.
Source code in src/async_kernel/caller.py
652 653 654 655 656 657 | |
get_existing
classmethod
¶
A classmethod to get the caller instance from the corresponding thread if it exists.
Parameters:
-
(caller_id¶int | None, default:None) –The id of the caller which in CPython is also the the id of the thread in which it is running.
Source code in src/async_kernel/caller.py
659 660 661 662 663 664 665 666 667 668 669 | |
current_pending
classmethod
¶
A classmethod that returns the current pending when called from inside a function scheduled by Caller.
Source code in src/async_kernel/caller.py
671 672 673 674 | |
all_callers
classmethod
¶
all_callers(running_only: bool = True) -> list[Caller]
A classmethod to get a list of the callers.
Parameters:
-
(running_only¶bool, default:True) –Restrict the list to callers that are active (running in an async context).
Source code in src/async_kernel/caller.py
676 677 678 679 680 681 682 683 684 | |
get
¶
get(**kwargs: Unpack[CallerCreateOptions]) -> Self
Retrieves an existing child caller by name and backend, or creates a new one if not found.
Parameters:
-
(**kwargs¶Unpack[CallerCreateOptions], default:{}) –Options for creating or retrieving a caller instance.
Returns:
-
Self(Self) –The retrieved or newly created caller instance.
Raises:
-
RuntimeError–If a caller with the specified name exists but the backend does not match.
Notes
- The returned caller is added to
childrenand stopped with this instance. - If 'backend' or 'zmq_context' are not specified they are copied from this instance.
Source code in src/async_kernel/caller.py
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 | |
schedule_call
¶
schedule_call(
func: Callable[..., CoroutineType[Any, Any, T] | T],
args: tuple,
kwargs: dict,
context: Context | None = None,
trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = PendingTracker,
backend: NoValue | Backend = NoValue,
/,
**metadata: Any,
) -> Pending[T]
Schedule func to be called inside a task running in the caller's thread.
The methods call_soon and call_later use this method in the background, they should be used in preference to this method since they provide type hinting for the arguments.
Parameters:
-
(func¶Callable[..., CoroutineType[Any, Any, T] | T]) –The function to be called. If it returns a coroutine, it will be awaited and its result will be returned.
-
(args¶tuple) –Arguments corresponding to in the call to
func. -
(kwargs¶dict) –Keyword arguments to use with in the call to
func. -
(context¶Context | None, default:None) –The context to use, if not provided the current context is used.
-
(trackers¶type[PendingTracker] | tuple[type[PendingTracker], ...], default:PendingTracker) –The tracker subclasses of active trackers which to add the pending.
-
(**metadata¶Any, default:{}) –Additional metadata to store in the instance.
Source code in src/async_kernel/caller.py
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 | |
call_later
¶
call_later(
delay: float,
func: Callable[P, T | CoroutineType[Any, Any, T]],
/,
*args: args,
**kwargs: kwargs,
) -> Pending[T]
Schedule func to be called in caller's event loop copying the current context.
Parameters:
-
(func¶Callable[P, T | CoroutineType[Any, Any, T]]) –The function.
-
(delay¶float) –The minimum delay to add between submission and execution.
-
(*args¶args, default:()) –Arguments to use with
func. -
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with
func.
Info
All call arguments are packed into the instance's metadata.
Source code in src/async_kernel/caller.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | |
call_soon
¶
call_soon(
func: Callable[P, T | CoroutineType[Any, Any, T]], /, *args: args, **kwargs: kwargs
) -> Pending[T]
Schedule func to be executed.
Parameters:
-
(func¶Callable[P, T | CoroutineType[Any, Any, T]]) –The function.
-
(*args¶args, default:()) –Arguments to use with
func. -
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with
func.
Source code in src/async_kernel/caller.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
call_using_backend
¶
call_using_backend(
backend: Backend | Literal["asyncio", "trio"],
func: Callable[P, T | CoroutineType[Any, Any, T]],
/,
*args: args,
**kwargs: kwargs,
) -> Pending[T]
Schedule func to be executed using the specified backend.
This methods enables coroutines written for a specific function to be run irresective of the callers backend.
backend == caller.backend-funcis executed via Caller.call_soon.backend != caller.backend-funcis executed with a backend running as a guest.
Parameters:
-
(backend¶Backend | Literal['asyncio', 'trio']) –The backend in which
funcmust be executed. -
(func¶Callable[P, T | CoroutineType[Any, Any, T]]) –The function.
-
(*args¶args, default:()) –Arguments to use with
func. -
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with
func.
Notes:
- **Only use this to execute coroutines that require a specific backend to run in the caller's thread.**
- Where possible use a separate caller/thread with [Caller.get][] instead.
Source code in src/async_kernel/caller.py
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 | |
call_direct
¶
call_direct(
func: Callable[P, T | CoroutineType[Any, Any, T]], /, *args: args, **kwargs: kwargs
) -> None
Schedule func to be called in caller's event loop directly.
Use this for short-running function calls only.
Parameters:
-
(func¶Callable[P, T | CoroutineType[Any, Any, T]]) –The function.
-
(*args¶args, default:()) –Arguments to use with
func. -
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with
func.
Warning:
**Use this method for lightweight calls only!**
Source code in src/async_kernel/caller.py
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |
to_thread
¶
to_thread(
func: Callable[P, T | CoroutineType[Any, Any, T]], /, *args: args, **kwargs: kwargs
) -> Pending[T]
Call func in a worker thread using the same backend as the current instance.
Parameters:
-
(func¶Callable[P, T | CoroutineType[Any, Any, T]]) –The function.
-
(*args¶args, default:()) –Arguments to use with func.
-
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with func.
Notes
- A minimum number of caller instances are retained for this method.
- Async code run inside func should use taskgroups for creating task.
Source code in src/async_kernel/caller.py
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 | |
queue_get
¶
Returns Pending instance for func where the queue is running.
Notes
queue_closeis the preferred means to shutdown the queue.
Source code in src/async_kernel/caller.py
917 918 919 920 921 922 923 | |
queue_call
¶
A low-level function to queue the execution of func in a queue unique to it and the caller instance.
This sets up a long-lived task to provide a fast pathway for repetitive calls to a function where call order is also respected.
Parameters:
-
(func¶Callable[P, Any | Awaitable[Any]]) –The function.
-
(*args¶args, default:()) –Arguments to use with
func. -
(**kwargs¶kwargs, default:{}) –Keyword arguments to use with
func.
Notes
- The queue runs inside a pending that remains running until one of the following occurs:
- The queue is stopped.
- The method Caller.queue_close is called with
funcorfunc's hash. funcis deleted (utilising weakref.finalize).
- The context of the initial call is used for subsequent queue calls.
- Exceptions are logged to caller.log but not propagated.
- The pending created on the first call will only registered with PendingManager subclassed trackers and not PendingGroup.
Source code in src/async_kernel/caller.py
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 | |
queue_close
¶
Close the execution queue associated with func.
Parameters:
-
Reference
caller
Callerqueue_call
Source code in src/async_kernel/caller.py
980 981 982 983 984 985 986 987 988 989 990 | |
as_completed
async
¶
as_completed(
items: Iterable[Awaitable[T]] | AsyncGenerator[Awaitable[T]],
*,
max_concurrent: NoValue | int = NoValue,
cancel_unfinished: bool = True,
) -> AsyncGenerator[Pending[T], Any]
An iterator to get result as they complete.
Parameters:
-
(items¶Iterable[Awaitable[T]] | AsyncGenerator[Awaitable[T]]) –Either a container with existing results or generator of Pendings.
-
(max_concurrent¶NoValue | int, default:NoValue) –The maximum number of concurrent results to monitor at a time. This is useful when
itemsis a generator utilising Caller.to_thread. By default this will limit toCaller.MAX_IDLE_POOL_INSTANCES. -
(cancel_unfinished¶bool, default:True) –Cancel any
pendingwhen exiting.
Tip
- Pass a generator if you wish to limit the number result jobs when calling to_thread/to_task etc.
- Pass a container with all results when the limiter is not relevant.
Source code in src/async_kernel/caller.py
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 1073 1074 1075 1076 1077 1078 | |
wait
async
¶
wait(
items: Iterable[Awaitable[T]],
*,
timeout: float | None = None,
return_when: Literal[
"FIRST_COMPLETED", "FIRST_EXCEPTION", "ALL_COMPLETED"
] = "ALL_COMPLETED",
) -> tuple[set[Pending[T]], set[Pending[T]]]
Wait for the results given by items to complete.
Returns two sets of the results: (done, pending).
Parameters:
-
(items¶Iterable[Awaitable[T]]) –An iterable of results to wait for.
-
(timeout¶float | None, default:None) –The maximum time before returning.
-
(return_when¶Literal['FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED'], default:'ALL_COMPLETED') –The same options as available for asyncio.wait.
Example
done, pending = await asyncio.wait(items)
Info: - This does not raise a TimeoutError! - Pendings that aren't done when the timeout occurs are returned in the second set.
Source code in src/async_kernel/caller.py
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 | |
create_pending_group
¶
create_pending_group(*, shield: bool = False, mode: Literal[0, 1, 2] = 0) -> PendingGroup
Create a new PendingGroup instance.
The pending group registers all pending created in its context that opt in (via trackers). On exiting
the context, it will await all remaining pending to complete. The exit and cancellation behaviour of
the instance is a function of mode.
If any pending result in exception, the pending group and all registered pending are cancelled. If the pending group context is cancelled or results in exception, all pending in the group are also cancelled.
Parameters:
-
(shield¶bool, default:False) –Shield the pending group from external cancellation.
-
(mode¶Literal[0, 1, 2], default:0) –The mode. - 0: Ignore cancellation of pending. - 1: Cancel if any pending is cancelled - raise PendingCancelled on exit. - 2: Cancel if any pending is cancelled - exit quietly.
Usage:
```python
async with Caller().create_pending_group() as pg:
pg.caller.to_thread(my_func)
```
Source code in src/async_kernel/caller.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 | |