blob: 19bf8a6e058b8863fdab102c5adcc8f5df48b612 [file] [log] [blame]
Yury Selivanov3faaa882018-09-14 13:32:07 -07001.. currentmodule:: asyncio
2
3
Yury Selivanov6c731642018-09-14 14:57:39 -07004.. _asyncio-futures:
5
Yury Selivanov3faaa882018-09-14 13:32:07 -07006=======
7Futures
8=======
9
10*Future* objects are used to bridge low-level callback-based code
11with high-level async/await code.
12
13
14Future Functions
15================
16
17.. function:: isfuture(obj)
18
19 Return ``True`` if *obj* is either of:
20
21 * an instance of :class:`asyncio.Future`,
22 * an instance of :class:`asyncio.Task`,
23 * a Future-like object with a ``_asyncio_future_blocking``
24 attribute.
25
26 .. versionadded:: 3.5
27
28
29.. function:: ensure_future(obj, \*, loop=None)
30
31 Return:
32
33 * *obj* argument as is, if *obj* is a :class:`Future`,
34 a :class:`Task`, or a Future-like object (:func:`isfuture`
35 is used for the test.)
36
37 * a :class:`Task` object wrapping *obj*, if *obj* is a
38 coroutine (:func:`iscoroutine` is used for the test.)
39
40 * a :class:`Task` object that would await on *obj*, if *obj* is an
41 awaitable (:func:`inspect.isawaitable` is used for the test.)
42
43 If *obj* is neither of the above a :exc:`TypeError` is raised.
44
45 .. important::
46
47 See also the :func:`create_task` function which is the
48 preferred way for creating new Tasks.
49
50 .. versionchanged:: 3.5.1
51 The function accepts any :term:`awaitable` object.
52
53
54.. function:: wrap_future(future, \*, loop=None)
55
56 Wrap a :class:`concurrent.futures.Future` object in a
57 :class:`asyncio.Future` object.
58
59
60Future Object
61=============
62
63.. class:: Future(\*, loop=None)
64
65 A Future represents an eventual result of an asynchronous
66 operation. Not thread-safe.
67
68 Future is an :term:`awaitable` object. Coroutines can await on
69 Future objects until they either have a result or an exception
70 set, or until they are cancelled.
71
72 Typically Futures are used to enable low-level
73 callback-based code (e.g. in protocols implemented using asyncio
74 :ref:`transports <asyncio-transports-protocols>`)
75 to interoperate with high-level async/await code.
76
77 The rule of thumb is to never expose Future objects in user-facing
78 APIs, and the recommended way to create a Future object is to call
79 :meth:`loop.create_future`. This way alternative event loop
80 implementations can inject their own optimized implementations
81 of a Future object.
82
83 .. versionchanged:: 3.7
84 Added support for the :mod:`contextvars` module.
85
86 .. method:: result()
87
88 Return the result of the Future.
89
90 If the Future is *done* and has a result set by the
91 :meth:`set_result` method, the result value is returned.
92
93 If the Future is *done* and has an exception set by the
94 :meth:`set_exception` method, this method raises the exception.
95
96 If the Future has been *cancelled*, this method raises
97 a :exc:`CancelledError` exception.
98
99 If the Future's result isn't yet available, this method raises
100 a :exc:`InvalidStateError` exception.
101
102 .. method:: set_result(result)
103
104 Mark the Future as *done* and set its result.
105
106 Raises a :exc:`InvalidStateError` error if the Future is
107 already *done*.
108
109 .. method:: set_exception(exception)
110
111 Mark the Future as *done* and set an exception.
112
113 Raises a :exc:`InvalidStateError` error if the Future is
114 already *done*.
115
116 .. method:: done()
117
118 Return ``True`` if the Future is *done*.
119
120 A Future is *done* if it was *cancelled* or if it has a result
121 or an exception set with :meth:`set_result` or
122 :meth:`set_exception` calls.
123
Yury Selivanov805e27e2018-09-14 16:57:11 -0700124 .. method:: cancelled()
125
126 Return ``True`` if the Future was *cancelled*.
127
128 The method is usually used to check if a Future is not
129 *cancelled* before setting a result or an exception for it::
130
131 if not fut.cancelled():
132 fut.set_result(42)
133
Yury Selivanov3faaa882018-09-14 13:32:07 -0700134 .. method:: add_done_callback(callback, *, context=None)
135
136 Add a callback to be run when the Future is *done*.
137
138 The *callback* is called with the Future object as its only
139 argument.
140
141 If the Future is already *done* when this method is called,
142 the callback is scheduled with :meth:`loop.call_soon`.
143
144 An optional keyword-only *context* argument allows specifying a
145 custom :class:`contextvars.Context` for the *callback* to run in.
146 The current context is used when no *context* is provided.
147
148 :func:`functools.partial` can be used to pass parameters
149 to the callback, e.g.::
150
151 # Call 'print("Future:", fut)' when "fut" is done.
152 fut.add_done_callback(
153 functools.partial(print, "Future:"))
154
155 .. versionchanged:: 3.7
156 The *context* keyword-only parameter was added.
157 See :pep:`567` for more details.
158
159 .. method:: remove_done_callback(callback)
160
161 Remove *callback* from the callbacks list.
162
163 Returns the number of callbacks removed, which is typically 1,
164 unless a callback was added more than once.
165
166 .. method:: cancel()
167
168 Cancel the Future and schedule callbacks.
169
170 If the Future is already *done* or *cancelled*, return ``False``.
171 Otherwise, change the Future's state to *cancelled*,
172 schedule the callbacks, and return ``True``.
173
174 .. method:: exception()
175
176 Return the exception that was set on this Future.
177
178 The exception (or ``None`` if no exception was set) is
179 returned only if the Future is *done*.
180
181 If the Future has been *cancelled*, this method raises a
182 :exc:`CancelledError` exception.
183
184 If the Future isn't *done* yet, this method raises an
185 :exc:`InvalidStateError` exception.
186
187 .. method:: get_loop()
188
189 Return the event loop the Future object is bound to.
190
191 .. versionadded:: 3.7
192
Yury Selivanov3faaa882018-09-14 13:32:07 -0700193
194This example creates a Future object, creates and schedules an
195asynchronous Task to set result for the Future, and waits until
196the Future has a result::
197
198 async def set_after(fut, delay, value):
199 # Sleep for *delay* seconds.
200 await asyncio.sleep(delay)
201
202 # Set *value* as a result of *fut* Future.
203 fut.set_result(value)
204
205 async def main():
206 # Get the current event loop.
207 loop = asyncio.get_running_loop()
208
209 # Create a new Future object.
210 fut = loop.create_future()
211
212 # Run "set_after()" coroutine in a parallel Task.
213 # We are using the low-level "loop.create_task()" API here because
214 # we already have a reference to the event loop at hand.
215 # Otherwise we could have just used "asyncio.create_task()".
216 loop.create_task(
217 set_after(fut, 1, '... world'))
218
219 print('hello ...')
220
221 # Wait until *fut* has a result (1 second) and print it.
222 print(await fut)
223
224 asyncio.run(main())
225
226
227.. important::
228
229 The Future object was designed to mimic
230 :class:`concurrent.futures.Future`. Key differences include:
231
232 - unlike asyncio Futures, :class:`concurrent.futures.Future`
233 instances cannot be awaited.
234
235 - :meth:`asyncio.Future.result` and :meth:`asyncio.Future.exception`
236 do not accept the *timeout* argument.
237
238 - :meth:`asyncio.Future.result` and :meth:`asyncio.Future.exception`
239 raise an :exc:`InvalidStateError` exception when the Future is not
240 *done*.
241
242 - Callbacks registered with :meth:`asyncio.Future.add_done_callback`
243 are not called immediately. They are scheduled with
244 :meth:`loop.call_soon` instead.
245
246 - asyncio Future is not compatible with the
247 :func:`concurrent.futures.wait` and
248 :func:`concurrent.futures.as_completed` functions.