| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,10 +67,22 @@ Run an event loop | |||
| 67 | 67 | This is idempotent and irreversible. No other methods should be called after | |
| 68 | 68 | this one. | |
| 69 | 69 | ||
| 70 | + .. _asyncio-pass-keywords: | ||
| 70 | 71 | ||
| 71 | 72 | Calls | |
| 72 | 73 | ----- | |
| 73 | 74 | ||
| 75 | + Most :mod:`asyncio` functions don't accept keywords. If you want to pass | ||
| 76 | + keywords to your callback, use :func:`functools.partial`. For example, | ||
| 77 | + ``loop.call_soon(functools.partial(print, "Hello", flush=True))`` will call | ||
| 78 | + ``print("Hello", flush=True)``. | ||
| 79 | + | ||
| 80 | + .. note:: | ||
| 81 | + :func:`functools.partial` is better than ``lambda`` functions, because | ||
| 82 | + :mod:`asyncio` can inspect :func:`functools.partial` object to display | ||
| 83 | + parameters in debug mode, whereas ``lambda`` functions have a poor | ||
| 84 | + representation. | ||
| 85 | + | ||
| 74 | 86 | .. method:: BaseEventLoop.call_soon(callback, \*args) | |
| 75 | 87 | ||
| 76 | 88 | Arrange for a callback to be called as soon as possible. | |
@@ -83,6 +95,9 @@ Calls | |||
| 83 | 95 | ||
| 84 | 96 | An instance of :class:`asyncio.Handle` is returned. | |
| 85 | 97 | ||
| 98 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 99 | + <asyncio-pass-keywords>`. | ||
| 100 | + | ||
| 86 | 101 | .. method:: BaseEventLoop.call_soon_threadsafe(callback, \*args) | |
| 87 | 102 | ||
| 88 | 103 | Like :meth:`call_soon`, but thread safe. | |
@@ -118,6 +133,9 @@ a different clock than :func:`time.time`. | |||
| 118 | 133 | is called. If you want the callback to be called with some named | |
| 119 | 134 | arguments, use a closure or :func:`functools.partial`. | |
| 120 | 135 | ||
| 136 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 137 | + <asyncio-pass-keywords>`. | ||
| 138 | + | ||
| 121 | 139 | .. method:: BaseEventLoop.call_at(when, callback, *args) | |
| 122 | 140 | ||
| 123 | 141 | Arrange for the *callback* to be called at the given absolute timestamp | |
@@ -126,6 +144,9 @@ a different clock than :func:`time.time`. | |||
| 126 | 144 | ||
| 127 | 145 | This method's behavior is the same as :meth:`call_later`. | |
| 128 | 146 | ||
| 147 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 148 | + <asyncio-pass-keywords>`. | ||
| 149 | + | ||
| 129 | 150 | .. method:: BaseEventLoop.time() | |
| 130 | 151 | ||
| 131 | 152 | Return the current time, as a :class:`float` value, according to the | |
@@ -334,6 +355,9 @@ On Windows with :class:`ProactorEventLoop`, these methods are not supported. | |||
| 334 | 355 | Start watching the file descriptor for read availability and then call the | |
| 335 | 356 | *callback* with specified arguments. | |
| 336 | 357 | ||
| 358 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 359 | + <asyncio-pass-keywords>`. | ||
| 360 | + | ||
| 337 | 361 | .. method:: BaseEventLoop.remove_reader(fd) | |
| 338 | 362 | ||
| 339 | 363 | Stop watching the file descriptor for read availability. | |
@@ -343,6 +367,9 @@ On Windows with :class:`ProactorEventLoop`, these methods are not supported. | |||
| 343 | 367 | Start watching the file descriptor for write availability and then call the | |
| 344 | 368 | *callback* with specified arguments. | |
| 345 | 369 | ||
| 370 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 371 | + <asyncio-pass-keywords>`. | ||
| 372 | + | ||
| 346 | 373 | .. method:: BaseEventLoop.remove_writer(fd) | |
| 347 | 374 | ||
| 348 | 375 | Stop watching the file descriptor for write availability. | |
@@ -493,6 +520,9 @@ Availability: UNIX only. | |||
| 493 | 520 | Raise :exc:`ValueError` if the signal number is invalid or uncatchable. | |
| 494 | 521 | Raise :exc:`RuntimeError` if there is a problem setting up the handler. | |
| 495 | 522 | ||
| 523 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 524 | + <asyncio-pass-keywords>`. | ||
| 525 | + | ||
| 496 | 526 | .. method:: BaseEventLoop.remove_signal_handler(sig) | |
| 497 | 527 | ||
| 498 | 528 | Remove a handler for a signal. | |
@@ -518,6 +548,9 @@ pool of processes). By default, an event loop uses a thread pool executor | |||
| 518 | 548 | The *executor* argument should be an :class:`~concurrent.futures.Executor` | |
| 519 | 549 | instance. The default executor is used if *executor* is ``None``. | |
| 520 | 550 | ||
| 551 | + :ref:`Use functools.partial to pass keywords to the callback | ||
| 552 | + <asyncio-pass-keywords>`. | ||
| 553 | + | ||
| 521 | 554 | This method is a :ref:`coroutine <coroutine>`. | |
| 522 | 555 | ||
| 523 | 556 | .. method:: BaseEventLoop.set_default_executor(executor) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -253,6 +253,11 @@ Future | |||
| 253 | 253 | future is already done when this is called, the callback is scheduled | |
| 254 | 254 | with :meth:`~BaseEventLoop.call_soon`. | |
| 255 | 255 | ||
| 256 | + :ref:`Use functools.partial to pass parameters to the callback | ||
| 257 | + <asyncio-pass-keywords>`. For example, | ||
| 258 | + ``fut.add_done_callback(functools.partial(print, "Future:", | ||
| 259 | + flush=True))`` will call ``print("Future:", fut, flush=True)``. | ||
| 260 | + | ||
| 256 | 261 | .. method:: remove_done_callback(fn) | |
| 257 | 262 | ||
| 258 | 263 | Remove all instances of a callback from the "call when done" list. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments