event_loop
- Reference typing
Classes:
-
Host–A class that provides the necessary callbacks to run a gui event loop with a
backendstarted usingstart_guest_run.
Functions:
-
run–Run
functo completion asynchronously in the current thread using a backend with an optional host (gui event loop).
Host
¶
Bases: Generic[T]
A class that provides the necessary callbacks to run a gui event loop with a backend started using start_guest_run.
-
Reference
event_loop
run
-
Reference
event_loop
Hostcurrent
- Reference event_loop
Methods:
-
current–The host running in the corresponding thread or current thread.
-
run–Run the loop in the current thread with a backend guest.
-
mainloop–Start the main event loop of the host.
Attributes:
-
start_guest(Callable[[], Any]) –A callback to start the guest. This must be called by a subclass.
Source code in src/async_kernel/event_loop/run.py
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 | |
start_guest
class-attribute
instance-attribute
¶
start_guest: Callable[[], Any] = staticmethod(lambda: None)
A callback to start the guest. This must be called by a subclass.
current
classmethod
¶
The host running in the corresponding thread or current thread.
Source code in src/async_kernel/event_loop/run.py
95 96 97 98 99 | |
run
classmethod
¶
run(
func: Callable[..., CoroutineType[Any, Any, T]], args: tuple, settings: RunSettings
) -> T
Run the loop in the current thread with a backend guest.
Source code in src/async_kernel/event_loop/run.py
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 | |
mainloop
¶
mainloop() -> T
Start the main event loop of the host.
Source code in src/async_kernel/event_loop/run.py
159 160 161 162 163 164 165 | |
run
¶
run(
func: Callable[..., CoroutineType[Any, Any, T]], args: tuple, settings: RunSettings
) -> T
Run func to completion asynchronously in the current thread using a backend with an optional host (gui event loop).
The default backend is 'asyncio'.
If host is specified in settings. A host (gui) mainloop
will be started with the backend running as a guest (in the same thread). The backend
will execute func asynchronously to completion. Once completed the backend and host
are stopped and finally the result is returned.
Parameters:
-
(func¶Callable[..., CoroutineType[Any, Any, T]]) –A coroutine function.
-
(args¶tuple) –Args to use when calling func.
-
(settings¶RunSettings) –Settings to use when running func.
Custom host
A custom host can be started by subclassing Host and passed as the 'host_class' as the class or a dotted path if it is importable.
loop_factory
When there is no host and the backend is 'asyncio', the loop factory can be specified
in backend_options as a function or an importatable dotted path such as 'asyncio.new_event_loop'.
Source code in src/async_kernel/event_loop/run.py
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 | |
Bases: Host[T]
Methods:
-
run_sync_soon_threadsafe–Use Tcl "after" command to schedule a function call.
-
run_sync_soon_not_threadsafe–Use Tcl "after" command to schedule a function call from the main thread.
-
done_callback–End the Tk app.
Source code in src/async_kernel/event_loop/tk_host.py
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 | |
run_sync_soon_threadsafe
¶
Use Tcl "after" command to schedule a function call.
Based on tkinter source comments
the issuance of the tcl call to after itself is thread-safe since it is sent
to the appropriate thread on line 1522.
Tkapp_ThreadSend effectively uses "after 0" while putting the command in the
event queue so the "after idle after 0"` incantation
is unnecessary here.
Compare to tkthread where definitely thread unsafe eval is used to send thread safe signals between tcl interpreters.
Source code in src/async_kernel/event_loop/tk_host.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
run_sync_soon_not_threadsafe
¶
run_sync_soon_not_threadsafe(fn) -> None
Use Tcl "after" command to schedule a function call from the main thread.
If .call is called from the Tcl thread, the locking and sending are optimized away so it should be fast enough.
The incantation "after idle after 0"
avoids blocking the normal event queue when faced with an unending stream of tasks, for
example while True: await trio.sleep(0).
Source code in src/async_kernel/event_loop/tk_host.py
69 70 71 72 73 74 75 76 77 78 79 80 81 | |
done_callback
¶
done_callback(outcome) -> None
End the Tk app.
Source code in src/async_kernel/event_loop/tk_host.py
87 88 89 90 91 | |
options: show_root_heading: true
Bases: Host[T]
Source code in src/async_kernel/event_loop/qt_host.py
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 | |
options: show_root_heading: true
Defines a background ZMQ poller thread.
Classes:
-
ZMQPollSocket–A zmq socket which uses a ZMQPoll thread to perform sensitive operation.
-
ZMQPoll–A zmq_poll based event loop.
ZMQPollSocket
¶
Bases: Socket[bytes]
A zmq socket which uses a ZMQPoll thread to perform sensitive operation.
For best reliability, sensitive operations are performed in the zmq_poll's thread. This socket will close automatically when Poll is stopped. It is still better to close the socket when it is no longer required. The socket must be bound/connected in the context of the ZMQPoll to which it is associated.
-
Reference
event_loop
ZMQPoll
-
Reference
event_loop
ZMQPoll
-
Reference
event_loop
ZMQPoll
Attributes:
-
curve_secretkey(bytes | None) –The curve encryption secret key.
-
curve_publickey(bytes | None) –The curve encryption public key.
-
curve_server(bool) –If it is the server (must be bound).
-
curve_serverkey(bytes | None) –The key to use when the socket is not a serve (normally matches curve_publickey).
Source code in src/async_kernel/event_loop/zmq_poll.py
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 | |
ZMQPoll
¶
A zmq_poll based event loop.
When a (socket, flags) -> handler mapping context manager is used ZMQPoll.event_handler
handler is called with the socket and event when the event occurs on the socket. The
callback occurs in the zmq poll thread. async_kernel.caller.Caller is recommended to
to scheduling code execution in different threads.
Only ZMQPollSocket sockets created using the ZMQPoll.socket factory function are allowed.
The methods ZMQPoll.execute and the async version ZMQPoll.aexecute are provided to executed code in the zmq_poll thread, which is useful when creating and configuring sockets to reduce thread switching.
- Reference event_loop
Methods:
-
validate_socket–Check
sockis correctly registered. -
socket–Create a new ZMQPollSocket.
-
execute–Execute
funcin the 'zmq_poll' thread waiting for the result synchronously. -
aexecute–Execute
funcin the 'zmq_poll' thread waiting for the result asynchronously. -
event_handler–A context manager where
handleris called in the 'zmq_poll' thread with the event number when it occurs forsock.
Attributes:
-
stopped(Fixed[Self, ProtectedPending]) –Set when the poll thread event has stopped.
-
sockets(Fixed[Self, set[ZMQPollSocket]]) –The sockets currently registered with this instance.
Source code in src/async_kernel/event_loop/zmq_poll.py
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 | |
stopped
class-attribute
instance-attribute
¶
Set when the poll thread event has stopped.
sockets
class-attribute
instance-attribute
¶
The sockets currently registered with this instance.
validate_socket
¶
validate_socket(sock: ZMQPollSocket | Any) -> ZMQPollSocket
Check sock is correctly registered.
Source code in src/async_kernel/event_loop/zmq_poll.py
336 337 338 339 340 341 | |
socket
¶
socket(socket_type: SocketType) -> ZMQPollSocket
Create a new ZMQPollSocket.
Parameters:
-
(socket_type¶SocketType) –The type of socket.
-
Reference
event_loop
ZMQPoll
Source code in src/async_kernel/event_loop/zmq_poll.py
343 344 345 346 347 348 349 350 351 352 | |
execute
¶
execute(func: Callable[P, T], /, *args: args, **kwargs: kwargs) -> T
Execute func in the 'zmq_poll' thread waiting for the result synchronously.
-
Reference
event_loop
ZMQPoll
Source code in src/async_kernel/event_loop/zmq_poll.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
aexecute
async
¶
aexecute(func: Callable[P, T], /, *args: args, **kwargs: kwargs) -> T
Execute func in the 'zmq_poll' thread waiting for the result asynchronously.
-
Reference
event_loop
ZMQPoll
Source code in src/async_kernel/event_loop/zmq_poll.py
370 371 372 373 374 375 376 377 378 | |
event_handler
¶
event_handler(
sock: ZMQPollSocket,
handler: Callable[[ZMQPollSocket, int], Any],
/,
*,
flags: Literal[POLLIN, POLLOUT] = POLLIN,
count: tuple[int, Callable[[], Any]] | None = None,
canceller: Callable[[], Any] | NoValue | None = NoValue,
) -> Generator[None, Any, None]
A context manager where handler is called in the 'zmq_poll' thread with the event number when it occurs for sock.
Only one handler is allowed per (socket, flags) combination.
Parameters:
-
(sock¶ZMQPollSocket) –A registered ZMQPollSocket.
-
(handler¶Callable[[ZMQPollSocket, int], Any]) –A handler to handle the event. The handler is called inside the zmq_poll thread. Thread-safe primitives must be used by the handler such as async_kernel.caller.Caller.call_soon,async_kernel.caller.Caller.queue_call, etc.
-
(flags¶Literal[POLLIN, POLLOUT], default:POLLIN) –The type of event to listen for. zmq.PollEvent.POLLIN:
sockis readable. zmq.PollEvent.POLLOUT:sockwas read from. -
(count¶tuple[int, Callable[[], Any]] | None, default:None) –A tuple ('n', callback) where the handler is run exactly 'n' times. The callback could be an
event.setto release the context. -
(canceller¶Callable[[], Any] | NoValue | None, default:NoValue) –A callback to use on the event the poll is stopped. The default cancellation behavior is to cancel the pending returned by async_kernel.caller.Caller.current_pending. Set to
Noneto disable cancellation support. This is safe when this context manager is inside the context of this instance.
Raises:
-
BusyResourceError–If the
(sock, flags)combination is already in use. -
RuntimeError–If the default canceller can not be created.
Tip
The zmq_poll thread normally disables debugging in the zmq_poll thread so inserting breakpoints in the event handler may interfere with debugging.
-
Reference
event_loop
ZMQPoll
Source code in src/async_kernel/event_loop/zmq_poll.py
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 | |