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