| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 1 | """Event loop and event loop policy.""" |
| 2 | |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 3 | __all__ = ( |
| 4 | 'AbstractEventLoopPolicy', |
| 5 | 'AbstractEventLoop', 'AbstractServer', |
| 6 | 'Handle', 'TimerHandle', |
| 7 | 'get_event_loop_policy', 'set_event_loop_policy', |
| 8 | 'get_event_loop', 'set_event_loop', 'new_event_loop', |
| 9 | 'get_child_watcher', 'set_child_watcher', |
| Yury Selivanov | abae67e | 2017-12-11 10:07:44 -0500 | [diff] [blame] | 10 | '_set_running_loop', 'get_running_loop', |
| 11 | '_get_running_loop', |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 12 | ) |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 13 | |
| Yury Selivanov | ba7e1f9 | 2017-03-02 20:07:11 -0500 | [diff] [blame] | 14 | import os |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 15 | import socket |
| Victor Stinner | 313a980 | 2014-07-29 12:58:23 +0200 | [diff] [blame] | 16 | import subprocess |
| Victor Stinner | 307bccc | 2014-06-12 18:39:26 +0200 | [diff] [blame] | 17 | import sys |
| Victor Stinner | 313a980 | 2014-07-29 12:58:23 +0200 | [diff] [blame] | 18 | import threading |
| Victor Stinner | 307bccc | 2014-06-12 18:39:26 +0200 | [diff] [blame] | 19 | |
| Antoine Pitrou | 921e943 | 2017-11-07 17:23:29 +0100 | [diff] [blame] | 20 | from . import constants |
| Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 21 | from . import format_helpers |
| Antoine Pitrou | 921e943 | 2017-11-07 17:23:29 +0100 | [diff] [blame] | 22 | |
| 23 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 24 | class Handle: |
| 25 | """Object returned by callback registration methods.""" |
| 26 | |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 27 | __slots__ = ('_callback', '_args', '_cancelled', '_loop', |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 28 | '_source_traceback', '_repr', '__weakref__') |
| Yury Selivanov | b131778 | 2014-02-12 17:01:52 -0500 | [diff] [blame] | 29 | |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 30 | def __init__(self, callback, args, loop): |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 31 | self._loop = loop |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 32 | self._callback = callback |
| 33 | self._args = args |
| 34 | self._cancelled = False |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 35 | self._repr = None |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 36 | if self._loop.get_debug(): |
| Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 37 | self._source_traceback = format_helpers.extract_stack( |
| 38 | sys._getframe(1)) |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 39 | else: |
| 40 | self._source_traceback = None |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 41 | |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 42 | def _repr_info(self): |
| Victor Stinner | f68bd88 | 2014-07-10 22:32:58 +0200 | [diff] [blame] | 43 | info = [self.__class__.__name__] |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 44 | if self._cancelled: |
| Victor Stinner | 975735f | 2014-06-25 21:41:58 +0200 | [diff] [blame] | 45 | info.append('cancelled') |
| Victor Stinner | f68bd88 | 2014-07-10 22:32:58 +0200 | [diff] [blame] | 46 | if self._callback is not None: |
| Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 47 | info.append(format_helpers._format_callback_source( |
| 48 | self._callback, self._args)) |
| Victor Stinner | f68bd88 | 2014-07-10 22:32:58 +0200 | [diff] [blame] | 49 | if self._source_traceback: |
| 50 | frame = self._source_traceback[-1] |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 51 | info.append(f'created at {frame[0]}:{frame[1]}') |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 52 | return info |
| 53 | |
| 54 | def __repr__(self): |
| 55 | if self._repr is not None: |
| 56 | return self._repr |
| 57 | info = self._repr_info() |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 58 | return '<{}>'.format(' '.join(info)) |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 59 | |
| 60 | def cancel(self): |
| Yury Selivanov | 592ada9 | 2014-09-25 12:07:56 -0400 | [diff] [blame] | 61 | if not self._cancelled: |
| 62 | self._cancelled = True |
| 63 | if self._loop.get_debug(): |
| 64 | # Keep a representation in debug mode to keep callback and |
| 65 | # parameters. For example, to log the warning |
| 66 | # "Executing <Handle...> took 2.5 second" |
| 67 | self._repr = repr(self) |
| 68 | self._callback = None |
| 69 | self._args = None |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 70 | |
| Marat Sharafutdinov | 69cfed1 | 2017-11-07 12:06:05 +0300 | [diff] [blame] | 71 | def cancelled(self): |
| 72 | return self._cancelled |
| 73 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 74 | def _run(self): |
| 75 | try: |
| 76 | self._callback(*self._args) |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 77 | except Exception as exc: |
| Andrew Svetlov | f74ef45 | 2017-12-15 07:04:38 +0200 | [diff] [blame] | 78 | cb = format_helpers._format_callback_source( |
| 79 | self._callback, self._args) |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 80 | msg = f'Exception in callback {cb}' |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 81 | context = { |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 82 | 'message': msg, |
| 83 | 'exception': exc, |
| 84 | 'handle': self, |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 85 | } |
| 86 | if self._source_traceback: |
| 87 | context['source_traceback'] = self._source_traceback |
| 88 | self._loop.call_exception_handler(context) |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 89 | self = None # Needed to break cycles when an exception occurs. |
| 90 | |
| 91 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 92 | class TimerHandle(Handle): |
| 93 | """Object returned by timed callback registration methods.""" |
| 94 | |
| Yury Selivanov | 592ada9 | 2014-09-25 12:07:56 -0400 | [diff] [blame] | 95 | __slots__ = ['_scheduled', '_when'] |
| Yury Selivanov | b131778 | 2014-02-12 17:01:52 -0500 | [diff] [blame] | 96 | |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 97 | def __init__(self, when, callback, args, loop): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 98 | assert when is not None |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 99 | super().__init__(callback, args, loop) |
| Victor Stinner | 80f53aa | 2014-06-27 13:52:20 +0200 | [diff] [blame] | 100 | if self._source_traceback: |
| 101 | del self._source_traceback[-1] |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 102 | self._when = when |
| Yury Selivanov | 592ada9 | 2014-09-25 12:07:56 -0400 | [diff] [blame] | 103 | self._scheduled = False |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 104 | |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 105 | def _repr_info(self): |
| 106 | info = super()._repr_info() |
| 107 | pos = 2 if self._cancelled else 1 |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 108 | info.insert(pos, f'when={self._when}') |
| Victor Stinner | 1b38bc6 | 2014-09-17 23:24:13 +0200 | [diff] [blame] | 109 | return info |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 110 | |
| 111 | def __hash__(self): |
| 112 | return hash(self._when) |
| 113 | |
| 114 | def __lt__(self, other): |
| 115 | return self._when < other._when |
| 116 | |
| 117 | def __le__(self, other): |
| 118 | if self._when < other._when: |
| 119 | return True |
| 120 | return self.__eq__(other) |
| 121 | |
| 122 | def __gt__(self, other): |
| 123 | return self._when > other._when |
| 124 | |
| 125 | def __ge__(self, other): |
| 126 | if self._when > other._when: |
| 127 | return True |
| 128 | return self.__eq__(other) |
| 129 | |
| 130 | def __eq__(self, other): |
| 131 | if isinstance(other, TimerHandle): |
| 132 | return (self._when == other._when and |
| 133 | self._callback == other._callback and |
| 134 | self._args == other._args and |
| 135 | self._cancelled == other._cancelled) |
| 136 | return NotImplemented |
| 137 | |
| 138 | def __ne__(self, other): |
| 139 | equal = self.__eq__(other) |
| 140 | return NotImplemented if equal is NotImplemented else not equal |
| 141 | |
| Yury Selivanov | 592ada9 | 2014-09-25 12:07:56 -0400 | [diff] [blame] | 142 | def cancel(self): |
| 143 | if not self._cancelled: |
| 144 | self._loop._timer_handle_cancelled(self) |
| 145 | super().cancel() |
| 146 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 147 | |
| 148 | class AbstractServer: |
| Victor Stinner | cf6f72e | 2013-12-03 18:23:52 +0100 | [diff] [blame] | 149 | """Abstract server returned by create_server().""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 150 | |
| 151 | def close(self): |
| 152 | """Stop serving. This leaves existing connections open.""" |
| 153 | return NotImplemented |
| 154 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 155 | async def wait_closed(self): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 156 | """Coroutine to wait until service is closed.""" |
| 157 | return NotImplemented |
| 158 | |
| 159 | |
| 160 | class AbstractEventLoop: |
| 161 | """Abstract event loop.""" |
| 162 | |
| 163 | # Running and stopping the event loop. |
| 164 | |
| 165 | def run_forever(self): |
| 166 | """Run the event loop until stop() is called.""" |
| 167 | raise NotImplementedError |
| 168 | |
| 169 | def run_until_complete(self, future): |
| 170 | """Run the event loop until a Future is done. |
| 171 | |
| 172 | Return the Future's result, or raise its exception. |
| 173 | """ |
| 174 | raise NotImplementedError |
| 175 | |
| 176 | def stop(self): |
| 177 | """Stop the event loop as soon as reasonable. |
| 178 | |
| 179 | Exactly how soon that is may depend on the implementation, but |
| 180 | no more I/O callbacks should be scheduled. |
| 181 | """ |
| 182 | raise NotImplementedError |
| 183 | |
| 184 | def is_running(self): |
| 185 | """Return whether the event loop is currently running.""" |
| 186 | raise NotImplementedError |
| 187 | |
| Victor Stinner | 896a25a | 2014-07-08 11:29:25 +0200 | [diff] [blame] | 188 | def is_closed(self): |
| 189 | """Returns True if the event loop was closed.""" |
| 190 | raise NotImplementedError |
| 191 | |
| Guido van Rossum | e3f52ef | 2013-11-01 14:19:04 -0700 | [diff] [blame] | 192 | def close(self): |
| 193 | """Close the loop. |
| 194 | |
| 195 | The loop should not be running. |
| 196 | |
| 197 | This is idempotent and irreversible. |
| 198 | |
| 199 | No other methods should be called after this one. |
| 200 | """ |
| 201 | raise NotImplementedError |
| 202 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 203 | async def shutdown_asyncgens(self): |
| Yury Selivanov | f6d991d | 2016-09-15 13:10:51 -0400 | [diff] [blame] | 204 | """Shutdown all active asynchronous generators.""" |
| 205 | raise NotImplementedError |
| 206 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 207 | # Methods scheduling callbacks. All these return Handles. |
| 208 | |
| Yury Selivanov | 592ada9 | 2014-09-25 12:07:56 -0400 | [diff] [blame] | 209 | def _timer_handle_cancelled(self, handle): |
| 210 | """Notification that a TimerHandle has been cancelled.""" |
| 211 | raise NotImplementedError |
| 212 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 213 | def call_soon(self, callback, *args): |
| 214 | return self.call_later(0, callback, *args) |
| 215 | |
| 216 | def call_later(self, delay, callback, *args): |
| 217 | raise NotImplementedError |
| 218 | |
| 219 | def call_at(self, when, callback, *args): |
| 220 | raise NotImplementedError |
| 221 | |
| 222 | def time(self): |
| 223 | raise NotImplementedError |
| 224 | |
| Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 225 | def create_future(self): |
| 226 | raise NotImplementedError |
| 227 | |
| Victor Stinner | 896a25a | 2014-07-08 11:29:25 +0200 | [diff] [blame] | 228 | # Method scheduling a coroutine object: create a task. |
| 229 | |
| 230 | def create_task(self, coro): |
| 231 | raise NotImplementedError |
| 232 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 233 | # Methods for interacting with threads. |
| 234 | |
| 235 | def call_soon_threadsafe(self, callback, *args): |
| 236 | raise NotImplementedError |
| 237 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 238 | async def run_in_executor(self, executor, func, *args): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 239 | raise NotImplementedError |
| 240 | |
| 241 | def set_default_executor(self, executor): |
| 242 | raise NotImplementedError |
| 243 | |
| 244 | # Network I/O methods returning Futures. |
| 245 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 246 | async def getaddrinfo(self, host, port, *, |
| 247 | family=0, type=0, proto=0, flags=0): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 248 | raise NotImplementedError |
| 249 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 250 | async def getnameinfo(self, sockaddr, flags=0): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 251 | raise NotImplementedError |
| 252 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 253 | async def create_connection(self, protocol_factory, host=None, port=None, |
| 254 | *, ssl=None, family=0, proto=0, |
| 255 | flags=0, sock=None, local_addr=None, |
| 256 | server_hostname=None): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 257 | raise NotImplementedError |
| 258 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 259 | async def create_server(self, protocol_factory, host=None, port=None, |
| 260 | *, family=socket.AF_UNSPEC, |
| 261 | flags=socket.AI_PASSIVE, sock=None, backlog=100, |
| 262 | ssl=None, reuse_address=None, reuse_port=None): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 263 | """A coroutine which creates a TCP server bound to host and port. |
| 264 | |
| 265 | The return value is a Server object which can be used to stop |
| 266 | the service. |
| 267 | |
| 268 | If host is an empty string or None all interfaces are assumed |
| 269 | and a list of multiple sockets will be returned (most likely |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 270 | one for IPv4 and another one for IPv6). The host parameter can also be |
| 271 | a sequence (e.g. list) of hosts to bind to. |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 272 | |
| 273 | family can be set to either AF_INET or AF_INET6 to force the |
| 274 | socket to use IPv4 or IPv6. If not set it will be determined |
| 275 | from host (defaults to AF_UNSPEC). |
| 276 | |
| 277 | flags is a bitmask for getaddrinfo(). |
| 278 | |
| 279 | sock can optionally be specified in order to use a preexisting |
| 280 | socket object. |
| 281 | |
| 282 | backlog is the maximum number of queued connections passed to |
| 283 | listen() (defaults to 100). |
| 284 | |
| 285 | ssl can be set to an SSLContext to enable SSL over the |
| 286 | accepted connections. |
| 287 | |
| 288 | reuse_address tells the kernel to reuse a local socket in |
| 289 | TIME_WAIT state, without waiting for its natural timeout to |
| 290 | expire. If not specified will automatically be set to True on |
| 291 | UNIX. |
| Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 292 | |
| 293 | reuse_port tells the kernel to allow this endpoint to be bound to |
| 294 | the same port as other existing endpoints are bound to, so long as |
| 295 | they all set this flag when being created. This option is not |
| 296 | supported on Windows. |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 297 | """ |
| 298 | raise NotImplementedError |
| 299 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 300 | async def create_unix_connection(self, protocol_factory, path=None, *, |
| 301 | ssl=None, sock=None, |
| 302 | server_hostname=None): |
| Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 303 | raise NotImplementedError |
| 304 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 305 | async def create_unix_server(self, protocol_factory, path=None, *, |
| 306 | sock=None, backlog=100, ssl=None): |
| Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 307 | """A coroutine which creates a UNIX Domain Socket server. |
| 308 | |
| Yury Selivanov | dec1a45 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 309 | The return value is a Server object, which can be used to stop |
| Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 310 | the service. |
| 311 | |
| 312 | path is a str, representing a file systsem path to bind the |
| 313 | server socket to. |
| 314 | |
| 315 | sock can optionally be specified in order to use a preexisting |
| 316 | socket object. |
| 317 | |
| 318 | backlog is the maximum number of queued connections passed to |
| 319 | listen() (defaults to 100). |
| 320 | |
| 321 | ssl can be set to an SSLContext to enable SSL over the |
| 322 | accepted connections. |
| 323 | """ |
| 324 | raise NotImplementedError |
| 325 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 326 | async def create_datagram_endpoint(self, protocol_factory, |
| 327 | local_addr=None, remote_addr=None, *, |
| 328 | family=0, proto=0, flags=0, |
| 329 | reuse_address=None, reuse_port=None, |
| 330 | allow_broadcast=None, sock=None): |
| Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 331 | """A coroutine which creates a datagram endpoint. |
| 332 | |
| 333 | This method will try to establish the endpoint in the background. |
| 334 | When successful, the coroutine returns a (transport, protocol) pair. |
| 335 | |
| 336 | protocol_factory must be a callable returning a protocol instance. |
| 337 | |
| Quentin Dawans | fe4ea9c | 2017-10-30 14:43:02 +0100 | [diff] [blame] | 338 | socket family AF_INET, socket.AF_INET6 or socket.AF_UNIX depending on |
| 339 | host (or family if specified), socket type SOCK_DGRAM. |
| Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 340 | |
| 341 | reuse_address tells the kernel to reuse a local socket in |
| 342 | TIME_WAIT state, without waiting for its natural timeout to |
| 343 | expire. If not specified it will automatically be set to True on |
| 344 | UNIX. |
| 345 | |
| 346 | reuse_port tells the kernel to allow this endpoint to be bound to |
| 347 | the same port as other existing endpoints are bound to, so long as |
| 348 | they all set this flag when being created. This option is not |
| 349 | supported on Windows and some UNIX's. If the |
| 350 | :py:data:`~socket.SO_REUSEPORT` constant is not defined then this |
| 351 | capability is unsupported. |
| 352 | |
| 353 | allow_broadcast tells the kernel to allow this endpoint to send |
| 354 | messages to the broadcast address. |
| 355 | |
| 356 | sock can optionally be specified in order to use a preexisting |
| 357 | socket object. |
| 358 | """ |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 359 | raise NotImplementedError |
| 360 | |
| Guido van Rossum | e3f52ef | 2013-11-01 14:19:04 -0700 | [diff] [blame] | 361 | # Pipes and subprocesses. |
| 362 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 363 | async def connect_read_pipe(self, protocol_factory, pipe): |
| Victor Stinner | a5b257a | 2014-05-29 00:14:03 +0200 | [diff] [blame] | 364 | """Register read pipe in event loop. Set the pipe to non-blocking mode. |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 365 | |
| 366 | protocol_factory should instantiate object with Protocol interface. |
| Victor Stinner | a5b257a | 2014-05-29 00:14:03 +0200 | [diff] [blame] | 367 | pipe is a file-like object. |
| 368 | Return pair (transport, protocol), where transport supports the |
| Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 369 | ReadTransport interface.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 370 | # The reason to accept file-like object instead of just file descriptor |
| 371 | # is: we need to own pipe and close it at transport finishing |
| 372 | # Can got complicated errors if pass f.fileno(), |
| 373 | # close fd in pipe transport then close f and vise versa. |
| 374 | raise NotImplementedError |
| 375 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 376 | async def connect_write_pipe(self, protocol_factory, pipe): |
| Yury Selivanov | dec1a45 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 377 | """Register write pipe in event loop. |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 378 | |
| 379 | protocol_factory should instantiate object with BaseProtocol interface. |
| 380 | Pipe is file-like object already switched to nonblocking. |
| 381 | Return pair (transport, protocol), where transport support |
| Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 382 | WriteTransport interface.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 383 | # The reason to accept file-like object instead of just file descriptor |
| 384 | # is: we need to own pipe and close it at transport finishing |
| 385 | # Can got complicated errors if pass f.fileno(), |
| 386 | # close fd in pipe transport then close f and vise versa. |
| 387 | raise NotImplementedError |
| 388 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 389 | async def subprocess_shell(self, protocol_factory, cmd, *, |
| 390 | stdin=subprocess.PIPE, |
| 391 | stdout=subprocess.PIPE, |
| 392 | stderr=subprocess.PIPE, |
| 393 | **kwargs): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 394 | raise NotImplementedError |
| 395 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 396 | async def subprocess_exec(self, protocol_factory, *args, |
| 397 | stdin=subprocess.PIPE, |
| 398 | stdout=subprocess.PIPE, |
| 399 | stderr=subprocess.PIPE, |
| 400 | **kwargs): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 401 | raise NotImplementedError |
| 402 | |
| 403 | # Ready-based callback registration methods. |
| 404 | # The add_*() methods return None. |
| 405 | # The remove_*() methods return True if something was removed, |
| 406 | # False if there was nothing to delete. |
| 407 | |
| 408 | def add_reader(self, fd, callback, *args): |
| 409 | raise NotImplementedError |
| 410 | |
| 411 | def remove_reader(self, fd): |
| 412 | raise NotImplementedError |
| 413 | |
| 414 | def add_writer(self, fd, callback, *args): |
| 415 | raise NotImplementedError |
| 416 | |
| 417 | def remove_writer(self, fd): |
| 418 | raise NotImplementedError |
| 419 | |
| 420 | # Completion based I/O methods returning Futures. |
| 421 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 422 | async def sock_recv(self, sock, nbytes): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 423 | raise NotImplementedError |
| 424 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 425 | async def sock_recv_into(self, sock, buf): |
| Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 426 | raise NotImplementedError |
| 427 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 428 | async def sock_sendall(self, sock, data): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 429 | raise NotImplementedError |
| 430 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 431 | async def sock_connect(self, sock, address): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 432 | raise NotImplementedError |
| 433 | |
| Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 434 | async def sock_accept(self, sock): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 435 | raise NotImplementedError |
| 436 | |
| 437 | # Signal handling. |
| 438 | |
| 439 | def add_signal_handler(self, sig, callback, *args): |
| 440 | raise NotImplementedError |
| 441 | |
| 442 | def remove_signal_handler(self, sig): |
| 443 | raise NotImplementedError |
| 444 | |
| Yury Selivanov | 740169c | 2015-05-11 14:23:38 -0400 | [diff] [blame] | 445 | # Task factory. |
| 446 | |
| 447 | def set_task_factory(self, factory): |
| 448 | raise NotImplementedError |
| 449 | |
| 450 | def get_task_factory(self): |
| 451 | raise NotImplementedError |
| 452 | |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 453 | # Error handlers. |
| 454 | |
| Yury Selivanov | 7ed7ce6 | 2016-05-16 15:20:38 -0400 | [diff] [blame] | 455 | def get_exception_handler(self): |
| 456 | raise NotImplementedError |
| 457 | |
| Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 458 | def set_exception_handler(self, handler): |
| 459 | raise NotImplementedError |
| 460 | |
| 461 | def default_exception_handler(self, context): |
| 462 | raise NotImplementedError |
| 463 | |
| 464 | def call_exception_handler(self, context): |
| 465 | raise NotImplementedError |
| 466 | |
| Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 467 | # Debug flag management. |
| 468 | |
| 469 | def get_debug(self): |
| 470 | raise NotImplementedError |
| 471 | |
| 472 | def set_debug(self, enabled): |
| 473 | raise NotImplementedError |
| 474 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 475 | |
| 476 | class AbstractEventLoopPolicy: |
| 477 | """Abstract policy for accessing the event loop.""" |
| 478 | |
| 479 | def get_event_loop(self): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 480 | """Get the event loop for the current context. |
| 481 | |
| 482 | Returns an event loop object implementing the BaseEventLoop interface, |
| 483 | or raises an exception in case no event loop has been set for the |
| 484 | current context and the current policy does not specify to create one. |
| 485 | |
| 486 | It should never return None.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 487 | raise NotImplementedError |
| 488 | |
| 489 | def set_event_loop(self, loop): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 490 | """Set the event loop for the current context to loop.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 491 | raise NotImplementedError |
| 492 | |
| 493 | def new_event_loop(self): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 494 | """Create and return a new event loop object according to this |
| 495 | policy's rules. If there's need to set this loop as the event loop for |
| 496 | the current context, set_event_loop must be called explicitly.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 497 | raise NotImplementedError |
| 498 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 499 | # Child processes handling (Unix only). |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 500 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 501 | def get_child_watcher(self): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 502 | "Get the watcher for child processes." |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 503 | raise NotImplementedError |
| 504 | |
| 505 | def set_child_watcher(self, watcher): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 506 | """Set the watcher for child processes.""" |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 507 | raise NotImplementedError |
| 508 | |
| 509 | |
| 510 | class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy): |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 511 | """Default policy implementation for accessing the event loop. |
| 512 | |
| 513 | In this policy, each thread has its own event loop. However, we |
| 514 | only automatically create an event loop by default for the main |
| 515 | thread; other threads by default have no event loop. |
| 516 | |
| 517 | Other policies may have different rules (e.g. a single global |
| 518 | event loop, or automatically creating an event loop per thread, or |
| 519 | using some other notion of context to which an event loop is |
| 520 | associated). |
| 521 | """ |
| 522 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 523 | _loop_factory = None |
| 524 | |
| 525 | class _Local(threading.local): |
| 526 | _loop = None |
| 527 | _set_called = False |
| 528 | |
| 529 | def __init__(self): |
| 530 | self._local = self._Local() |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 531 | |
| 532 | def get_event_loop(self): |
| 533 | """Get the event loop. |
| 534 | |
| 535 | This may be None or an instance of EventLoop. |
| 536 | """ |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 537 | if (self._local._loop is None and |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 538 | not self._local._set_called and |
| 539 | isinstance(threading.current_thread(), threading._MainThread)): |
| Guido van Rossum | cced076 | 2013-11-27 10:37:13 -0800 | [diff] [blame] | 540 | self.set_event_loop(self.new_event_loop()) |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 541 | |
| Victor Stinner | 3a1c738 | 2014-12-18 01:20:10 +0100 | [diff] [blame] | 542 | if self._local._loop is None: |
| 543 | raise RuntimeError('There is no current event loop in thread %r.' |
| 544 | % threading.current_thread().name) |
| Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 545 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 546 | return self._local._loop |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 547 | |
| 548 | def set_event_loop(self, loop): |
| 549 | """Set the event loop.""" |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 550 | self._local._set_called = True |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 551 | assert loop is None or isinstance(loop, AbstractEventLoop) |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 552 | self._local._loop = loop |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 553 | |
| 554 | def new_event_loop(self): |
| 555 | """Create a new event loop. |
| 556 | |
| 557 | You must call set_event_loop() to make this the current event |
| 558 | loop. |
| 559 | """ |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 560 | return self._loop_factory() |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 561 | |
| 562 | |
| 563 | # Event loop policy. The policy itself is always global, even if the |
| 564 | # policy's rules say that there is an event loop per thread (or other |
| 565 | # notion of context). The default policy is installed by the first |
| 566 | # call to get_event_loop_policy(). |
| 567 | _event_loop_policy = None |
| 568 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 569 | # Lock for protecting the on-the-fly creation of the event loop policy. |
| 570 | _lock = threading.Lock() |
| 571 | |
| 572 | |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 573 | # A TLS for the running event loop, used by _get_running_loop. |
| 574 | class _RunningLoop(threading.local): |
| jimmylai | 80bbe6a7 | 2017-09-05 17:36:59 -0700 | [diff] [blame] | 575 | loop_pid = (None, None) |
| Yury Selivanov | ba7e1f9 | 2017-03-02 20:07:11 -0500 | [diff] [blame] | 576 | |
| 577 | |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 578 | _running_loop = _RunningLoop() |
| 579 | |
| 580 | |
| Yury Selivanov | abae67e | 2017-12-11 10:07:44 -0500 | [diff] [blame] | 581 | def get_running_loop(): |
| 582 | """Return the running event loop. Raise a RuntimeError if there is none. |
| 583 | |
| 584 | This function is thread-specific. |
| 585 | """ |
| Yury Selivanov | a70232f | 2017-12-13 14:49:42 -0500 | [diff] [blame] | 586 | # NOTE: this function is implemented in C (see _asynciomodule.c) |
| Yury Selivanov | abae67e | 2017-12-11 10:07:44 -0500 | [diff] [blame] | 587 | loop = _get_running_loop() |
| 588 | if loop is None: |
| 589 | raise RuntimeError('no running event loop') |
| 590 | return loop |
| 591 | |
| 592 | |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 593 | def _get_running_loop(): |
| 594 | """Return the running event loop or None. |
| 595 | |
| 596 | This is a low-level function intended to be used by event loops. |
| 597 | This function is thread-specific. |
| 598 | """ |
| Yury Selivanov | a70232f | 2017-12-13 14:49:42 -0500 | [diff] [blame] | 599 | # NOTE: this function is implemented in C (see _asynciomodule.c) |
| jimmylai | 80bbe6a7 | 2017-09-05 17:36:59 -0700 | [diff] [blame] | 600 | running_loop, pid = _running_loop.loop_pid |
| 601 | if running_loop is not None and pid == os.getpid(): |
| Yury Selivanov | 902e9c5 | 2017-03-02 23:57:33 -0500 | [diff] [blame] | 602 | return running_loop |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 603 | |
| 604 | |
| 605 | def _set_running_loop(loop): |
| 606 | """Set the running event loop. |
| 607 | |
| 608 | This is a low-level function intended to be used by event loops. |
| 609 | This function is thread-specific. |
| 610 | """ |
| Yury Selivanov | a70232f | 2017-12-13 14:49:42 -0500 | [diff] [blame] | 611 | # NOTE: this function is implemented in C (see _asynciomodule.c) |
| jimmylai | 80bbe6a7 | 2017-09-05 17:36:59 -0700 | [diff] [blame] | 612 | _running_loop.loop_pid = (loop, os.getpid()) |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 613 | |
| 614 | |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 615 | def _init_event_loop_policy(): |
| 616 | global _event_loop_policy |
| 617 | with _lock: |
| 618 | if _event_loop_policy is None: # pragma: no branch |
| 619 | from . import DefaultEventLoopPolicy |
| 620 | _event_loop_policy = DefaultEventLoopPolicy() |
| 621 | |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 622 | |
| 623 | def get_event_loop_policy(): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 624 | """Get the current event loop policy.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 625 | if _event_loop_policy is None: |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 626 | _init_event_loop_policy() |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 627 | return _event_loop_policy |
| 628 | |
| 629 | |
| 630 | def set_event_loop_policy(policy): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 631 | """Set the current event loop policy. |
| 632 | |
| 633 | If policy is None, the default policy is restored.""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 634 | global _event_loop_policy |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 635 | assert policy is None or isinstance(policy, AbstractEventLoopPolicy) |
| 636 | _event_loop_policy = policy |
| 637 | |
| 638 | |
| 639 | def get_event_loop(): |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 640 | """Return an asyncio event loop. |
| 641 | |
| 642 | When called from a coroutine or a callback (e.g. scheduled with call_soon |
| 643 | or similar API), this function will always return the running event loop. |
| 644 | |
| 645 | If there is no running event loop set, the function will return |
| 646 | the result of `get_event_loop_policy().get_event_loop()` call. |
| 647 | """ |
| Yury Selivanov | a70232f | 2017-12-13 14:49:42 -0500 | [diff] [blame] | 648 | # NOTE: this function is implemented in C (see _asynciomodule.c) |
| Yury Selivanov | 600a349 | 2016-11-04 14:29:28 -0400 | [diff] [blame] | 649 | current_loop = _get_running_loop() |
| 650 | if current_loop is not None: |
| 651 | return current_loop |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 652 | return get_event_loop_policy().get_event_loop() |
| 653 | |
| 654 | |
| 655 | def set_event_loop(loop): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 656 | """Equivalent to calling get_event_loop_policy().set_event_loop(loop).""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 657 | get_event_loop_policy().set_event_loop(loop) |
| 658 | |
| 659 | |
| 660 | def new_event_loop(): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 661 | """Equivalent to calling get_event_loop_policy().new_event_loop().""" |
| Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 662 | return get_event_loop_policy().new_event_loop() |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 663 | |
| 664 | |
| 665 | def get_child_watcher(): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 666 | """Equivalent to calling get_event_loop_policy().get_child_watcher().""" |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 667 | return get_event_loop_policy().get_child_watcher() |
| 668 | |
| 669 | |
| 670 | def set_child_watcher(watcher): |
| Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 671 | """Equivalent to calling |
| 672 | get_event_loop_policy().set_child_watcher(watcher).""" |
| Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 673 | return get_event_loop_policy().set_child_watcher(watcher) |
| Yury Selivanov | a70232f | 2017-12-13 14:49:42 -0500 | [diff] [blame] | 674 | |
| 675 | |
| 676 | # Alias pure-Python implementations for testing purposes. |
| 677 | _py__get_running_loop = _get_running_loop |
| 678 | _py__set_running_loop = _set_running_loop |
| 679 | _py_get_running_loop = get_running_loop |
| 680 | _py_get_event_loop = get_event_loop |
| 681 | |
| 682 | |
| 683 | try: |
| 684 | # get_event_loop() is one of the most frequently called |
| 685 | # functions in asyncio. Pure Python implementation is |
| 686 | # about 4 times slower than C-accelerated. |
| 687 | from _asyncio import (_get_running_loop, _set_running_loop, |
| 688 | get_running_loop, get_event_loop) |
| 689 | except ImportError: |
| 690 | pass |
| 691 | else: |
| 692 | # Alias C implementations for testing purposes. |
| 693 | _c__get_running_loop = _get_running_loop |
| 694 | _c__set_running_loop = _set_running_loop |
| 695 | _c_get_running_loop = get_running_loop |
| 696 | _c_get_event_loop = get_event_loop |