Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 3 | .. _asyncio-streams: |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 4 | |
Victor Stinner | 1374bd4 | 2014-01-24 15:34:19 +0100 | [diff] [blame] | 5 | ++++++++++++++++++++++++ |
| 6 | Streams (high-level API) |
| 7 | ++++++++++++++++++++++++ |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 8 | |
| 9 | Stream functions |
| 10 | ================ |
| 11 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 12 | .. function:: open_connection(host=None, port=None, \*, loop=None, limit=None, **kwds) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 13 | |
| 14 | A wrapper for :meth:`~BaseEventLoop.create_connection()` returning a (reader, |
| 15 | writer) pair. |
| 16 | |
| 17 | The reader returned is a :class:`StreamReader` instance; the writer is |
| 18 | a :class:`StreamWriter` instance. |
| 19 | |
| 20 | The arguments are all the usual arguments to |
| 21 | :meth:`BaseEventLoop.create_connection` except *protocol_factory*; most |
| 22 | common are positional host and port, with various optional keyword arguments |
| 23 | following. |
| 24 | |
| 25 | Additional optional keyword arguments are *loop* (to set the event loop |
| 26 | instance to use) and *limit* (to set the buffer limit passed to the |
| 27 | :class:`StreamReader`). |
| 28 | |
| 29 | (If you want to customize the :class:`StreamReader` and/or |
| 30 | :class:`StreamReaderProtocol` classes, just copy the code -- there's really |
| 31 | nothing special here except some convenience.) |
| 32 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 33 | This function is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 34 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 35 | .. function:: start_server(client_connected_cb, host=None, port=None, \*, loop=None, limit=None, **kwds) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 36 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 37 | Start a socket server, with a callback for each client connected. The return |
| 38 | value is the same as :meth:`~BaseEventLoop.create_server()`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 39 | |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 40 | The *client_connected_cb* parameter is called with two parameters: |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 41 | *client_reader*, *client_writer*. *client_reader* is a |
| 42 | :class:`StreamReader` object, while *client_writer* is a |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 43 | :class:`StreamWriter` object. The *client_connected_cb* parameter can |
| 44 | either be a plain callback function or a :ref:`coroutine function |
| 45 | <coroutine>`; if it is a coroutine function, it will be automatically |
Victor Stinner | 337e03f | 2014-08-11 01:11:13 +0200 | [diff] [blame] | 46 | converted into a :class:`Task`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 47 | |
| 48 | The rest of the arguments are all the usual arguments to |
| 49 | :meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most |
Victor Stinner | 8ebeb03 | 2014-07-11 23:47:40 +0200 | [diff] [blame] | 50 | common are positional *host* and *port*, with various optional keyword |
| 51 | arguments following. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 52 | |
| 53 | Additional optional keyword arguments are *loop* (to set the event loop |
| 54 | instance to use) and *limit* (to set the buffer limit passed to the |
| 55 | :class:`StreamReader`). |
| 56 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 57 | This function is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 58 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 59 | .. function:: open_unix_connection(path=None, \*, loop=None, limit=None, **kwds) |
| 60 | |
| 61 | A wrapper for :meth:`~BaseEventLoop.create_unix_connection()` returning |
| 62 | a (reader, writer) pair. |
| 63 | |
| 64 | See :func:`open_connection` for information about return value and other |
| 65 | details. |
| 66 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 67 | This function is a :ref:`coroutine <coroutine>`. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 68 | |
| 69 | Availability: UNIX. |
| 70 | |
| 71 | .. function:: start_unix_server(client_connected_cb, path=None, \*, loop=None, limit=None, **kwds) |
| 72 | |
| 73 | Start a UNIX Domain Socket server, with a callback for each client connected. |
| 74 | |
| 75 | See :func:`start_server` for information about return value and other |
| 76 | details. |
| 77 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 78 | This function is a :ref:`coroutine <coroutine>`. |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 79 | |
| 80 | Availability: UNIX. |
| 81 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 82 | |
| 83 | StreamReader |
| 84 | ============ |
| 85 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 86 | .. class:: StreamReader(limit=None, loop=None) |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 87 | |
| 88 | .. method:: exception() |
| 89 | |
| 90 | Get the exception. |
| 91 | |
| 92 | .. method:: feed_eof() |
| 93 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 94 | Acknowledge the EOF. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 95 | |
| 96 | .. method:: feed_data(data) |
| 97 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 98 | Feed *data* bytes in the internal buffer. Any operations waiting |
| 99 | for the data will be resumed. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 100 | |
| 101 | .. method:: set_exception(exc) |
| 102 | |
| 103 | Set the exception. |
| 104 | |
| 105 | .. method:: set_transport(transport) |
| 106 | |
| 107 | Set the transport. |
| 108 | |
| 109 | .. method:: read(n=-1) |
| 110 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 111 | Read up to *n* bytes. If *n* is not provided, or set to ``-1``, |
| 112 | read until EOF and return all read bytes. |
| 113 | |
| 114 | If the EOF was received and the internal buffer is empty, |
| 115 | return an empty ``bytes`` object. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 116 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 117 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 118 | |
| 119 | .. method:: readline() |
| 120 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 121 | Read one line, where "line" is a sequence of bytes ending with ``\n``. |
| 122 | |
| 123 | If EOF is received, and ``\n`` was not found, the method will |
| 124 | return the partial read bytes. |
| 125 | |
| 126 | If the EOF was received and the internal buffer is empty, |
| 127 | return an empty ``bytes`` object. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 128 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 129 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 130 | |
| 131 | .. method:: readexactly(n) |
| 132 | |
Victor Stinner | b7f19ff | 2014-01-27 11:58:49 +0100 | [diff] [blame] | 133 | Read exactly *n* bytes. Raise an :exc:`IncompleteReadError` if the end of |
| 134 | the stream is reached before *n* can be read, the |
| 135 | :attr:`IncompleteReadError.partial` attribute of the exception contains |
| 136 | the partial read bytes. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 137 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 138 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 139 | |
Yury Selivanov | d3f8e30 | 2014-02-20 14:10:02 -0500 | [diff] [blame] | 140 | .. method:: at_eof() |
| 141 | |
| 142 | Return ``True`` if the buffer is empty and :meth:`feed_eof` was called. |
| 143 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 144 | |
| 145 | StreamWriter |
| 146 | ============ |
| 147 | |
| 148 | .. class:: StreamWriter(transport, protocol, reader, loop) |
| 149 | |
| 150 | Wraps a Transport. |
| 151 | |
| 152 | This exposes :meth:`write`, :meth:`writelines`, :meth:`can_write_eof()`, |
| 153 | :meth:`write_eof`, :meth:`get_extra_info` and :meth:`close`. It adds |
| 154 | :meth:`drain` which returns an optional :class:`Future` on which you can |
| 155 | wait for flow control. It also adds a transport attribute which references |
| 156 | the :class:`Transport` directly. |
| 157 | |
| 158 | .. attribute:: transport |
| 159 | |
| 160 | Transport. |
| 161 | |
Victor Stinner | ffbe3c6 | 2014-02-08 22:50:07 +0100 | [diff] [blame] | 162 | .. method:: can_write_eof() |
| 163 | |
| 164 | Return :const:`True` if the transport supports :meth:`write_eof`, |
| 165 | :const:`False` if not. See :meth:`WriteTransport.can_write_eof`. |
| 166 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 167 | .. method:: close() |
| 168 | |
| 169 | Close the transport: see :meth:`BaseTransport.close`. |
| 170 | |
| 171 | .. method:: drain() |
| 172 | |
Victor Stinner | 62f8ecc | 2014-01-24 18:47:26 +0100 | [diff] [blame] | 173 | Wait until the write buffer of the underlying transport is flushed. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 174 | |
Victor Stinner | d71dcbb | 2014-08-25 17:04:12 +0200 | [diff] [blame] | 175 | The intended use is to write:: |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 176 | |
| 177 | w.write(data) |
| 178 | yield from w.drain() |
| 179 | |
Victor Stinner | d71dcbb | 2014-08-25 17:04:12 +0200 | [diff] [blame] | 180 | When the transport buffer is full (the protocol is paused), block until |
| 181 | the buffer is (partially) drained and the protocol is resumed. When there |
| 182 | is nothing to wait for, the yield-from continues immediately. |
| 183 | |
| 184 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 185 | |
| 186 | .. method:: get_extra_info(name, default=None) |
| 187 | |
| 188 | Return optional transport information: see |
| 189 | :meth:`BaseTransport.get_extra_info`. |
| 190 | |
| 191 | .. method:: write(data) |
| 192 | |
| 193 | Write some *data* bytes to the transport: see |
| 194 | :meth:`WriteTransport.write`. |
| 195 | |
| 196 | .. method:: writelines(data) |
| 197 | |
| 198 | Write a list (or any iterable) of data bytes to the transport: |
| 199 | see :meth:`WriteTransport.writelines`. |
| 200 | |
Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame] | 201 | .. method:: write_eof() |
| 202 | |
| 203 | Close the write end of the transport after flushing buffered data: |
| 204 | see :meth:`WriteTransport.write_eof`. |
| 205 | |
| 206 | |
| 207 | StreamReaderProtocol |
| 208 | ==================== |
| 209 | |
| 210 | .. class:: StreamReaderProtocol(stream_reader, client_connected_cb=None, loop=None) |
| 211 | |
| 212 | Trivial helper class to adapt between :class:`Protocol` and |
| 213 | :class:`StreamReader`. Sublclass of :class:`Protocol`. |
| 214 | |
| 215 | *stream_reader* is a :class:`StreamReader` instance, *client_connected_cb* |
| 216 | is an optional function called with (stream_reader, stream_writer) when a |
| 217 | connection is made, *loop* is the event loop instance to use. |
| 218 | |
| 219 | (This is a helper class instead of making :class:`StreamReader` itself a |
| 220 | :class:`Protocol` subclass, because the :class:`StreamReader` has other |
| 221 | potential uses, and to prevent the user of the :class:`StreamReader` to |
| 222 | accidentally call inappropriate methods of the protocol.) |
| 223 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 224 | |
Victor Stinner | b7f19ff | 2014-01-27 11:58:49 +0100 | [diff] [blame] | 225 | IncompleteReadError |
| 226 | =================== |
| 227 | |
| 228 | .. exception:: IncompleteReadError |
| 229 | |
Victor Stinner | 32970b8 | 2014-01-27 12:18:49 +0100 | [diff] [blame] | 230 | Incomplete read error, subclass of :exc:`EOFError`. |
Victor Stinner | b7f19ff | 2014-01-27 11:58:49 +0100 | [diff] [blame] | 231 | |
| 232 | .. attribute:: expected |
| 233 | |
| 234 | Total number of expected bytes (:class:`int`). |
| 235 | |
| 236 | .. attribute:: partial |
| 237 | |
| 238 | Read bytes string before the end of stream was reached (:class:`bytes`). |
| 239 | |
| 240 | |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 241 | Stream examples |
| 242 | =============== |
| 243 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 244 | .. _asyncio-tcp-echo-client-streams: |
| 245 | |
| 246 | TCP echo client using streams |
| 247 | ----------------------------- |
| 248 | |
| 249 | TCP echo client using the :func:`asyncio.open_connection` function:: |
| 250 | |
| 251 | import asyncio |
| 252 | |
Victor Stinner | ed8e3a9 | 2014-10-13 00:55:50 +0200 | [diff] [blame^] | 253 | @asyncio.coroutine |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 254 | def tcp_echo_client(message, loop): |
| 255 | reader, writer = yield from asyncio.open_connection('127.0.0.1', 8888, |
| 256 | loop=loop) |
| 257 | |
| 258 | print('Send: %r' % message) |
| 259 | writer.write(message.encode()) |
| 260 | |
| 261 | data = yield from reader.read(100) |
| 262 | print('Received: %r' % data.decode()) |
| 263 | |
| 264 | print('Close the socket') |
| 265 | writer.close() |
| 266 | |
| 267 | message = 'Hello World!' |
| 268 | loop = asyncio.get_event_loop() |
| 269 | loop.run_until_complete(tcp_echo_client(message, loop)) |
| 270 | loop.close() |
| 271 | |
| 272 | .. seealso:: |
| 273 | |
| 274 | The :ref:`TCP echo client protocol <asyncio-tcp-echo-client-protocol>` |
| 275 | example uses the :meth:`BaseEventLoop.create_connection` method. |
| 276 | |
| 277 | |
| 278 | .. _asyncio-tcp-echo-server-streams: |
| 279 | |
| 280 | TCP echo server using streams |
| 281 | ----------------------------- |
| 282 | |
| 283 | TCP echo server using the :func:`asyncio.start_server` function:: |
| 284 | |
| 285 | import asyncio |
| 286 | |
| 287 | @asyncio.coroutine |
| 288 | def handle_echo(reader, writer): |
| 289 | data = yield from reader.read(100) |
| 290 | message = data.decode() |
| 291 | addr = writer.get_extra_info('peername') |
| 292 | print("Received %r from %r" % (message, addr)) |
| 293 | |
| 294 | print("Send: %r" % message) |
| 295 | writer.write(data) |
| 296 | yield from writer.drain() |
| 297 | |
| 298 | print("Close the client socket") |
| 299 | writer.close() |
| 300 | |
| 301 | loop = asyncio.get_event_loop() |
| 302 | coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) |
| 303 | server = loop.run_until_complete(coro) |
| 304 | |
| 305 | # Serve requests until CTRL+c is pressed |
| 306 | print('Serving on {}'.format(server.sockets[0].getsockname())) |
| 307 | try: |
| 308 | loop.run_forever() |
| 309 | except KeyboardInterrupt: |
| 310 | pass |
| 311 | |
| 312 | # Close the server |
| 313 | server.close() |
| 314 | loop.run_until_complete(server.wait_closed()) |
| 315 | loop.close() |
| 316 | |
| 317 | .. seealso:: |
| 318 | |
| 319 | The :ref:`TCP echo server protocol <asyncio-tcp-echo-server-protocol>` |
| 320 | example uses the :meth:`BaseEventLoop.create_server` method. |
| 321 | |
| 322 | |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 323 | Get HTTP headers |
| 324 | ---------------- |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 325 | |
| 326 | Simple example querying HTTP headers of the URL passed on the command line:: |
| 327 | |
| 328 | import asyncio |
| 329 | import urllib.parse |
| 330 | import sys |
| 331 | |
| 332 | @asyncio.coroutine |
| 333 | def print_http_headers(url): |
| 334 | url = urllib.parse.urlsplit(url) |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 335 | if url.scheme == 'https': |
| 336 | connect = asyncio.open_connection(url.hostname, 443, ssl=True) |
| 337 | else: |
| 338 | connect = asyncio.open_connection(url.hostname, 80) |
| 339 | reader, writer = yield from connect |
| 340 | query = ('HEAD {path} HTTP/1.0\r\n' |
| 341 | 'Host: {hostname}\r\n' |
| 342 | '\r\n').format(path=url.path or '/', hostname=url.hostname) |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 343 | writer.write(query.encode('latin-1')) |
| 344 | while True: |
| 345 | line = yield from reader.readline() |
| 346 | if not line: |
| 347 | break |
| 348 | line = line.decode('latin1').rstrip() |
| 349 | if line: |
| 350 | print('HTTP header> %s' % line) |
| 351 | |
Victor Stinner | 5121a9b | 2014-10-11 15:52:14 +0200 | [diff] [blame] | 352 | # Ignore the body, close the socket |
| 353 | writer.close() |
| 354 | |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 355 | url = sys.argv[1] |
| 356 | loop = asyncio.get_event_loop() |
| 357 | task = asyncio.async(print_http_headers(url)) |
| 358 | loop.run_until_complete(task) |
Victor Stinner | f40c663 | 2014-01-28 23:32:40 +0100 | [diff] [blame] | 359 | loop.close() |
Victor Stinner | c520edc | 2014-01-23 11:25:48 +0100 | [diff] [blame] | 360 | |
| 361 | Usage:: |
| 362 | |
| 363 | python example.py http://example.com/path/page.html |
| 364 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 365 | or with HTTPS:: |
| 366 | |
| 367 | python example.py https://example.com/path/page.html |
| 368 | |
| 369 | .. _asyncio-register-socket-streams: |
| 370 | |
| 371 | Register an open socket to wait for data using streams |
| 372 | ------------------------------------------------------ |
| 373 | |
| 374 | Coroutine waiting until a socket receives data using the |
| 375 | :func:`open_connection` function:: |
| 376 | |
| 377 | import asyncio |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 378 | try: |
| 379 | from socket import socketpair |
| 380 | except ImportError: |
| 381 | from asyncio.windows_utils import socketpair |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 382 | |
Victor Stinner | ed8e3a9 | 2014-10-13 00:55:50 +0200 | [diff] [blame^] | 383 | @asyncio.coroutine |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 384 | def wait_for_data(loop): |
| 385 | # Create a pair of connected sockets |
Victor Stinner | ccd8e34 | 2014-10-11 16:30:02 +0200 | [diff] [blame] | 386 | rsock, wsock = socketpair() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 387 | |
| 388 | # Register the open socket to wait for data |
| 389 | reader, writer = yield from asyncio.open_connection(sock=rsock, loop=loop) |
| 390 | |
| 391 | # Simulate the reception of data from the network |
| 392 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 393 | |
| 394 | # Wait for data |
| 395 | data = yield from reader.read(100) |
| 396 | |
| 397 | # Got data, we are done: close the socket |
| 398 | print("Received:", data.decode()) |
| 399 | writer.close() |
| 400 | |
| 401 | # Close the second socket |
| 402 | wsock.close() |
| 403 | |
| 404 | loop = asyncio.get_event_loop() |
| 405 | loop.run_until_complete(wait_for_data(loop)) |
| 406 | loop.close() |
| 407 | |
| 408 | .. seealso:: |
| 409 | |
| 410 | The :ref:`register an open socket to wait for data using a protocol |
| 411 | <asyncio-register-socket>` example uses a low-level protocol created by the |
| 412 | :meth:`BaseEventLoop.create_connection` method. |
| 413 | |
| 414 | The :ref:`watch a file descriptor for read events |
| 415 | <asyncio-watch-read-event>` example uses the low-level |
| 416 | :meth:`BaseEventLoop.add_reader` method to register the file descriptor of a |
| 417 | socket. |
| 418 | |