Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 1 | """Support for tasks, coroutines and the scheduler.""" |
| 2 | |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 3 | __all__ = ( |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 4 | 'Task', 'create_task', |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 5 | 'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED', |
Yury Selivanov | 9edad3c | 2017-12-11 10:03:48 -0500 | [diff] [blame] | 6 | 'wait', 'wait_for', 'as_completed', 'sleep', |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 7 | 'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe', |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 8 | 'current_task', 'all_tasks', |
| 9 | '_register_task', '_unregister_task', '_enter_task', '_leave_task', |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 10 | ) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 11 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 12 | import concurrent.futures |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 13 | import contextvars |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 14 | import functools |
| 15 | import inspect |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 16 | import itertools |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 17 | import types |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 18 | import warnings |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 19 | import weakref |
| 20 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 21 | from . import base_tasks |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 22 | from . import coroutines |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 23 | from . import events |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 24 | from . import exceptions |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 25 | from . import futures |
Andrew Svetlov | 68b34a7 | 2019-05-16 17:52:10 +0300 | [diff] [blame] | 26 | from .coroutines import _is_coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 27 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 28 | # Helper to generate new task names |
| 29 | # This uses itertools.count() instead of a "+= 1" operation because the latter |
| 30 | # is not thread safe. See bpo-11866 for a longer explanation. |
| 31 | _task_name_counter = itertools.count(1).__next__ |
| 32 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 33 | |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 34 | def current_task(loop=None): |
| 35 | """Return a currently executed task.""" |
| 36 | if loop is None: |
| 37 | loop = events.get_running_loop() |
| 38 | return _current_tasks.get(loop) |
| 39 | |
| 40 | |
| 41 | def all_tasks(loop=None): |
| 42 | """Return a set of all tasks for the loop.""" |
| 43 | if loop is None: |
Yury Selivanov | 416c1eb | 2018-05-28 17:54:02 -0400 | [diff] [blame] | 44 | loop = events.get_running_loop() |
Andrew Svetlov | 65aa64f | 2019-06-11 18:27:30 +0300 | [diff] [blame] | 45 | # Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another |
| 46 | # thread while we do so. Therefore we cast it to list prior to filtering. The list |
| 47 | # cast itself requires iteration, so we repeat it several times ignoring |
| 48 | # RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for |
| 49 | # details. |
| 50 | i = 0 |
| 51 | while True: |
| 52 | try: |
| 53 | tasks = list(_all_tasks) |
| 54 | except RuntimeError: |
| 55 | i += 1 |
| 56 | if i >= 1000: |
| 57 | raise |
| 58 | else: |
| 59 | break |
| 60 | return {t for t in tasks |
Yury Selivanov | 416c1eb | 2018-05-28 17:54:02 -0400 | [diff] [blame] | 61 | if futures._get_loop(t) is loop and not t.done()} |
| 62 | |
| 63 | |
| 64 | def _all_tasks_compat(loop=None): |
| 65 | # Different from "all_task()" by returning *all* Tasks, including |
| 66 | # the completed ones. Used to implement deprecated "Tasks.all_task()" |
| 67 | # method. |
| 68 | if loop is None: |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 69 | loop = events.get_event_loop() |
Andrew Svetlov | 65aa64f | 2019-06-11 18:27:30 +0300 | [diff] [blame] | 70 | # Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another |
| 71 | # thread while we do so. Therefore we cast it to list prior to filtering. The list |
| 72 | # cast itself requires iteration, so we repeat it several times ignoring |
| 73 | # RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for |
| 74 | # details. |
| 75 | i = 0 |
| 76 | while True: |
| 77 | try: |
| 78 | tasks = list(_all_tasks) |
| 79 | except RuntimeError: |
| 80 | i += 1 |
| 81 | if i >= 1000: |
| 82 | raise |
| 83 | else: |
| 84 | break |
| 85 | return {t for t in tasks if futures._get_loop(t) is loop} |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 86 | |
| 87 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 88 | def _set_task_name(task, name): |
| 89 | if name is not None: |
| 90 | try: |
| 91 | set_name = task.set_name |
| 92 | except AttributeError: |
| 93 | pass |
| 94 | else: |
| 95 | set_name(name) |
| 96 | |
| 97 | |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 98 | class Task(futures._PyFuture): # Inherit Python Task implementation |
| 99 | # from a Python Future implementation. |
| 100 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 101 | """A coroutine wrapped in a Future.""" |
| 102 | |
| 103 | # An important invariant maintained while a Task not done: |
| 104 | # |
| 105 | # - Either _fut_waiter is None, and _step() is scheduled; |
| 106 | # - or _fut_waiter is some Future, and _step() is *not* scheduled. |
| 107 | # |
| 108 | # The only transition from the latter to the former is through |
| 109 | # _wakeup(). When _fut_waiter is not None, one of its callbacks |
| 110 | # must be _wakeup(). |
| 111 | |
Victor Stinner | fe22e09 | 2014-12-04 23:00:13 +0100 | [diff] [blame] | 112 | # If False, don't log a message if the task is destroyed whereas its |
| 113 | # status is still pending |
| 114 | _log_destroy_pending = True |
| 115 | |
Guido van Rossum | 1a605ed | 2013-12-06 12:57:40 -0800 | [diff] [blame] | 116 | @classmethod |
| 117 | def current_task(cls, loop=None): |
| 118 | """Return the currently running task in an event loop or None. |
| 119 | |
| 120 | By default the current task for the current event loop is returned. |
| 121 | |
| 122 | None is returned when called not in the context of a Task. |
| 123 | """ |
Matthias Bussonnier | d0ebf13 | 2019-05-20 23:20:10 -0700 | [diff] [blame] | 124 | warnings.warn("Task.current_task() is deprecated since Python 3.7, " |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 125 | "use asyncio.current_task() instead", |
Inada Naoki | c5c6cda | 2019-03-22 20:07:32 +0900 | [diff] [blame] | 126 | DeprecationWarning, |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 127 | stacklevel=2) |
Guido van Rossum | 1a605ed | 2013-12-06 12:57:40 -0800 | [diff] [blame] | 128 | if loop is None: |
| 129 | loop = events.get_event_loop() |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 130 | return current_task(loop) |
Guido van Rossum | 1a605ed | 2013-12-06 12:57:40 -0800 | [diff] [blame] | 131 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 132 | @classmethod |
| 133 | def all_tasks(cls, loop=None): |
| 134 | """Return a set of all tasks for an event loop. |
| 135 | |
| 136 | By default all tasks for the current event loop are returned. |
| 137 | """ |
Matthias Bussonnier | d0ebf13 | 2019-05-20 23:20:10 -0700 | [diff] [blame] | 138 | warnings.warn("Task.all_tasks() is deprecated since Python 3.7, " |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 139 | "use asyncio.all_tasks() instead", |
Inada Naoki | c5c6cda | 2019-03-22 20:07:32 +0900 | [diff] [blame] | 140 | DeprecationWarning, |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 141 | stacklevel=2) |
Yury Selivanov | 416c1eb | 2018-05-28 17:54:02 -0400 | [diff] [blame] | 142 | return _all_tasks_compat(loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 143 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 144 | def __init__(self, coro, *, loop=None, name=None): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 145 | super().__init__(loop=loop) |
Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 146 | if self._source_traceback: |
| 147 | del self._source_traceback[-1] |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 148 | if not coroutines.iscoroutine(coro): |
| 149 | # raise after Future.__init__(), attrs are required for __del__ |
| 150 | # prevent logging for pending task in __del__ |
| 151 | self._log_destroy_pending = False |
| 152 | raise TypeError(f"a coroutine was expected, got {coro!r}") |
| 153 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 154 | if name is None: |
| 155 | self._name = f'Task-{_task_name_counter()}' |
| 156 | else: |
| 157 | self._name = str(name) |
| 158 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 159 | self._must_cancel = False |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 160 | self._fut_waiter = None |
| 161 | self._coro = coro |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 162 | self._context = contextvars.copy_context() |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 163 | |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 164 | self._loop.call_soon(self.__step, context=self._context) |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 165 | _register_task(self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 166 | |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 167 | def __del__(self): |
| 168 | if self._state == futures._PENDING and self._log_destroy_pending: |
| 169 | context = { |
| 170 | 'task': self, |
| 171 | 'message': 'Task was destroyed but it is pending!', |
| 172 | } |
| 173 | if self._source_traceback: |
| 174 | context['source_traceback'] = self._source_traceback |
| 175 | self._loop.call_exception_handler(context) |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 176 | super().__del__() |
Victor Stinner | a02f81f | 2014-06-24 22:37:53 +0200 | [diff] [blame] | 177 | |
Batuhan Taşkaya | dec3672 | 2019-12-07 14:05:07 +0300 | [diff] [blame] | 178 | def __class_getitem__(cls, type): |
| 179 | return cls |
| 180 | |
Victor Stinner | 313a980 | 2014-07-29 12:58:23 +0200 | [diff] [blame] | 181 | def _repr_info(self): |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 182 | return base_tasks._task_repr_info(self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 183 | |
Alex Grönholm | 98ef920 | 2019-05-30 18:30:09 +0300 | [diff] [blame] | 184 | def get_coro(self): |
| 185 | return self._coro |
| 186 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 187 | def get_name(self): |
| 188 | return self._name |
| 189 | |
| 190 | def set_name(self, value): |
| 191 | self._name = str(value) |
| 192 | |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 193 | def set_result(self, result): |
| 194 | raise RuntimeError('Task does not support set_result operation') |
| 195 | |
| 196 | def set_exception(self, exception): |
| 197 | raise RuntimeError('Task does not support set_exception operation') |
| 198 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 199 | def get_stack(self, *, limit=None): |
| 200 | """Return the list of stack frames for this task's coroutine. |
| 201 | |
Victor Stinner | d87de83 | 2014-12-02 17:57:04 +0100 | [diff] [blame] | 202 | If the coroutine is not done, this returns the stack where it is |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 203 | suspended. If the coroutine has completed successfully or was |
| 204 | cancelled, this returns an empty list. If the coroutine was |
| 205 | terminated by an exception, this returns the list of traceback |
| 206 | frames. |
| 207 | |
| 208 | The frames are always ordered from oldest to newest. |
| 209 | |
Yury Selivanov | b0b0e62 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 210 | The optional limit gives the maximum number of frames to |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 211 | return; by default all available frames are returned. Its |
| 212 | meaning differs depending on whether a stack or a traceback is |
| 213 | returned: the newest frames of a stack are returned, but the |
| 214 | oldest frames of a traceback are returned. (This matches the |
| 215 | behavior of the traceback module.) |
| 216 | |
| 217 | For reasons beyond our control, only one stack frame is |
| 218 | returned for a suspended coroutine. |
| 219 | """ |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 220 | return base_tasks._task_get_stack(self, limit) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 221 | |
| 222 | def print_stack(self, *, limit=None, file=None): |
| 223 | """Print the stack or traceback for this task's coroutine. |
| 224 | |
| 225 | This produces output similar to that of the traceback module, |
| 226 | for the frames retrieved by get_stack(). The limit argument |
| 227 | is passed to get_stack(). The file argument is an I/O stream |
R David Murray | 8e069d5 | 2014-09-24 13:13:45 -0400 | [diff] [blame] | 228 | to which the output is written; by default output is written |
| 229 | to sys.stderr. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 230 | """ |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 231 | return base_tasks._task_print_stack(self, limit, file) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 232 | |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 233 | def cancel(self, msg=None): |
R David Murray | 8e069d5 | 2014-09-24 13:13:45 -0400 | [diff] [blame] | 234 | """Request that this task cancel itself. |
Victor Stinner | 4bd652a | 2014-04-07 11:18:06 +0200 | [diff] [blame] | 235 | |
Victor Stinner | 8d21357 | 2014-06-02 23:06:46 +0200 | [diff] [blame] | 236 | This arranges for a CancelledError to be thrown into the |
Victor Stinner | 4bd652a | 2014-04-07 11:18:06 +0200 | [diff] [blame] | 237 | wrapped coroutine on the next cycle through the event loop. |
| 238 | The coroutine then has a chance to clean up or even deny |
| 239 | the request using try/except/finally. |
| 240 | |
R David Murray | 8e069d5 | 2014-09-24 13:13:45 -0400 | [diff] [blame] | 241 | Unlike Future.cancel, this does not guarantee that the |
Victor Stinner | 4bd652a | 2014-04-07 11:18:06 +0200 | [diff] [blame] | 242 | task will be cancelled: the exception might be caught and |
R David Murray | 8e069d5 | 2014-09-24 13:13:45 -0400 | [diff] [blame] | 243 | acted upon, delaying cancellation of the task or preventing |
| 244 | cancellation completely. The task may also return a value or |
| 245 | raise a different exception. |
Victor Stinner | 4bd652a | 2014-04-07 11:18:06 +0200 | [diff] [blame] | 246 | |
| 247 | Immediately after this method is called, Task.cancelled() will |
| 248 | not return True (unless the task was already cancelled). A |
| 249 | task will be marked as cancelled when the wrapped coroutine |
| 250 | terminates with a CancelledError exception (even if cancel() |
| 251 | was not called). |
| 252 | """ |
Yury Selivanov | 7ce1c6f | 2017-06-11 13:49:18 +0000 | [diff] [blame] | 253 | self._log_traceback = False |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 254 | if self.done(): |
| 255 | return False |
| 256 | if self._fut_waiter is not None: |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 257 | if self._fut_waiter.cancel(msg=msg): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 258 | # Leave self._fut_waiter; it may be a Task that |
| 259 | # catches and ignores the cancellation so we may have |
| 260 | # to cancel it again later. |
| 261 | return True |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 262 | # It must be the case that self.__step is already scheduled. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 263 | self._must_cancel = True |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 264 | self._cancel_message = msg |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 265 | return True |
| 266 | |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 267 | def __step(self, exc=None): |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 268 | if self.done(): |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 269 | raise exceptions.InvalidStateError( |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 270 | f'_step(): already done: {self!r}, {exc!r}') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 271 | if self._must_cancel: |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 272 | if not isinstance(exc, exceptions.CancelledError): |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 273 | exc = self._make_cancelled_error() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 274 | self._must_cancel = False |
| 275 | coro = self._coro |
| 276 | self._fut_waiter = None |
Guido van Rossum | 1a605ed | 2013-12-06 12:57:40 -0800 | [diff] [blame] | 277 | |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 278 | _enter_task(self._loop, self) |
Yury Selivanov | d59bba8 | 2015-11-20 12:41:03 -0500 | [diff] [blame] | 279 | # Call either coro.throw(exc) or coro.send(None). |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 280 | try: |
Yury Selivanov | d59bba8 | 2015-11-20 12:41:03 -0500 | [diff] [blame] | 281 | if exc is None: |
| 282 | # We use the `send` method directly, because coroutines |
| 283 | # don't have `__iter__` and `__next__` methods. |
| 284 | result = coro.send(None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 285 | else: |
Yury Selivanov | d59bba8 | 2015-11-20 12:41:03 -0500 | [diff] [blame] | 286 | result = coro.throw(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 287 | except StopIteration as exc: |
INADA Naoki | 991adca | 2017-05-11 21:18:38 +0900 | [diff] [blame] | 288 | if self._must_cancel: |
| 289 | # Task is cancelled right before coro stops. |
| 290 | self._must_cancel = False |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 291 | super().cancel(msg=self._cancel_message) |
INADA Naoki | 991adca | 2017-05-11 21:18:38 +0900 | [diff] [blame] | 292 | else: |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 293 | super().set_result(exc.value) |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 294 | except exceptions.CancelledError as exc: |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 295 | # Save the original exception so we can chain it later. |
| 296 | self._cancelled_exc = exc |
| 297 | super().cancel() # I.e., Future.cancel(self). |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 298 | except (KeyboardInterrupt, SystemExit) as exc: |
Yury Selivanov | 0cf16f9 | 2017-12-25 10:48:15 -0500 | [diff] [blame] | 299 | super().set_exception(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 300 | raise |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 301 | except BaseException as exc: |
| 302 | super().set_exception(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 303 | else: |
Guido van Rossum | 1140a03 | 2016-09-09 12:54:54 -0700 | [diff] [blame] | 304 | blocking = getattr(result, '_asyncio_future_blocking', None) |
| 305 | if blocking is not None: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 306 | # Yielded Future must come from Future.__iter__(). |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 307 | if futures._get_loop(result) is not self._loop: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 308 | new_exc = RuntimeError( |
| 309 | f'Task {self!r} got Future ' |
| 310 | f'{result!r} attached to a different loop') |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 311 | self._loop.call_soon( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 312 | self.__step, new_exc, context=self._context) |
Guido van Rossum | 1140a03 | 2016-09-09 12:54:54 -0700 | [diff] [blame] | 313 | elif blocking: |
Yury Selivanov | 4145c83 | 2016-10-09 12:19:12 -0400 | [diff] [blame] | 314 | if result is self: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 315 | new_exc = RuntimeError( |
| 316 | f'Task cannot await on itself: {self!r}') |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 317 | self._loop.call_soon( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 318 | self.__step, new_exc, context=self._context) |
Yury Selivanov | 4145c83 | 2016-10-09 12:19:12 -0400 | [diff] [blame] | 319 | else: |
| 320 | result._asyncio_future_blocking = False |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 321 | result.add_done_callback( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 322 | self.__wakeup, context=self._context) |
Yury Selivanov | 4145c83 | 2016-10-09 12:19:12 -0400 | [diff] [blame] | 323 | self._fut_waiter = result |
| 324 | if self._must_cancel: |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 325 | if self._fut_waiter.cancel( |
| 326 | msg=self._cancel_message): |
Yury Selivanov | 4145c83 | 2016-10-09 12:19:12 -0400 | [diff] [blame] | 327 | self._must_cancel = False |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 328 | else: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 329 | new_exc = RuntimeError( |
| 330 | f'yield was used instead of yield from ' |
| 331 | f'in task {self!r} with {result!r}') |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 332 | self._loop.call_soon( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 333 | self.__step, new_exc, context=self._context) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 334 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 335 | elif result is None: |
| 336 | # Bare yield relinquishes control for one event loop iteration. |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 337 | self._loop.call_soon(self.__step, context=self._context) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 338 | elif inspect.isgenerator(result): |
| 339 | # Yielding a generator is just wrong. |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 340 | new_exc = RuntimeError( |
| 341 | f'yield was used instead of yield from for ' |
Serhiy Storchaka | 6655354 | 2018-05-20 16:30:31 +0300 | [diff] [blame] | 342 | f'generator in task {self!r} with {result!r}') |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 343 | self._loop.call_soon( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 344 | self.__step, new_exc, context=self._context) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 345 | else: |
| 346 | # Yielding something else is an error. |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 347 | new_exc = RuntimeError(f'Task got bad yield: {result!r}') |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 348 | self._loop.call_soon( |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 349 | self.__step, new_exc, context=self._context) |
Guido van Rossum | 1a605ed | 2013-12-06 12:57:40 -0800 | [diff] [blame] | 350 | finally: |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 351 | _leave_task(self._loop, self) |
Victor Stinner | d74ac82 | 2014-03-04 23:07:08 +0100 | [diff] [blame] | 352 | self = None # Needed to break cycles when an exception occurs. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 353 | |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 354 | def __wakeup(self, future): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 355 | try: |
Yury Selivanov | a4afc48 | 2015-11-16 15:12:10 -0500 | [diff] [blame] | 356 | future.result() |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 357 | except BaseException as exc: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 358 | # This may also be a cancellation. |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 359 | self.__step(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 360 | else: |
Yury Selivanov | a4afc48 | 2015-11-16 15:12:10 -0500 | [diff] [blame] | 361 | # Don't pass the value of `future.result()` explicitly, |
| 362 | # as `Future.__iter__` and `Future.__await__` don't need it. |
| 363 | # If we call `_step(value, None)` instead of `_step()`, |
| 364 | # Python eval loop would use `.send(value)` method call, |
| 365 | # instead of `__next__()`, which is slower for futures |
| 366 | # that return non-generator iterators from their `__iter__`. |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 367 | self.__step() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 368 | self = None # Needed to break cycles when an exception occurs. |
| 369 | |
| 370 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 371 | _PyTask = Task |
| 372 | |
| 373 | |
| 374 | try: |
| 375 | import _asyncio |
| 376 | except ImportError: |
| 377 | pass |
| 378 | else: |
| 379 | # _CTask is needed for tests. |
| 380 | Task = _CTask = _asyncio.Task |
| 381 | |
| 382 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 383 | def create_task(coro, *, name=None): |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 384 | """Schedule the execution of a coroutine object in a spawn task. |
| 385 | |
| 386 | Return a Task object. |
| 387 | """ |
| 388 | loop = events.get_running_loop() |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 389 | task = loop.create_task(coro) |
| 390 | _set_task_name(task, name) |
| 391 | return task |
Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 392 | |
| 393 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 394 | # wait() and as_completed() similar to those in PEP 3148. |
| 395 | |
| 396 | FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED |
| 397 | FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION |
| 398 | ALL_COMPLETED = concurrent.futures.ALL_COMPLETED |
| 399 | |
| 400 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 401 | async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 402 | """Wait for the Futures and coroutines given by fs to complete. |
| 403 | |
Victor Stinner | db74d98 | 2014-06-10 11:16:05 +0200 | [diff] [blame] | 404 | The sequence futures must not be empty. |
| 405 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 406 | Coroutines will be wrapped in Tasks. |
| 407 | |
| 408 | Returns two sets of Future: (done, pending). |
| 409 | |
| 410 | Usage: |
| 411 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 412 | done, pending = await asyncio.wait(fs) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 413 | |
| 414 | Note: This does not raise TimeoutError! Futures that aren't done |
| 415 | when the timeout occurs are returned in the second set. |
| 416 | """ |
Guido van Rossum | 7b3b3dc | 2016-09-09 14:26:31 -0700 | [diff] [blame] | 417 | if futures.isfuture(fs) or coroutines.iscoroutine(fs): |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 418 | raise TypeError(f"expect a list of futures, not {type(fs).__name__}") |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 419 | if not fs: |
| 420 | raise ValueError('Set of coroutines/Futures is empty.') |
Victor Stinner | e931f7b | 2014-07-16 18:50:39 +0200 | [diff] [blame] | 421 | if return_when not in (FIRST_COMPLETED, FIRST_EXCEPTION, ALL_COMPLETED): |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 422 | raise ValueError(f'Invalid return_when value: {return_when}') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 423 | |
| 424 | if loop is None: |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 425 | loop = events.get_running_loop() |
| 426 | else: |
Matthias Bussonnier | d0ebf13 | 2019-05-20 23:20:10 -0700 | [diff] [blame] | 427 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 428 | "and scheduled for removal in Python 3.10.", |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 429 | DeprecationWarning, stacklevel=2) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 430 | |
Kyle Stanley | 89aa7f0 | 2019-12-30 06:50:19 -0500 | [diff] [blame] | 431 | if any(coroutines.iscoroutine(f) for f in set(fs)): |
| 432 | warnings.warn("The explicit passing of coroutine objects to " |
| 433 | "asyncio.wait() is deprecated since Python 3.8, and " |
| 434 | "scheduled for removal in Python 3.11.", |
| 435 | DeprecationWarning, stacklevel=2) |
| 436 | |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 437 | fs = {ensure_future(f, loop=loop) for f in set(fs)} |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 438 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 439 | return await _wait(fs, timeout, return_when, loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 440 | |
| 441 | |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 442 | def _release_waiter(waiter, *args): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 443 | if not waiter.done(): |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 444 | waiter.set_result(None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 445 | |
| 446 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 447 | async def wait_for(fut, timeout, *, loop=None): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 448 | """Wait for the single Future or coroutine to complete, with timeout. |
| 449 | |
| 450 | Coroutine will be wrapped in Task. |
| 451 | |
Victor Stinner | 421e49b | 2014-01-23 17:40:59 +0100 | [diff] [blame] | 452 | Returns result of the Future or coroutine. When a timeout occurs, |
| 453 | it cancels the task and raises TimeoutError. To avoid the task |
| 454 | cancellation, wrap it in shield(). |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 455 | |
Victor Stinner | 922bc2c | 2015-01-15 16:29:10 +0100 | [diff] [blame] | 456 | If the wait is cancelled, the task is also cancelled. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 457 | |
Victor Stinner | 922bc2c | 2015-01-15 16:29:10 +0100 | [diff] [blame] | 458 | This function is a coroutine. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 459 | """ |
| 460 | if loop is None: |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 461 | loop = events.get_running_loop() |
| 462 | else: |
Matthias Bussonnier | d0ebf13 | 2019-05-20 23:20:10 -0700 | [diff] [blame] | 463 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 464 | "and scheduled for removal in Python 3.10.", |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 465 | DeprecationWarning, stacklevel=2) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 466 | |
Guido van Rossum | 48c66c3 | 2014-01-29 14:30:38 -0800 | [diff] [blame] | 467 | if timeout is None: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 468 | return await fut |
Guido van Rossum | 48c66c3 | 2014-01-29 14:30:38 -0800 | [diff] [blame] | 469 | |
Victor K | 4d07189 | 2017-10-05 19:04:39 +0300 | [diff] [blame] | 470 | if timeout <= 0: |
| 471 | fut = ensure_future(fut, loop=loop) |
| 472 | |
| 473 | if fut.done(): |
| 474 | return fut.result() |
| 475 | |
| 476 | fut.cancel() |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 477 | raise exceptions.TimeoutError() |
Victor K | 4d07189 | 2017-10-05 19:04:39 +0300 | [diff] [blame] | 478 | |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 479 | waiter = loop.create_future() |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 480 | timeout_handle = loop.call_later(timeout, _release_waiter, waiter) |
| 481 | cb = functools.partial(_release_waiter, waiter) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 482 | |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 483 | fut = ensure_future(fut, loop=loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 484 | fut.add_done_callback(cb) |
| 485 | |
| 486 | try: |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 487 | # wait until the future completes or the timeout |
Victor Stinner | 922bc2c | 2015-01-15 16:29:10 +0100 | [diff] [blame] | 488 | try: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 489 | await waiter |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 490 | except exceptions.CancelledError: |
Victor Stinner | 922bc2c | 2015-01-15 16:29:10 +0100 | [diff] [blame] | 491 | fut.remove_done_callback(cb) |
| 492 | fut.cancel() |
| 493 | raise |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 494 | |
| 495 | if fut.done(): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 496 | return fut.result() |
| 497 | else: |
| 498 | fut.remove_done_callback(cb) |
Elvis Pranskevichus | e2b340a | 2018-05-29 17:31:01 -0400 | [diff] [blame] | 499 | # We must ensure that the task is not running |
| 500 | # after wait_for() returns. |
| 501 | # See https://bugs.python.org/issue32751 |
| 502 | await _cancel_and_wait(fut, loop=loop) |
romasku | 382a563 | 2020-05-15 23:12:05 +0300 | [diff] [blame] | 503 | # In case task cancellation failed with some |
| 504 | # exception, we should re-raise it |
| 505 | # See https://bugs.python.org/issue40607 |
| 506 | try: |
| 507 | fut.result() |
| 508 | except exceptions.CancelledError as exc: |
| 509 | raise exceptions.TimeoutError() from exc |
| 510 | else: |
| 511 | raise exceptions.TimeoutError() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 512 | finally: |
| 513 | timeout_handle.cancel() |
| 514 | |
| 515 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 516 | async def _wait(fs, timeout, return_when, loop): |
Elvis Pranskevichus | e2b340a | 2018-05-29 17:31:01 -0400 | [diff] [blame] | 517 | """Internal helper for wait(). |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 518 | |
| 519 | The fs argument must be a collection of Futures. |
| 520 | """ |
| 521 | assert fs, 'Set of Futures is empty.' |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 522 | waiter = loop.create_future() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 523 | timeout_handle = None |
| 524 | if timeout is not None: |
| 525 | timeout_handle = loop.call_later(timeout, _release_waiter, waiter) |
| 526 | counter = len(fs) |
| 527 | |
| 528 | def _on_completion(f): |
| 529 | nonlocal counter |
| 530 | counter -= 1 |
| 531 | if (counter <= 0 or |
| 532 | return_when == FIRST_COMPLETED or |
| 533 | return_when == FIRST_EXCEPTION and (not f.cancelled() and |
| 534 | f.exception() is not None)): |
| 535 | if timeout_handle is not None: |
| 536 | timeout_handle.cancel() |
| 537 | if not waiter.done(): |
Victor Stinner | 59e0802 | 2014-08-28 11:19:25 +0200 | [diff] [blame] | 538 | waiter.set_result(None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 539 | |
| 540 | for f in fs: |
| 541 | f.add_done_callback(_on_completion) |
| 542 | |
| 543 | try: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 544 | await waiter |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 545 | finally: |
| 546 | if timeout_handle is not None: |
| 547 | timeout_handle.cancel() |
gescheit | c1964e9 | 2019-05-03 18:18:02 +0300 | [diff] [blame] | 548 | for f in fs: |
| 549 | f.remove_done_callback(_on_completion) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 550 | |
| 551 | done, pending = set(), set() |
| 552 | for f in fs: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 553 | if f.done(): |
| 554 | done.add(f) |
| 555 | else: |
| 556 | pending.add(f) |
| 557 | return done, pending |
| 558 | |
| 559 | |
Elvis Pranskevichus | e2b340a | 2018-05-29 17:31:01 -0400 | [diff] [blame] | 560 | async def _cancel_and_wait(fut, loop): |
| 561 | """Cancel the *fut* future or task and wait until it completes.""" |
| 562 | |
| 563 | waiter = loop.create_future() |
| 564 | cb = functools.partial(_release_waiter, waiter) |
| 565 | fut.add_done_callback(cb) |
| 566 | |
| 567 | try: |
| 568 | fut.cancel() |
| 569 | # We cannot wait on *fut* directly to make |
| 570 | # sure _cancel_and_wait itself is reliably cancellable. |
| 571 | await waiter |
| 572 | finally: |
| 573 | fut.remove_done_callback(cb) |
| 574 | |
| 575 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 576 | # This is *not* a @coroutine! It is just an iterator (yielding Futures). |
| 577 | def as_completed(fs, *, loop=None, timeout=None): |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 578 | """Return an iterator whose values are coroutines. |
| 579 | |
| 580 | When waiting for the yielded coroutines you'll get the results (or |
| 581 | exceptions!) of the original Futures (or coroutines), in the order |
| 582 | in which and as soon as they complete. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 583 | |
| 584 | This differs from PEP 3148; the proper way to use this is: |
| 585 | |
| 586 | for f in as_completed(fs): |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 587 | result = await f # The 'await' may raise. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 588 | # Use result. |
| 589 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 590 | If a timeout is specified, the 'await' will raise |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 591 | TimeoutError when the timeout occurs before all Futures are done. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 592 | |
| 593 | Note: The futures 'f' are not necessarily members of fs. |
| 594 | """ |
Guido van Rossum | 7b3b3dc | 2016-09-09 14:26:31 -0700 | [diff] [blame] | 595 | if futures.isfuture(fs) or coroutines.iscoroutine(fs): |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 596 | raise TypeError(f"expect a list of futures, not {type(fs).__name__}") |
Andrew Svetlov | a488879 | 2019-09-12 15:40:40 +0300 | [diff] [blame] | 597 | |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 598 | from .queues import Queue # Import here to avoid circular import problem. |
| 599 | done = Queue(loop=loop) |
Andrew Svetlov | a488879 | 2019-09-12 15:40:40 +0300 | [diff] [blame] | 600 | |
| 601 | if loop is None: |
| 602 | loop = events.get_event_loop() |
| 603 | else: |
| 604 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 605 | "and scheduled for removal in Python 3.10.", |
| 606 | DeprecationWarning, stacklevel=2) |
| 607 | todo = {ensure_future(f, loop=loop) for f in set(fs)} |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 608 | timeout_handle = None |
| 609 | |
| 610 | def _on_timeout(): |
| 611 | for f in todo: |
| 612 | f.remove_done_callback(_on_completion) |
| 613 | done.put_nowait(None) # Queue a dummy value for _wait_for_one(). |
| 614 | todo.clear() # Can't do todo.remove(f) in the loop. |
| 615 | |
| 616 | def _on_completion(f): |
| 617 | if not todo: |
| 618 | return # _on_timeout() was here first. |
| 619 | todo.remove(f) |
| 620 | done.put_nowait(f) |
| 621 | if not todo and timeout_handle is not None: |
| 622 | timeout_handle.cancel() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 623 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 624 | async def _wait_for_one(): |
| 625 | f = await done.get() |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 626 | if f is None: |
| 627 | # Dummy value from _on_timeout(). |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 628 | raise exceptions.TimeoutError |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 629 | return f.result() # May raise f.exception(). |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 630 | |
Guido van Rossum | b58f053 | 2014-02-12 17:58:19 -0800 | [diff] [blame] | 631 | for f in todo: |
| 632 | f.add_done_callback(_on_completion) |
| 633 | if todo and timeout is not None: |
| 634 | timeout_handle = loop.call_later(timeout, _on_timeout) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 635 | for _ in range(len(todo)): |
| 636 | yield _wait_for_one() |
| 637 | |
| 638 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 639 | @types.coroutine |
| 640 | def __sleep0(): |
| 641 | """Skip one event loop run cycle. |
| 642 | |
| 643 | This is a private helper for 'asyncio.sleep()', used |
| 644 | when the 'delay' is set to 0. It uses a bare 'yield' |
Yury Selivanov | 22feeb8 | 2018-01-24 11:31:01 -0500 | [diff] [blame] | 645 | expression (which Task.__step knows how to handle) |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 646 | instead of creating a Future object. |
| 647 | """ |
| 648 | yield |
| 649 | |
| 650 | |
| 651 | async def sleep(delay, result=None, *, loop=None): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 652 | """Coroutine that completes after a given time (in seconds).""" |
Andrew Svetlov | 5382c05 | 2017-12-17 16:41:30 +0200 | [diff] [blame] | 653 | if delay <= 0: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 654 | await __sleep0() |
Yury Selivanov | ade0412 | 2015-11-05 14:29:04 -0500 | [diff] [blame] | 655 | return result |
| 656 | |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 657 | if loop is None: |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 658 | loop = events.get_running_loop() |
| 659 | else: |
Matthias Bussonnier | d0ebf13 | 2019-05-20 23:20:10 -0700 | [diff] [blame] | 660 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 661 | "and scheduled for removal in Python 3.10.", |
João Júnior | 558c49b | 2018-09-24 06:51:22 -0300 | [diff] [blame] | 662 | DeprecationWarning, stacklevel=2) |
| 663 | |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 664 | future = loop.create_future() |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 665 | h = loop.call_later(delay, |
| 666 | futures._set_result_unless_cancelled, |
| 667 | future, result) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 668 | try: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 669 | return await future |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 670 | finally: |
| 671 | h.cancel() |
| 672 | |
| 673 | |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 674 | def ensure_future(coro_or_future, *, loop=None): |
Yury Selivanov | 620279b | 2015-10-02 15:00:19 -0400 | [diff] [blame] | 675 | """Wrap a coroutine or an awaitable in a future. |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 676 | |
| 677 | If the argument is a Future, it is returned directly. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 678 | """ |
jimmylai | e549c4b | 2018-05-28 06:42:05 -1000 | [diff] [blame] | 679 | if coroutines.iscoroutine(coro_or_future): |
Victor Stinner | 896a25a | 2014-07-08 11:29:25 +0200 | [diff] [blame] | 680 | if loop is None: |
| 681 | loop = events.get_event_loop() |
| 682 | task = loop.create_task(coro_or_future) |
Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 683 | if task._source_traceback: |
| 684 | del task._source_traceback[-1] |
| 685 | return task |
jimmylai | e549c4b | 2018-05-28 06:42:05 -1000 | [diff] [blame] | 686 | elif futures.isfuture(coro_or_future): |
| 687 | if loop is not None and loop is not futures._get_loop(coro_or_future): |
Zackery Spytz | 4737b92 | 2019-05-03 09:35:26 -0600 | [diff] [blame] | 688 | raise ValueError('The future belongs to a different loop than ' |
| 689 | 'the one specified as the loop argument') |
jimmylai | e549c4b | 2018-05-28 06:42:05 -1000 | [diff] [blame] | 690 | return coro_or_future |
Victor Stinner | 3f438a9 | 2017-11-28 14:43:52 +0100 | [diff] [blame] | 691 | elif inspect.isawaitable(coro_or_future): |
Yury Selivanov | 620279b | 2015-10-02 15:00:19 -0400 | [diff] [blame] | 692 | return ensure_future(_wrap_awaitable(coro_or_future), loop=loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 693 | else: |
Charles Renwick | ae5b326 | 2017-04-21 16:49:48 -0400 | [diff] [blame] | 694 | raise TypeError('An asyncio.Future, a coroutine or an awaitable is ' |
| 695 | 'required') |
Yury Selivanov | 620279b | 2015-10-02 15:00:19 -0400 | [diff] [blame] | 696 | |
| 697 | |
Andrew Svetlov | 68b34a7 | 2019-05-16 17:52:10 +0300 | [diff] [blame] | 698 | @types.coroutine |
Yury Selivanov | 620279b | 2015-10-02 15:00:19 -0400 | [diff] [blame] | 699 | def _wrap_awaitable(awaitable): |
| 700 | """Helper for asyncio.ensure_future(). |
| 701 | |
| 702 | Wraps awaitable (an object with __await__) into a coroutine |
| 703 | that will later be wrapped in a Task by ensure_future(). |
| 704 | """ |
| 705 | return (yield from awaitable.__await__()) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 706 | |
Andrew Svetlov | 68b34a7 | 2019-05-16 17:52:10 +0300 | [diff] [blame] | 707 | _wrap_awaitable._is_coroutine = _is_coroutine |
| 708 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 709 | |
| 710 | class _GatheringFuture(futures.Future): |
| 711 | """Helper for gather(). |
| 712 | |
| 713 | This overrides cancel() to cancel all the children and act more |
| 714 | like Task.cancel(), which doesn't immediately mark itself as |
| 715 | cancelled. |
| 716 | """ |
| 717 | |
| 718 | def __init__(self, children, *, loop=None): |
| 719 | super().__init__(loop=loop) |
| 720 | self._children = children |
Yury Selivanov | 863b674 | 2018-05-29 17:20:02 -0400 | [diff] [blame] | 721 | self._cancel_requested = False |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 722 | |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 723 | def cancel(self, msg=None): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 724 | if self.done(): |
| 725 | return False |
Yury Selivanov | 3d67615 | 2016-10-21 17:22:17 -0400 | [diff] [blame] | 726 | ret = False |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 727 | for child in self._children: |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 728 | if child.cancel(msg=msg): |
Yury Selivanov | 3d67615 | 2016-10-21 17:22:17 -0400 | [diff] [blame] | 729 | ret = True |
Yury Selivanov | 863b674 | 2018-05-29 17:20:02 -0400 | [diff] [blame] | 730 | if ret: |
| 731 | # If any child tasks were actually cancelled, we should |
| 732 | # propagate the cancellation request regardless of |
| 733 | # *return_exceptions* argument. See issue 32684. |
| 734 | self._cancel_requested = True |
Yury Selivanov | 3d67615 | 2016-10-21 17:22:17 -0400 | [diff] [blame] | 735 | return ret |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 736 | |
| 737 | |
| 738 | def gather(*coros_or_futures, loop=None, return_exceptions=False): |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 739 | """Return a future aggregating results from the given coroutines/futures. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 740 | |
Guido van Rossum | e3c65a7 | 2016-09-30 08:17:15 -0700 | [diff] [blame] | 741 | Coroutines will be wrapped in a future and scheduled in the event |
| 742 | loop. They will not necessarily be scheduled in the same order as |
| 743 | passed in. |
| 744 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 745 | All futures must share the same event loop. If all the tasks are |
| 746 | done successfully, the returned future's result is the list of |
| 747 | results (in the order of the original sequence, not necessarily |
Yury Selivanov | f317cb7 | 2014-02-06 12:03:53 -0500 | [diff] [blame] | 748 | the order of results arrival). If *return_exceptions* is True, |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 749 | exceptions in the tasks are treated the same as successful |
| 750 | results, and gathered in the result list; otherwise, the first |
| 751 | raised exception will be immediately propagated to the returned |
| 752 | future. |
| 753 | |
| 754 | Cancellation: if the outer Future is cancelled, all children (that |
| 755 | have not completed yet) are also cancelled. If any child is |
| 756 | cancelled, this is treated as if it raised CancelledError -- |
| 757 | the outer Future is *not* cancelled in this case. (This is to |
| 758 | prevent the cancellation of one child to cause other children to |
| 759 | be cancelled.) |
| 760 | """ |
Victor Stinner | f03b3c7 | 2014-07-16 18:36:24 +0200 | [diff] [blame] | 761 | if not coros_or_futures: |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 762 | if loop is None: |
| 763 | loop = events.get_event_loop() |
Andrew Svetlov | a488879 | 2019-09-12 15:40:40 +0300 | [diff] [blame] | 764 | else: |
| 765 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 766 | "and scheduled for removal in Python 3.10.", |
| 767 | DeprecationWarning, stacklevel=2) |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 768 | outer = loop.create_future() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 769 | outer.set_result([]) |
| 770 | return outer |
Victor Stinner | f03b3c7 | 2014-07-16 18:36:24 +0200 | [diff] [blame] | 771 | |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 772 | def _done_callback(fut): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 773 | nonlocal nfinished |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 774 | nfinished += 1 |
| 775 | |
Victor Stinner | 3531d90 | 2015-01-09 01:42:52 +0100 | [diff] [blame] | 776 | if outer.done(): |
| 777 | if not fut.cancelled(): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 778 | # Mark exception retrieved. |
| 779 | fut.exception() |
| 780 | return |
Victor Stinner | 3531d90 | 2015-01-09 01:42:52 +0100 | [diff] [blame] | 781 | |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 782 | if not return_exceptions: |
| 783 | if fut.cancelled(): |
| 784 | # Check if 'fut' is cancelled first, as |
| 785 | # 'fut.exception()' will *raise* a CancelledError |
| 786 | # instead of returning it. |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 787 | exc = fut._make_cancelled_error() |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 788 | outer.set_exception(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 789 | return |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 790 | else: |
| 791 | exc = fut.exception() |
| 792 | if exc is not None: |
| 793 | outer.set_exception(exc) |
| 794 | return |
| 795 | |
| 796 | if nfinished == nfuts: |
| 797 | # All futures are done; create a list of results |
| 798 | # and set it to the 'outer' future. |
| 799 | results = [] |
| 800 | |
| 801 | for fut in children: |
| 802 | if fut.cancelled(): |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 803 | # Check if 'fut' is cancelled first, as 'fut.exception()' |
| 804 | # will *raise* a CancelledError instead of returning it. |
| 805 | # Also, since we're adding the exception return value |
| 806 | # to 'results' instead of raising it, don't bother |
| 807 | # setting __context__. This also lets us preserve |
| 808 | # calling '_make_cancelled_error()' at most once. |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 809 | res = exceptions.CancelledError( |
| 810 | '' if fut._cancel_message is None else |
| 811 | fut._cancel_message) |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 812 | else: |
| 813 | res = fut.exception() |
| 814 | if res is None: |
| 815 | res = fut.result() |
| 816 | results.append(res) |
| 817 | |
Yury Selivanov | 863b674 | 2018-05-29 17:20:02 -0400 | [diff] [blame] | 818 | if outer._cancel_requested: |
| 819 | # If gather is being cancelled we must propagate the |
| 820 | # cancellation regardless of *return_exceptions* argument. |
| 821 | # See issue 32684. |
Chris Jerdonek | da742ba | 2020-05-17 22:47:31 -0700 | [diff] [blame] | 822 | exc = fut._make_cancelled_error() |
Chris Jerdonek | 1ce5841 | 2020-05-15 16:55:50 -0700 | [diff] [blame] | 823 | outer.set_exception(exc) |
Yury Selivanov | 863b674 | 2018-05-29 17:20:02 -0400 | [diff] [blame] | 824 | else: |
| 825 | outer.set_result(results) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 826 | |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 827 | arg_to_fut = {} |
| 828 | children = [] |
| 829 | nfuts = 0 |
| 830 | nfinished = 0 |
| 831 | for arg in coros_or_futures: |
| 832 | if arg not in arg_to_fut: |
| 833 | fut = ensure_future(arg, loop=loop) |
| 834 | if loop is None: |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 835 | loop = futures._get_loop(fut) |
Yury Selivanov | 36c2c04 | 2017-12-19 07:19:53 -0500 | [diff] [blame] | 836 | if fut is not arg: |
| 837 | # 'arg' was not a Future, therefore, 'fut' is a new |
| 838 | # Future created specifically for 'arg'. Since the caller |
| 839 | # can't control it, disable the "destroy pending task" |
| 840 | # warning. |
| 841 | fut._log_destroy_pending = False |
| 842 | |
| 843 | nfuts += 1 |
| 844 | arg_to_fut[arg] = fut |
| 845 | fut.add_done_callback(_done_callback) |
| 846 | |
| 847 | else: |
| 848 | # There's a duplicate Future object in coros_or_futures. |
| 849 | fut = arg_to_fut[arg] |
| 850 | |
| 851 | children.append(fut) |
| 852 | |
| 853 | outer = _GatheringFuture(children, loop=loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 854 | return outer |
| 855 | |
| 856 | |
| 857 | def shield(arg, *, loop=None): |
| 858 | """Wait for a future, shielding it from cancellation. |
| 859 | |
| 860 | The statement |
| 861 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 862 | res = await shield(something()) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 863 | |
| 864 | is exactly equivalent to the statement |
| 865 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 866 | res = await something() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 867 | |
| 868 | *except* that if the coroutine containing it is cancelled, the |
| 869 | task running in something() is not cancelled. From the POV of |
| 870 | something(), the cancellation did not happen. But its caller is |
| 871 | still cancelled, so the yield-from expression still raises |
| 872 | CancelledError. Note: If something() is cancelled by other means |
| 873 | this will still cancel shield(). |
| 874 | |
| 875 | If you want to completely ignore cancellation (not recommended) |
| 876 | you can combine shield() with a try/except clause, as follows: |
| 877 | |
| 878 | try: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 879 | res = await shield(something()) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 880 | except CancelledError: |
| 881 | res = None |
| 882 | """ |
Andrew Svetlov | a488879 | 2019-09-12 15:40:40 +0300 | [diff] [blame] | 883 | if loop is not None: |
| 884 | warnings.warn("The loop argument is deprecated since Python 3.8, " |
| 885 | "and scheduled for removal in Python 3.10.", |
| 886 | DeprecationWarning, stacklevel=2) |
Yury Selivanov | 59eb9a4 | 2015-05-11 14:48:38 -0400 | [diff] [blame] | 887 | inner = ensure_future(arg, loop=loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 888 | if inner.done(): |
| 889 | # Shortcut. |
| 890 | return inner |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 891 | loop = futures._get_loop(inner) |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 892 | outer = loop.create_future() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 893 | |
Romain Picard | b35acc5 | 2019-05-07 20:58:24 +0200 | [diff] [blame] | 894 | def _inner_done_callback(inner): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 895 | if outer.cancelled(): |
Victor Stinner | 3531d90 | 2015-01-09 01:42:52 +0100 | [diff] [blame] | 896 | if not inner.cancelled(): |
| 897 | # Mark inner's result as retrieved. |
| 898 | inner.exception() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 899 | return |
Victor Stinner | 3531d90 | 2015-01-09 01:42:52 +0100 | [diff] [blame] | 900 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 901 | if inner.cancelled(): |
| 902 | outer.cancel() |
| 903 | else: |
| 904 | exc = inner.exception() |
| 905 | if exc is not None: |
| 906 | outer.set_exception(exc) |
| 907 | else: |
| 908 | outer.set_result(inner.result()) |
| 909 | |
Romain Picard | b35acc5 | 2019-05-07 20:58:24 +0200 | [diff] [blame] | 910 | |
| 911 | def _outer_done_callback(outer): |
| 912 | if not inner.done(): |
| 913 | inner.remove_done_callback(_inner_done_callback) |
| 914 | |
| 915 | inner.add_done_callback(_inner_done_callback) |
| 916 | outer.add_done_callback(_outer_done_callback) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 917 | return outer |
Guido van Rossum | 841d9ee | 2015-10-03 08:31:42 -0700 | [diff] [blame] | 918 | |
| 919 | |
| 920 | def run_coroutine_threadsafe(coro, loop): |
| 921 | """Submit a coroutine object to a given event loop. |
| 922 | |
| 923 | Return a concurrent.futures.Future to access the result. |
| 924 | """ |
| 925 | if not coroutines.iscoroutine(coro): |
| 926 | raise TypeError('A coroutine object is required') |
| 927 | future = concurrent.futures.Future() |
| 928 | |
| 929 | def callback(): |
Guido van Rossum | 601953b | 2015-10-05 16:20:00 -0700 | [diff] [blame] | 930 | try: |
| 931 | futures._chain_future(ensure_future(coro, loop=loop), future) |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 932 | except (SystemExit, KeyboardInterrupt): |
| 933 | raise |
| 934 | except BaseException as exc: |
Guido van Rossum | 601953b | 2015-10-05 16:20:00 -0700 | [diff] [blame] | 935 | if future.set_running_or_notify_cancel(): |
| 936 | future.set_exception(exc) |
| 937 | raise |
Guido van Rossum | 841d9ee | 2015-10-03 08:31:42 -0700 | [diff] [blame] | 938 | |
| 939 | loop.call_soon_threadsafe(callback) |
| 940 | return future |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 941 | |
| 942 | |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 943 | # WeakSet containing all alive tasks. |
| 944 | _all_tasks = weakref.WeakSet() |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 945 | |
| 946 | # Dictionary containing tasks that are currently active in |
| 947 | # all running event loops. {EventLoop: Task} |
| 948 | _current_tasks = {} |
| 949 | |
| 950 | |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 951 | def _register_task(task): |
| 952 | """Register a new task in asyncio as executed by loop.""" |
| 953 | _all_tasks.add(task) |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 954 | |
| 955 | |
| 956 | def _enter_task(loop, task): |
| 957 | current_task = _current_tasks.get(loop) |
| 958 | if current_task is not None: |
| 959 | raise RuntimeError(f"Cannot enter into task {task!r} while another " |
| 960 | f"task {current_task!r} is being executed.") |
| 961 | _current_tasks[loop] = task |
| 962 | |
| 963 | |
| 964 | def _leave_task(loop, task): |
| 965 | current_task = _current_tasks.get(loop) |
| 966 | if current_task is not task: |
| 967 | raise RuntimeError(f"Leaving task {task!r} does not match " |
| 968 | f"the current task {current_task!r}.") |
| 969 | del _current_tasks[loop] |
| 970 | |
| 971 | |
Yury Selivanov | ca9b36c | 2017-12-23 15:04:15 -0500 | [diff] [blame] | 972 | def _unregister_task(task): |
| 973 | """Unregister a task.""" |
| 974 | _all_tasks.discard(task) |
Andrew Svetlov | 44d1a59 | 2017-12-16 21:58:38 +0200 | [diff] [blame] | 975 | |
| 976 | |
| 977 | _py_register_task = _register_task |
| 978 | _py_unregister_task = _unregister_task |
| 979 | _py_enter_task = _enter_task |
| 980 | _py_leave_task = _leave_task |
| 981 | |
| 982 | |
| 983 | try: |
| 984 | from _asyncio import (_register_task, _unregister_task, |
| 985 | _enter_task, _leave_task, |
| 986 | _all_tasks, _current_tasks) |
| 987 | except ImportError: |
| 988 | pass |
| 989 | else: |
| 990 | _c_register_task = _register_task |
| 991 | _c_unregister_task = _unregister_task |
| 992 | _c_enter_task = _enter_task |
| 993 | _c_leave_task = _leave_task |