Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07ff92a

Browse files
committed
(Merge 3.4) asyncio doc: explain how to pass keywords to callbacks
(functools.partial)
2 parents 2292def + 8464c24 commit 07ff92a

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Doc/library/asyncio-eventloop.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,22 @@ Run an event loop
6767
This is idempotent and irreversible. No other methods should be called after
6868
this one.
6969

70+
.. _asyncio-pass-keywords:
7071

7172
Calls
7273
-----
7374

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+
7486
.. method:: BaseEventLoop.call_soon(callback, \*args)
7587

7688
Arrange for a callback to be called as soon as possible.
@@ -83,6 +95,9 @@ Calls
8395

8496
An instance of :class:`asyncio.Handle` is returned.
8597

98+
:ref:`Use functools.partial to pass keywords to the callback
99+
<asyncio-pass-keywords>`.
100+
86101
.. method:: BaseEventLoop.call_soon_threadsafe(callback, \*args)
87102

88103
Like :meth:`call_soon`, but thread safe.
@@ -118,6 +133,9 @@ a different clock than :func:`time.time`.
118133
is called. If you want the callback to be called with some named
119134
arguments, use a closure or :func:`functools.partial`.
120135

136+
:ref:`Use functools.partial to pass keywords to the callback
137+
<asyncio-pass-keywords>`.
138+
121139
.. method:: BaseEventLoop.call_at(when, callback, *args)
122140

123141
Arrange for the *callback* to be called at the given absolute timestamp
@@ -126,6 +144,9 @@ a different clock than :func:`time.time`.
126144

127145
This method's behavior is the same as :meth:`call_later`.
128146

147+
:ref:`Use functools.partial to pass keywords to the callback
148+
<asyncio-pass-keywords>`.
149+
129150
.. method:: BaseEventLoop.time()
130151

131152
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.
334355
Start watching the file descriptor for read availability and then call the
335356
*callback* with specified arguments.
336357

358+
:ref:`Use functools.partial to pass keywords to the callback
359+
<asyncio-pass-keywords>`.
360+
337361
.. method:: BaseEventLoop.remove_reader(fd)
338362

339363
Stop watching the file descriptor for read availability.
@@ -343,6 +367,9 @@ On Windows with :class:`ProactorEventLoop`, these methods are not supported.
343367
Start watching the file descriptor for write availability and then call the
344368
*callback* with specified arguments.
345369

370+
:ref:`Use functools.partial to pass keywords to the callback
371+
<asyncio-pass-keywords>`.
372+
346373
.. method:: BaseEventLoop.remove_writer(fd)
347374

348375
Stop watching the file descriptor for write availability.
@@ -493,6 +520,9 @@ Availability: UNIX only.
493520
Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
494521
Raise :exc:`RuntimeError` if there is a problem setting up the handler.
495522

523+
:ref:`Use functools.partial to pass keywords to the callback
524+
<asyncio-pass-keywords>`.
525+
496526
.. method:: BaseEventLoop.remove_signal_handler(sig)
497527

498528
Remove a handler for a signal.
@@ -518,6 +548,9 @@ pool of processes). By default, an event loop uses a thread pool executor
518548
The *executor* argument should be an :class:`~concurrent.futures.Executor`
519549
instance. The default executor is used if *executor* is ``None``.
520550

551+
:ref:`Use functools.partial to pass keywords to the callback
552+
<asyncio-pass-keywords>`.
553+
521554
This method is a :ref:`coroutine <coroutine>`.
522555

523556
.. method:: BaseEventLoop.set_default_executor(executor)

Doc/library/asyncio-task.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ Future
253253
future is already done when this is called, the callback is scheduled
254254
with :meth:`~BaseEventLoop.call_soon`.
255255

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+
256261
.. method:: remove_done_callback(fn)
257262

258263
Remove all instances of a callback from the "call when done" list.

0 commit comments

Comments
 (0)