R David Murray | 6a14381 | 2013-12-20 14:37:39 -0500 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 2 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 3 | .. _asyncio-event-loop: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 4 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 5 | Base Event Loop |
| 6 | =============== |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 7 | |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 8 | **Source code:** :source:`Lib/asyncio/events.py` |
| 9 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 10 | The event loop is the central execution device provided by :mod:`asyncio`. |
Zachary Ware | 5e580da | 2015-08-27 15:54:39 -0500 | [diff] [blame] | 11 | It provides multiple facilities, including: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 12 | |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 13 | * Registering, executing and cancelling delayed calls (timeouts). |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 14 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 15 | * Creating client and server :ref:`transports <asyncio-transport>` for various |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 16 | kinds of communication. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 17 | |
Eli Bendersky | 136fea2 | 2014-02-09 06:55:58 -0800 | [diff] [blame] | 18 | * Launching subprocesses and the associated :ref:`transports |
| 19 | <asyncio-transport>` for communication with an external program. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 20 | |
Eli Bendersky | b73c833 | 2014-02-09 06:07:47 -0800 | [diff] [blame] | 21 | * Delegating costly function calls to a pool of threads. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 22 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 23 | .. class:: BaseEventLoop |
Eli Bendersky | 136fea2 | 2014-02-09 06:55:58 -0800 | [diff] [blame] | 24 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 25 | This class is an implementation detail. It is a subclass of |
| 26 | :class:`AbstractEventLoop` and may be a base class of concrete |
| 27 | event loop implementations found in :mod:`asyncio`. It should not |
| 28 | be used directly; use :class:`AbstractEventLoop` instead. |
| 29 | ``BaseEventLoop`` should not be subclassed by third-party code; the |
| 30 | internal interface is not stable. |
| 31 | |
| 32 | .. class:: AbstractEventLoop |
| 33 | |
| 34 | Abstract base class of event loops. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 35 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 36 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 37 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 38 | Run an event loop |
| 39 | ----------------- |
| 40 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 41 | .. method:: AbstractEventLoop.run_forever() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 42 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 43 | Run until :meth:`stop` is called. If :meth:`stop` is called before |
| 44 | :meth:`run_forever()` is called, this polls the I/O selector once |
| 45 | with a timeout of zero, runs all callbacks scheduled in response to |
| 46 | I/O events (and those that were already scheduled), and then exits. |
| 47 | If :meth:`stop` is called while :meth:`run_forever` is running, |
| 48 | this will run the current batch of callbacks and then exit. Note |
| 49 | that callbacks scheduled by callbacks will not run in that case; |
| 50 | they will run the next time :meth:`run_forever` is called. |
| 51 | |
Guido van Rossum | 82f9fea | 2015-11-19 13:33:34 -0800 | [diff] [blame] | 52 | .. versionchanged:: 3.5.1 |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 53 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 54 | .. method:: AbstractEventLoop.run_until_complete(future) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 55 | |
Victor Stinner | 99c2ab4 | 2013-12-03 19:17:25 +0100 | [diff] [blame] | 56 | Run until the :class:`Future` is done. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 57 | |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 58 | If the argument is a :ref:`coroutine object <coroutine>`, it is wrapped by |
Yury Selivanov | 04356e1 | 2015-06-30 22:13:22 -0400 | [diff] [blame] | 59 | :func:`ensure_future`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 60 | |
| 61 | Return the Future's result, or raise its exception. |
| 62 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 63 | .. method:: AbstractEventLoop.is_running() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 64 | |
| 65 | Returns running status of event loop. |
| 66 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 67 | .. method:: AbstractEventLoop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 68 | |
| 69 | Stop running the event loop. |
| 70 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 71 | This causes :meth:`run_forever` to exit at the next suitable |
| 72 | opportunity (see there for more details). |
| 73 | |
Guido van Rossum | 82f9fea | 2015-11-19 13:33:34 -0800 | [diff] [blame] | 74 | .. versionchanged:: 3.5.1 |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 75 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 76 | .. method:: AbstractEventLoop.is_closed() |
Victor Stinner | bb2fc5b | 2014-06-10 10:23:10 +0200 | [diff] [blame] | 77 | |
| 78 | Returns ``True`` if the event loop was closed. |
| 79 | |
| 80 | .. versionadded:: 3.4.2 |
| 81 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 82 | .. method:: AbstractEventLoop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 83 | |
Guido van Rossum | 41f69f4 | 2015-11-19 13:28:47 -0800 | [diff] [blame] | 84 | Close the event loop. The loop must not be running. Pending |
| 85 | callbacks will be lost. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 86 | |
| 87 | This clears the queues and shuts down the executor, but does not wait for |
| 88 | the executor to finish. |
| 89 | |
| 90 | This is idempotent and irreversible. No other methods should be called after |
| 91 | this one. |
| 92 | |
Yury Selivanov | 0366004 | 2016-12-15 17:36:05 -0500 | [diff] [blame] | 93 | |
| 94 | .. coroutinemethod:: AbstractEventLoop.shutdown_asyncgens() |
| 95 | |
| 96 | Schedule all currently open :term:`asynchronous generator` objects to |
| 97 | close with an :meth:`~agen.aclose()` call. After calling this method, |
| 98 | the event loop will issue a warning whenever a new asynchronous generator |
| 99 | is iterated. Should be used to finalize all scheduled asynchronous |
| 100 | generators reliably. Example:: |
| 101 | |
| 102 | try: |
| 103 | loop.run_forever() |
| 104 | finally: |
| 105 | loop.run_until_complete(loop.shutdown_asyncgens()) |
| 106 | loop.close() |
| 107 | |
| 108 | .. versionadded:: 3.6 |
| 109 | |
| 110 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 111 | .. _asyncio-pass-keywords: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 112 | |
| 113 | Calls |
| 114 | ----- |
| 115 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 116 | Most :mod:`asyncio` functions don't accept keywords. If you want to pass |
| 117 | keywords to your callback, use :func:`functools.partial`. For example, |
| 118 | ``loop.call_soon(functools.partial(print, "Hello", flush=True))`` will call |
| 119 | ``print("Hello", flush=True)``. |
| 120 | |
| 121 | .. note:: |
| 122 | :func:`functools.partial` is better than ``lambda`` functions, because |
| 123 | :mod:`asyncio` can inspect :func:`functools.partial` object to display |
| 124 | parameters in debug mode, whereas ``lambda`` functions have a poor |
| 125 | representation. |
| 126 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 127 | .. method:: AbstractEventLoop.call_soon(callback, \*args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 128 | |
Victor Stinner | 4d5115c | 2014-12-15 17:50:55 +0100 | [diff] [blame] | 129 | Arrange for a callback to be called as soon as possible. The callback is |
| 130 | called after :meth:`call_soon` returns, when control returns to the event |
| 131 | loop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 132 | |
Serhiy Storchaka | 4ecfa45 | 2016-05-16 09:31:54 +0300 | [diff] [blame] | 133 | This operates as a :abbr:`FIFO (first-in, first-out)` queue, callbacks |
| 134 | are called in the order in which they are registered. Each callback |
| 135 | will be called exactly once. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 136 | |
| 137 | Any positional arguments after the callback will be passed to the |
| 138 | callback when it is called. |
| 139 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 140 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 141 | used to cancel the callback. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 142 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 143 | :ref:`Use functools.partial to pass keywords to the callback |
| 144 | <asyncio-pass-keywords>`. |
| 145 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 146 | .. method:: AbstractEventLoop.call_soon_threadsafe(callback, \*args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 147 | |
| 148 | Like :meth:`call_soon`, but thread safe. |
| 149 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 150 | See the :ref:`concurrency and multithreading <asyncio-multithreading>` |
| 151 | section of the documentation. |
| 152 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 153 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 154 | .. _asyncio-delayed-calls: |
| 155 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 156 | Delayed calls |
| 157 | ------------- |
| 158 | |
| 159 | The event loop has its own internal clock for computing timeouts. |
| 160 | Which clock is used depends on the (platform-specific) event loop |
| 161 | implementation; ideally it is a monotonic clock. This will generally be |
| 162 | a different clock than :func:`time.time`. |
| 163 | |
Victor Stinner | fd9d374 | 2014-02-18 09:37:43 +0100 | [diff] [blame] | 164 | .. note:: |
| 165 | |
| 166 | Timeouts (relative *delay* or absolute *when*) should not exceed one day. |
| 167 | |
Victor Stinner | 45b27ed | 2014-02-01 02:36:43 +0100 | [diff] [blame] | 168 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 169 | .. method:: AbstractEventLoop.call_later(delay, callback, *args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 170 | |
| 171 | Arrange for the *callback* to be called after the given *delay* |
| 172 | seconds (either an int or float). |
| 173 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 174 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 175 | used to cancel the callback. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 176 | |
| 177 | *callback* will be called exactly once per call to :meth:`call_later`. |
| 178 | If two callbacks are scheduled for exactly the same time, it is |
| 179 | undefined which will be called first. |
| 180 | |
| 181 | The optional positional *args* will be passed to the callback when it |
| 182 | is called. If you want the callback to be called with some named |
| 183 | arguments, use a closure or :func:`functools.partial`. |
| 184 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 185 | :ref:`Use functools.partial to pass keywords to the callback |
| 186 | <asyncio-pass-keywords>`. |
| 187 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 188 | .. method:: AbstractEventLoop.call_at(when, callback, *args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 189 | |
| 190 | Arrange for the *callback* to be called at the given absolute timestamp |
Berker Peksag | b556399 | 2014-11-07 19:51:07 +0200 | [diff] [blame] | 191 | *when* (an int or float), using the same time reference as |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 192 | :meth:`AbstractEventLoop.time`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 193 | |
| 194 | This method's behavior is the same as :meth:`call_later`. |
| 195 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 196 | An instance of :class:`asyncio.Handle` is returned, which can be |
| 197 | used to cancel the callback. |
| 198 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 199 | :ref:`Use functools.partial to pass keywords to the callback |
| 200 | <asyncio-pass-keywords>`. |
| 201 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 202 | .. method:: AbstractEventLoop.time() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 203 | |
| 204 | Return the current time, as a :class:`float` value, according to the |
| 205 | event loop's internal clock. |
| 206 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 207 | .. seealso:: |
| 208 | |
| 209 | The :func:`asyncio.sleep` function. |
| 210 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 211 | |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 212 | Futures |
| 213 | ------- |
| 214 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 215 | .. method:: AbstractEventLoop.create_future() |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 216 | |
| 217 | Create an :class:`asyncio.Future` object attached to the loop. |
| 218 | |
| 219 | This is a preferred way to create futures in asyncio, as event |
| 220 | loop implementations can provide alternative implementations |
| 221 | of the Future class (with better performance or instrumentation). |
| 222 | |
| 223 | .. versionadded:: 3.5.2 |
| 224 | |
| 225 | |
Yury Selivanov | bb96134 | 2015-06-25 11:54:34 -0400 | [diff] [blame] | 226 | Tasks |
| 227 | ----- |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 228 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 229 | .. method:: AbstractEventLoop.create_task(coro) |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 230 | |
| 231 | Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in |
| 232 | a future. Return a :class:`Task` object. |
| 233 | |
| 234 | Third-party event loops can use their own subclass of :class:`Task` for |
| 235 | interoperability. In this case, the result type is a subclass of |
| 236 | :class:`Task`. |
| 237 | |
Victor Stinner | 337e03f | 2014-08-11 01:11:13 +0200 | [diff] [blame] | 238 | This method was added in Python 3.4.2. Use the :func:`async` function to |
| 239 | support also older Python versions. |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 240 | |
| 241 | .. versionadded:: 3.4.2 |
| 242 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 243 | .. method:: AbstractEventLoop.set_task_factory(factory) |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 244 | |
| 245 | Set a task factory that will be used by |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 246 | :meth:`AbstractEventLoop.create_task`. |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 247 | |
| 248 | If *factory* is ``None`` the default task factory will be set. |
| 249 | |
| 250 | If *factory* is a *callable*, it should have a signature matching |
| 251 | ``(loop, coro)``, where *loop* will be a reference to the active |
| 252 | event loop, *coro* will be a coroutine object. The callable |
| 253 | must return an :class:`asyncio.Future` compatible object. |
| 254 | |
| 255 | .. versionadded:: 3.4.4 |
| 256 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 257 | .. method:: AbstractEventLoop.get_task_factory() |
Yury Selivanov | 7185461 | 2015-05-11 16:28:27 -0400 | [diff] [blame] | 258 | |
| 259 | Return a task factory, or ``None`` if the default one is in use. |
| 260 | |
| 261 | .. versionadded:: 3.4.4 |
| 262 | |
Victor Stinner | 530ef2f | 2014-07-08 12:39:10 +0200 | [diff] [blame] | 263 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 264 | Creating connections |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 265 | -------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 266 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 267 | .. coroutinemethod:: AbstractEventLoop.create_connection(protocol_factory, host=None, port=None, \*, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 268 | |
| 269 | Create a streaming transport connection to a given Internet *host* and |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 270 | *port*: socket family :py:data:`~socket.AF_INET` or |
| 271 | :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified), |
| 272 | socket type :py:data:`~socket.SOCK_STREAM`. *protocol_factory* must be a |
| 273 | callable returning a :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 274 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 275 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 276 | establish the connection in the background. When successful, the |
| 277 | coroutine returns a ``(transport, protocol)`` pair. |
| 278 | |
| 279 | The chronological synopsis of the underlying operation is as follows: |
| 280 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 281 | #. The connection is established, and a :ref:`transport <asyncio-transport>` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 282 | is created to represent it. |
| 283 | |
| 284 | #. *protocol_factory* is called without arguments and must return a |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 285 | :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 286 | |
| 287 | #. The protocol instance is tied to the transport, and its |
| 288 | :meth:`connection_made` method is called. |
| 289 | |
| 290 | #. The coroutine returns successfully with the ``(transport, protocol)`` |
| 291 | pair. |
| 292 | |
| 293 | The created transport is an implementation-dependent bidirectional stream. |
| 294 | |
| 295 | .. note:: |
| 296 | *protocol_factory* can be any kind of callable, not necessarily |
| 297 | a class. For example, if you want to use a pre-created |
| 298 | protocol instance, you can pass ``lambda: my_protocol``. |
| 299 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 300 | Options that change how the connection is created: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 301 | |
| 302 | * *ssl*: if given and not false, a SSL/TLS transport is created |
| 303 | (by default a plain TCP transport is created). If *ssl* is |
| 304 | a :class:`ssl.SSLContext` object, this context is used to create |
| 305 | the transport; if *ssl* is :const:`True`, a context with some |
| 306 | unspecified default settings is used. |
| 307 | |
Berker Peksag | 9c1dba2 | 2014-09-28 00:00:58 +0300 | [diff] [blame] | 308 | .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>` |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 309 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 310 | * *server_hostname*, is only for use together with *ssl*, |
| 311 | and sets or overrides the hostname that the target server's certificate |
| 312 | will be matched against. By default the value of the *host* argument |
| 313 | is used. If *host* is empty, there is no default and you must pass a |
| 314 | value for *server_hostname*. If *server_hostname* is an empty |
| 315 | string, hostname matching is disabled (which is a serious security |
| 316 | risk, allowing for man-in-the-middle-attacks). |
| 317 | |
| 318 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 319 | and flags to be passed through to getaddrinfo() for *host* resolution. |
| 320 | If given, these should all be integers from the corresponding |
| 321 | :mod:`socket` module constants. |
| 322 | |
| 323 | * *sock*, if given, should be an existing, already connected |
| 324 | :class:`socket.socket` object to be used by the transport. |
| 325 | If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags* |
| 326 | and *local_addr* should be specified. |
| 327 | |
| 328 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
| 329 | to bind the socket to locally. The *local_host* and *local_port* |
| 330 | are looked up using getaddrinfo(), similarly to *host* and *port*. |
| 331 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 332 | .. versionchanged:: 3.5 |
| 333 | |
| 334 | On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 335 | |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 336 | .. seealso:: |
| 337 | |
| 338 | The :func:`open_connection` function can be used to get a pair of |
| 339 | (:class:`StreamReader`, :class:`StreamWriter`) instead of a protocol. |
| 340 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 341 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 342 | .. coroutinemethod:: AbstractEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None) |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 343 | |
Quentin Dawans | fe4ea9c | 2017-10-30 14:43:02 +0100 | [diff] [blame] | 344 | Create datagram connection: socket family :py:data:`~socket.AF_INET`, |
| 345 | :py:data:`~socket.AF_INET6` or :py:data:`~socket.AF_UNIX` depending on |
| 346 | *host* (or *family* if specified), socket type |
| 347 | :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 348 | callable returning a :ref:`protocol <asyncio-protocol>` instance. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 349 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 350 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 351 | establish the connection in the background. When successful, the |
| 352 | coroutine returns a ``(transport, protocol)`` pair. |
| 353 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 354 | Options changing how the connection is created: |
| 355 | |
| 356 | * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used |
| 357 | to bind the socket to locally. The *local_host* and *local_port* |
| 358 | are looked up using :meth:`getaddrinfo`. |
| 359 | |
| 360 | * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used |
| 361 | to connect the socket to a remote address. The *remote_host* and |
| 362 | *remote_port* are looked up using :meth:`getaddrinfo`. |
| 363 | |
| 364 | * *family*, *proto*, *flags* are the optional address family, protocol |
| 365 | and flags to be passed through to :meth:`getaddrinfo` for *host* |
| 366 | resolution. If given, these should all be integers from the |
| 367 | corresponding :mod:`socket` module constants. |
| 368 | |
| 369 | * *reuse_address* tells the kernel to reuse a local socket in |
| 370 | TIME_WAIT state, without waiting for its natural timeout to |
Serhiy Storchaka | 4adf01c | 2016-10-19 18:30:05 +0300 | [diff] [blame] | 371 | expire. If not specified will automatically be set to ``True`` on |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 372 | UNIX. |
| 373 | |
| 374 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 375 | same port as other existing endpoints are bound to, so long as they all |
| 376 | set this flag when being created. This option is not supported on Windows |
| 377 | and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not |
| 378 | defined then this capability is unsupported. |
| 379 | |
| 380 | * *allow_broadcast* tells the kernel to allow this endpoint to send |
| 381 | messages to the broadcast address. |
| 382 | |
| 383 | * *sock* can optionally be specified in order to use a preexisting, |
| 384 | already connected, :class:`socket.socket` object to be used by the |
| 385 | transport. If specified, *local_addr* and *remote_addr* should be omitted |
| 386 | (must be :const:`None`). |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 387 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 388 | On Windows with :class:`ProactorEventLoop`, this method is not supported. |
| 389 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 390 | See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and |
| 391 | :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples. |
| 392 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 393 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 394 | .. coroutinemethod:: AbstractEventLoop.create_unix_connection(protocol_factory, path, \*, ssl=None, sock=None, server_hostname=None) |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 395 | |
| 396 | Create UNIX connection: socket family :py:data:`~socket.AF_UNIX`, socket |
| 397 | type :py:data:`~socket.SOCK_STREAM`. The :py:data:`~socket.AF_UNIX` socket |
| 398 | family is used to communicate between processes on the same machine |
| 399 | efficiently. |
| 400 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 401 | This method is a :ref:`coroutine <coroutine>` which will try to |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 402 | establish the connection in the background. When successful, the |
| 403 | coroutine returns a ``(transport, protocol)`` pair. |
| 404 | |
Guido van Rossum | 9e80eeb | 2016-11-03 14:17:25 -0700 | [diff] [blame] | 405 | *path* is the name of a UNIX domain socket, and is required unless a *sock* |
| 406 | parameter is specified. Abstract UNIX sockets, :class:`str`, and |
| 407 | :class:`bytes` paths are supported. |
| 408 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 409 | See the :meth:`AbstractEventLoop.create_connection` method for parameters. |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 410 | |
| 411 | Availability: UNIX. |
| 412 | |
| 413 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 414 | Creating listening connections |
| 415 | ------------------------------ |
| 416 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 417 | .. coroutinemethod:: AbstractEventLoop.create_server(protocol_factory, host=None, port=None, \*, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 418 | |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 419 | Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) bound to |
| 420 | *host* and *port*. |
| 421 | |
| 422 | Return a :class:`Server` object, its :attr:`~Server.sockets` attribute |
| 423 | contains created sockets. Use the :meth:`Server.close` method to stop the |
| 424 | server: close listening sockets. |
| 425 | |
| 426 | Parameters: |
| 427 | |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 428 | * The *host* parameter can be a string, in that case the TCP server is |
| 429 | bound to *host* and *port*. The *host* parameter can also be a sequence |
| 430 | of strings and in that case the TCP server is bound to all hosts of the |
| 431 | sequence. If *host* is an empty string or ``None``, all interfaces are |
| 432 | assumed and a list of multiple sockets will be returned (most likely one |
| 433 | for IPv4 and another one for IPv6). |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 434 | |
| 435 | * *family* can be set to either :data:`socket.AF_INET` or |
| 436 | :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set |
| 437 | it will be determined from host (defaults to :data:`socket.AF_UNSPEC`). |
| 438 | |
| 439 | * *flags* is a bitmask for :meth:`getaddrinfo`. |
| 440 | |
| 441 | * *sock* can optionally be specified in order to use a preexisting |
| 442 | socket object. If specified, *host* and *port* should be omitted (must be |
| 443 | :const:`None`). |
| 444 | |
| 445 | * *backlog* is the maximum number of queued connections passed to |
| 446 | :meth:`~socket.socket.listen` (defaults to 100). |
| 447 | |
| 448 | * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the |
| 449 | accepted connections. |
| 450 | |
| 451 | * *reuse_address* tells the kernel to reuse a local socket in |
| 452 | TIME_WAIT state, without waiting for its natural timeout to |
Serhiy Storchaka | 4adf01c | 2016-10-19 18:30:05 +0300 | [diff] [blame] | 453 | expire. If not specified will automatically be set to ``True`` on |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 454 | UNIX. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 455 | |
Guido van Rossum | b9bf913 | 2015-10-05 09:15:28 -0700 | [diff] [blame] | 456 | * *reuse_port* tells the kernel to allow this endpoint to be bound to the |
| 457 | same port as other existing endpoints are bound to, so long as they all |
| 458 | set this flag when being created. This option is not supported on |
| 459 | Windows. |
| 460 | |
Victor Stinner | d143209 | 2014-06-19 17:11:49 +0200 | [diff] [blame] | 461 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 462 | |
Victor Stinner | 60208a1 | 2015-09-15 22:41:52 +0200 | [diff] [blame] | 463 | .. versionchanged:: 3.5 |
| 464 | |
| 465 | On Windows with :class:`ProactorEventLoop`, SSL/TLS is now supported. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 466 | |
Victor Stinner | c8ea813 | 2014-01-23 11:02:09 +0100 | [diff] [blame] | 467 | .. seealso:: |
| 468 | |
| 469 | The function :func:`start_server` creates a (:class:`StreamReader`, |
| 470 | :class:`StreamWriter`) pair and calls back a function with this pair. |
| 471 | |
Victor Stinner | 7b58a2b | 2015-09-21 18:41:05 +0200 | [diff] [blame] | 472 | .. versionchanged:: 3.5.1 |
Victor Stinner | 5e4a7d8 | 2015-09-21 18:33:43 +0200 | [diff] [blame] | 473 | |
| 474 | The *host* parameter can now be a sequence of strings. |
| 475 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 476 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 477 | .. coroutinemethod:: AbstractEventLoop.create_unix_server(protocol_factory, path=None, \*, sock=None, backlog=100, ssl=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 478 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 479 | Similar to :meth:`AbstractEventLoop.create_server`, but specific to the |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 480 | socket family :py:data:`~socket.AF_UNIX`. |
| 481 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 482 | This method is a :ref:`coroutine <coroutine>`. |
| 483 | |
Victor Stinner | a6919aa | 2014-02-19 13:32:34 +0100 | [diff] [blame] | 484 | Availability: UNIX. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 485 | |
Yury Selivanov | 3b3a141 | 2016-11-07 15:35:25 -0500 | [diff] [blame] | 486 | .. coroutinemethod:: BaseEventLoop.connect_accepted_socket(protocol_factory, sock, \*, ssl=None) |
| 487 | |
| 488 | Handle an accepted connection. |
| 489 | |
| 490 | This is used by servers that accept connections outside of |
| 491 | asyncio but that use asyncio to handle them. |
| 492 | |
| 493 | Parameters: |
| 494 | |
| 495 | * *sock* is a preexisting socket object returned from an ``accept`` |
| 496 | call. |
| 497 | |
| 498 | * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the |
| 499 | accepted connections. |
| 500 | |
| 501 | This method is a :ref:`coroutine <coroutine>`. When completed, the |
| 502 | coroutine returns a ``(transport, protocol)`` pair. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 503 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 504 | Watch file descriptors |
| 505 | ---------------------- |
| 506 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 507 | On Windows with :class:`SelectorEventLoop`, only socket handles are supported |
| 508 | (ex: pipe file descriptors are not supported). |
| 509 | |
| 510 | On Windows with :class:`ProactorEventLoop`, these methods are not supported. |
| 511 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 512 | .. method:: AbstractEventLoop.add_reader(fd, callback, \*args) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 513 | |
| 514 | Start watching the file descriptor for read availability and then call the |
| 515 | *callback* with specified arguments. |
| 516 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 517 | :ref:`Use functools.partial to pass keywords to the callback |
| 518 | <asyncio-pass-keywords>`. |
| 519 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 520 | .. method:: AbstractEventLoop.remove_reader(fd) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 521 | |
| 522 | Stop watching the file descriptor for read availability. |
| 523 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 524 | .. method:: AbstractEventLoop.add_writer(fd, callback, \*args) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 525 | |
| 526 | Start watching the file descriptor for write availability and then call the |
| 527 | *callback* with specified arguments. |
| 528 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 529 | :ref:`Use functools.partial to pass keywords to the callback |
| 530 | <asyncio-pass-keywords>`. |
| 531 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 532 | .. method:: AbstractEventLoop.remove_writer(fd) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 533 | |
| 534 | Stop watching the file descriptor for write availability. |
| 535 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 536 | The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>` |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 537 | example uses the low-level :meth:`AbstractEventLoop.add_reader` method to register |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 538 | the file descriptor of a socket. |
| 539 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 540 | |
| 541 | Low-level socket operations |
| 542 | --------------------------- |
| 543 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 544 | .. coroutinemethod:: AbstractEventLoop.sock_recv(sock, nbytes) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 545 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 546 | Receive data from the socket. Modeled after blocking |
| 547 | :meth:`socket.socket.recv` method. |
| 548 | |
| 549 | The return value is a bytes object |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 550 | representing the data received. The maximum amount of data to be received |
| 551 | at once is specified by *nbytes*. |
| 552 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 553 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 554 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 555 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 556 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 557 | |
Antoine Pitrou | 525f40d | 2017-10-19 21:46:40 +0200 | [diff] [blame] | 558 | .. coroutinemethod:: AbstractEventLoop.sock_recv_into(sock, buf) |
| 559 | |
| 560 | Receive data from the socket. Modeled after blocking |
| 561 | :meth:`socket.socket.recv_into` method. |
| 562 | |
| 563 | The received data is written into *buf* (a writable buffer). |
| 564 | The return value is the number of bytes written. |
| 565 | |
| 566 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 567 | non-blocking. |
| 568 | |
| 569 | This method is a :ref:`coroutine <coroutine>`. |
| 570 | |
| 571 | .. versionadded:: 3.7 |
| 572 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 573 | .. coroutinemethod:: AbstractEventLoop.sock_sendall(sock, data) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 574 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 575 | Send data to the socket. Modeled after blocking |
| 576 | :meth:`socket.socket.sendall` method. |
| 577 | |
| 578 | The socket must be connected to a remote socket. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 579 | This method continues to send data from *data* until either all data has |
| 580 | been sent or an error occurs. ``None`` is returned on success. On error, |
| 581 | an exception is raised, and there is no way to determine how much data, if |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 582 | any, was successfully processed by the receiving end of the connection. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 583 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 584 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 585 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 586 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 587 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 588 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 589 | .. coroutinemethod:: AbstractEventLoop.sock_connect(sock, address) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 590 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 591 | Connect to a remote socket at *address*. Modeled after |
| 592 | blocking :meth:`socket.socket.connect` method. |
Victor Stinner | 1b0580b | 2014-02-13 09:24:37 +0100 | [diff] [blame] | 593 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 594 | With :class:`SelectorEventLoop` event loop, the socket *sock* must be |
| 595 | non-blocking. |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 596 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 597 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 598 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 599 | .. versionchanged:: 3.5.2 |
| 600 | ``address`` no longer needs to be resolved. ``sock_connect`` |
| 601 | will try to check if the *address* is already resolved by calling |
| 602 | :func:`socket.inet_pton`. If not, |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 603 | :meth:`AbstractEventLoop.getaddrinfo` will be used to resolve the |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 604 | *address*. |
| 605 | |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 606 | .. seealso:: |
| 607 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 608 | :meth:`AbstractEventLoop.create_connection` |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 609 | and :func:`asyncio.open_connection() <open_connection>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 610 | |
| 611 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 612 | .. coroutinemethod:: AbstractEventLoop.sock_accept(sock) |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 613 | |
Yury Selivanov | 55c5084 | 2016-06-08 12:48:15 -0400 | [diff] [blame] | 614 | Accept a connection. Modeled after blocking |
| 615 | :meth:`socket.socket.accept`. |
| 616 | |
| 617 | The socket must be bound to an address and listening |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 618 | for connections. The return value is a pair ``(conn, address)`` where *conn* |
| 619 | is a *new* socket object usable to send and receive data on the connection, |
| 620 | and *address* is the address bound to the socket on the other end of the |
| 621 | connection. |
| 622 | |
Victor Stinner | ec2ce09 | 2014-07-29 23:12:22 +0200 | [diff] [blame] | 623 | The socket *sock* must be non-blocking. |
| 624 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 625 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 626 | |
| 627 | .. seealso:: |
| 628 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 629 | :meth:`AbstractEventLoop.create_server` and :func:`start_server`. |
Victor Stinner | c1567df | 2014-02-08 23:22:58 +0100 | [diff] [blame] | 630 | |
| 631 | |
| 632 | Resolve host name |
| 633 | ----------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 634 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 635 | .. coroutinemethod:: AbstractEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 636 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 637 | This method is a :ref:`coroutine <coroutine>`, similar to |
| 638 | :meth:`socket.getaddrinfo` function but non-blocking. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 639 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 640 | .. coroutinemethod:: AbstractEventLoop.getnameinfo(sockaddr, flags=0) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 641 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 642 | This method is a :ref:`coroutine <coroutine>`, similar to |
| 643 | :meth:`socket.getnameinfo` function but non-blocking. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 644 | |
| 645 | |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 646 | Connect pipes |
| 647 | ------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 648 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 649 | On Windows with :class:`SelectorEventLoop`, these methods are not supported. |
| 650 | Use :class:`ProactorEventLoop` to support pipes on Windows. |
| 651 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 652 | .. coroutinemethod:: AbstractEventLoop.connect_read_pipe(protocol_factory, pipe) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 653 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 654 | Register read pipe in eventloop. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 655 | |
| 656 | *protocol_factory* should instantiate object with :class:`Protocol` |
Victor Stinner | a5b257a | 2014-05-29 00:14:03 +0200 | [diff] [blame] | 657 | interface. *pipe* is a :term:`file-like object <file object>`. |
| 658 | Return pair ``(transport, protocol)``, where *transport* supports the |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 659 | :class:`ReadTransport` interface. |
| 660 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 661 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 662 | non-blocking mode. |
| 663 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 664 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 665 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 666 | .. coroutinemethod:: AbstractEventLoop.connect_write_pipe(protocol_factory, pipe) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 667 | |
| 668 | Register write pipe in eventloop. |
| 669 | |
| 670 | *protocol_factory* should instantiate object with :class:`BaseProtocol` |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 671 | interface. *pipe* is :term:`file-like object <file object>`. |
| 672 | Return pair ``(transport, protocol)``, where *transport* supports |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 673 | :class:`WriteTransport` interface. |
| 674 | |
Victor Stinner | d84fd73 | 2014-08-26 01:01:59 +0200 | [diff] [blame] | 675 | With :class:`SelectorEventLoop` event loop, the *pipe* is set to |
| 676 | non-blocking mode. |
| 677 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 678 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 679 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 680 | .. seealso:: |
| 681 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 682 | The :meth:`AbstractEventLoop.subprocess_exec` and |
| 683 | :meth:`AbstractEventLoop.subprocess_shell` methods. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 684 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 685 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 686 | UNIX signals |
| 687 | ------------ |
| 688 | |
| 689 | Availability: UNIX only. |
| 690 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 691 | .. method:: AbstractEventLoop.add_signal_handler(signum, callback, \*args) |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 692 | |
| 693 | Add a handler for a signal. |
| 694 | |
| 695 | Raise :exc:`ValueError` if the signal number is invalid or uncatchable. |
| 696 | Raise :exc:`RuntimeError` if there is a problem setting up the handler. |
| 697 | |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 698 | :ref:`Use functools.partial to pass keywords to the callback |
| 699 | <asyncio-pass-keywords>`. |
| 700 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 701 | .. method:: AbstractEventLoop.remove_signal_handler(sig) |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 702 | |
| 703 | Remove a handler for a signal. |
| 704 | |
| 705 | Return ``True`` if a signal handler was removed, ``False`` if not. |
| 706 | |
| 707 | .. seealso:: |
| 708 | |
| 709 | The :mod:`signal` module. |
| 710 | |
| 711 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 712 | Executor |
| 713 | -------- |
| 714 | |
| 715 | Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or |
| 716 | pool of processes). By default, an event loop uses a thread pool executor |
| 717 | (:class:`~concurrent.futures.ThreadPoolExecutor`). |
| 718 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 719 | .. coroutinemethod:: AbstractEventLoop.run_in_executor(executor, func, \*args) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 720 | |
Andrew Svetlov | 1c62b52 | 2015-10-01 09:48:08 +0300 | [diff] [blame] | 721 | Arrange for a *func* to be called in the specified executor. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 722 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 723 | The *executor* argument should be an :class:`~concurrent.futures.Executor` |
| 724 | instance. The default executor is used if *executor* is ``None``. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 725 | |
Andrew Svetlov | 1c62b52 | 2015-10-01 09:48:08 +0300 | [diff] [blame] | 726 | :ref:`Use functools.partial to pass keywords to the *func* |
Victor Stinner | 8464c24 | 2014-11-28 13:15:41 +0100 | [diff] [blame] | 727 | <asyncio-pass-keywords>`. |
| 728 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 729 | This method is a :ref:`coroutine <coroutine>`. |
| 730 | |
Yury Selivanov | e8a6045 | 2016-10-21 17:40:42 -0400 | [diff] [blame] | 731 | .. versionchanged:: 3.5.3 |
| 732 | :meth:`BaseEventLoop.run_in_executor` no longer configures the |
| 733 | ``max_workers`` of the thread pool executor it creates, instead |
| 734 | leaving it up to the thread pool executor |
| 735 | (:class:`~concurrent.futures.ThreadPoolExecutor`) to set the |
| 736 | default. |
| 737 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 738 | .. method:: AbstractEventLoop.set_default_executor(executor) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 739 | |
| 740 | Set the default executor used by :meth:`run_in_executor`. |
| 741 | |
| 742 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 743 | Error Handling API |
| 744 | ------------------ |
| 745 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 746 | Allows customizing how exceptions are handled in the event loop. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 747 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 748 | .. method:: AbstractEventLoop.set_exception_handler(handler) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 749 | |
| 750 | Set *handler* as the new event loop exception handler. |
| 751 | |
| 752 | If *handler* is ``None``, the default exception handler will |
| 753 | be set. |
| 754 | |
| 755 | If *handler* is a callable object, it should have a |
| 756 | matching signature to ``(loop, context)``, where ``loop`` |
| 757 | will be a reference to the active event loop, ``context`` |
| 758 | will be a ``dict`` object (see :meth:`call_exception_handler` |
| 759 | documentation for details about context). |
| 760 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 761 | .. method:: AbstractEventLoop.get_exception_handler() |
Yury Selivanov | 950204d | 2016-05-16 16:23:00 -0400 | [diff] [blame] | 762 | |
| 763 | Return the exception handler, or ``None`` if the default one |
| 764 | is in use. |
| 765 | |
| 766 | .. versionadded:: 3.5.2 |
| 767 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 768 | .. method:: AbstractEventLoop.default_exception_handler(context) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 769 | |
| 770 | Default exception handler. |
| 771 | |
| 772 | This is called when an exception occurs and no exception |
| 773 | handler is set, and can be called by a custom exception |
| 774 | handler that wants to defer to the default behavior. |
| 775 | |
| 776 | *context* parameter has the same meaning as in |
| 777 | :meth:`call_exception_handler`. |
| 778 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 779 | .. method:: AbstractEventLoop.call_exception_handler(context) |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 780 | |
| 781 | Call the current event loop exception handler. |
| 782 | |
| 783 | *context* is a ``dict`` object containing the following keys |
| 784 | (new keys may be introduced later): |
| 785 | |
| 786 | * 'message': Error message; |
| 787 | * 'exception' (optional): Exception object; |
| 788 | * 'future' (optional): :class:`asyncio.Future` instance; |
| 789 | * 'handle' (optional): :class:`asyncio.Handle` instance; |
| 790 | * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance; |
| 791 | * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance; |
| 792 | * 'socket' (optional): :class:`socket.socket` instance. |
| 793 | |
| 794 | .. note:: |
| 795 | |
| 796 | Note: this method should not be overloaded in subclassed |
| 797 | event loops. For any custom exception handling, use |
| 798 | :meth:`set_exception_handler()` method. |
| 799 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 800 | Debug mode |
| 801 | ---------- |
| 802 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 803 | .. method:: AbstractEventLoop.get_debug() |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 804 | |
Victor Stinner | 7b7120e | 2014-06-23 00:12:14 +0200 | [diff] [blame] | 805 | Get the debug mode (:class:`bool`) of the event loop. |
| 806 | |
| 807 | The default value is ``True`` if the environment variable |
| 808 | :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False`` |
| 809 | otherwise. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 810 | |
Victor Stinner | 64d750b | 2014-06-18 03:25:23 +0200 | [diff] [blame] | 811 | .. versionadded:: 3.4.2 |
| 812 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 813 | .. method:: AbstractEventLoop.set_debug(enabled: bool) |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 814 | |
| 815 | Set the debug mode of the event loop. |
| 816 | |
Victor Stinner | 64d750b | 2014-06-18 03:25:23 +0200 | [diff] [blame] | 817 | .. versionadded:: 3.4.2 |
| 818 | |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 819 | .. seealso:: |
| 820 | |
Victor Stinner | 62511fd | 2014-06-23 00:36:11 +0200 | [diff] [blame] | 821 | The :ref:`debug mode of asyncio <asyncio-debug-mode>`. |
Victor Stinner | 0f3e6bc | 2014-02-19 23:15:02 +0100 | [diff] [blame] | 822 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 823 | Server |
| 824 | ------ |
| 825 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 826 | .. class:: Server |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 827 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 828 | Server listening on sockets. |
| 829 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 830 | Object created by the :meth:`AbstractEventLoop.create_server` method and the |
R David Murray | 756f0b1 | 2015-01-29 19:53:33 -0500 | [diff] [blame] | 831 | :func:`start_server` function. Don't instantiate the class directly. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 832 | |
| 833 | .. method:: close() |
| 834 | |
Victor Stinner | 4bfb14a | 2014-07-12 03:20:24 +0200 | [diff] [blame] | 835 | Stop serving: close listening sockets and set the :attr:`sockets` |
| 836 | attribute to ``None``. |
| 837 | |
Berker Peksag | 49c9edf | 2016-01-20 07:14:22 +0200 | [diff] [blame] | 838 | The sockets that represent existing incoming client connections are left |
| 839 | open. |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 840 | |
Berker Peksag | 49c9edf | 2016-01-20 07:14:22 +0200 | [diff] [blame] | 841 | The server is closed asynchronously, use the :meth:`wait_closed` |
| 842 | coroutine to wait until the server is closed. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 843 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 844 | .. coroutinemethod:: wait_closed() |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 845 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 846 | Wait until the :meth:`close` method completes. |
| 847 | |
| 848 | This method is a :ref:`coroutine <coroutine>`. |
| 849 | |
| 850 | .. attribute:: sockets |
| 851 | |
| 852 | List of :class:`socket.socket` objects the server is listening to, or |
| 853 | ``None`` if the server is closed. |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 854 | |
| 855 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 856 | Handle |
| 857 | ------ |
| 858 | |
| 859 | .. class:: Handle |
| 860 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 861 | A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`, |
| 862 | :func:`AbstractEventLoop.call_soon_threadsafe`, :func:`AbstractEventLoop.call_later`, |
| 863 | and :func:`AbstractEventLoop.call_at`. |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 864 | |
| 865 | .. method:: cancel() |
| 866 | |
Yury Selivanov | 1096f76 | 2015-06-25 13:49:52 -0400 | [diff] [blame] | 867 | Cancel the call. If the callback is already canceled or executed, |
| 868 | this method has no effect. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 869 | |
Marat Sharafutdinov | 69cfed1 | 2017-11-07 12:06:05 +0300 | [diff] [blame] | 870 | .. method:: cancelled() |
| 871 | |
| 872 | Return ``True`` if the call was cancelled. |
| 873 | |
| 874 | .. versionadded:: 3.7 |
| 875 | |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 876 | |
Victor Stinner | 6888b96 | 2014-10-11 16:15:58 +0200 | [diff] [blame] | 877 | Event loop examples |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 878 | ------------------- |
Yury Selivanov | 43ee1c1 | 2014-02-19 20:58:44 -0500 | [diff] [blame] | 879 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 880 | .. _asyncio-hello-world-callback: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 881 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 882 | Hello World with call_soon() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 883 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 884 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 885 | Example using the :meth:`AbstractEventLoop.call_soon` method to schedule a |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 886 | callback. The callback displays ``"Hello World"`` and then stops the event |
| 887 | loop:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 888 | |
| 889 | import asyncio |
| 890 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 891 | def hello_world(loop): |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 892 | print('Hello World') |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 893 | loop.stop() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 894 | |
| 895 | loop = asyncio.get_event_loop() |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 896 | |
| 897 | # Schedule a call to hello_world() |
| 898 | loop.call_soon(hello_world, loop) |
| 899 | |
| 900 | # Blocking call interrupted by loop.stop() |
| 901 | loop.run_forever() |
| 902 | loop.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 903 | |
Victor Stinner | 3e09e32 | 2013-12-03 01:22:06 +0100 | [diff] [blame] | 904 | .. seealso:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 905 | |
Victor Stinner | 6888b96 | 2014-10-11 16:15:58 +0200 | [diff] [blame] | 906 | The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example |
| 907 | uses a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 908 | |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 909 | |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 910 | .. _asyncio-date-callback: |
| 911 | |
| 912 | Display the current date with call_later() |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 913 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 914 | |
| 915 | Example of callback displaying the current date every second. The callback uses |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 916 | the :meth:`AbstractEventLoop.call_later` method to reschedule itself during 5 |
Victor Stinner | 7f314ed | 2014-10-15 18:49:16 +0200 | [diff] [blame] | 917 | seconds, and then stops the event loop:: |
| 918 | |
| 919 | import asyncio |
| 920 | import datetime |
| 921 | |
| 922 | def display_date(end_time, loop): |
| 923 | print(datetime.datetime.now()) |
| 924 | if (loop.time() + 1.0) < end_time: |
| 925 | loop.call_later(1, display_date, end_time, loop) |
| 926 | else: |
| 927 | loop.stop() |
| 928 | |
| 929 | loop = asyncio.get_event_loop() |
| 930 | |
| 931 | # Schedule the first call to display_date() |
| 932 | end_time = loop.time() + 5.0 |
| 933 | loop.call_soon(display_date, end_time, loop) |
| 934 | |
| 935 | # Blocking call interrupted by loop.stop() |
| 936 | loop.run_forever() |
| 937 | loop.close() |
| 938 | |
| 939 | .. seealso:: |
| 940 | |
| 941 | The :ref:`coroutine displaying the current date |
| 942 | <asyncio-date-coroutine>` example uses a :ref:`coroutine |
| 943 | <coroutine>`. |
| 944 | |
| 945 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 946 | .. _asyncio-watch-read-event: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 947 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 948 | Watch a file descriptor for read events |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 949 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 950 | |
| 951 | Wait until a file descriptor received some data using the |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 952 | :meth:`AbstractEventLoop.add_reader` method and then close the event loop:: |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 953 | |
| 954 | import asyncio |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 955 | try: |
| 956 | from socket import socketpair |
| 957 | except ImportError: |
| 958 | from asyncio.windows_utils import socketpair |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 959 | |
| 960 | # Create a pair of connected file descriptors |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 961 | rsock, wsock = socketpair() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 962 | loop = asyncio.get_event_loop() |
| 963 | |
| 964 | def reader(): |
| 965 | data = rsock.recv(100) |
| 966 | print("Received:", data.decode()) |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 967 | # We are done: unregister the file descriptor |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 968 | loop.remove_reader(rsock) |
| 969 | # Stop the event loop |
| 970 | loop.stop() |
| 971 | |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 972 | # Register the file descriptor for read event |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 973 | loop.add_reader(rsock, reader) |
| 974 | |
| 975 | # Simulate the reception of data from the network |
| 976 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 977 | |
| 978 | # Run the event loop |
| 979 | loop.run_forever() |
| 980 | |
| 981 | # We are done, close sockets and the event loop |
| 982 | rsock.close() |
| 983 | wsock.close() |
| 984 | loop.close() |
| 985 | |
| 986 | .. seealso:: |
| 987 | |
| 988 | The :ref:`register an open socket to wait for data using a protocol |
| 989 | <asyncio-register-socket>` example uses a low-level protocol created by the |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 990 | :meth:`AbstractEventLoop.create_connection` method. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 991 | |
| 992 | The :ref:`register an open socket to wait for data using streams |
| 993 | <asyncio-register-socket-streams>` example uses high-level streams |
| 994 | created by the :func:`open_connection` function in a coroutine. |
| 995 | |
| 996 | |
| 997 | Set signal handlers for SIGINT and SIGTERM |
Victor Stinner | a092a61 | 2015-01-09 15:58:41 +0100 | [diff] [blame] | 998 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 999 | |
| 1000 | Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 1001 | the :meth:`AbstractEventLoop.add_signal_handler` method:: |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1002 | |
| 1003 | import asyncio |
| 1004 | import functools |
| 1005 | import os |
| 1006 | import signal |
| 1007 | |
| 1008 | def ask_exit(signame): |
| 1009 | print("got signal %s: exit" % signame) |
| 1010 | loop.stop() |
| 1011 | |
| 1012 | loop = asyncio.get_event_loop() |
| 1013 | for signame in ('SIGINT', 'SIGTERM'): |
| 1014 | loop.add_signal_handler(getattr(signal, signame), |
| 1015 | functools.partial(ask_exit, signame)) |
| 1016 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 1017 | print("Event loop running forever, press Ctrl+C to interrupt.") |
Victor Stinner | 8b86348 | 2014-01-27 10:07:50 +0100 | [diff] [blame] | 1018 | print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid()) |
Victor Stinner | 63b21a8 | 2014-07-05 15:38:59 +0200 | [diff] [blame] | 1019 | try: |
| 1020 | loop.run_forever() |
| 1021 | finally: |
| 1022 | loop.close() |
Victor Stinner | 2cef300 | 2014-10-23 22:38:46 +0200 | [diff] [blame] | 1023 | |
| 1024 | This example only works on UNIX. |