Victor Stinner | 24f8ebf | 2014-01-23 11:05:01 +0100 | [diff] [blame^] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | +++++++ |
| 4 | Streams |
| 5 | +++++++ |
| 6 | |
| 7 | Stream functions |
| 8 | ================ |
| 9 | |
| 10 | .. function:: open_connection(host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds) |
| 11 | |
| 12 | A wrapper for :meth:`~BaseEventLoop.create_connection()` returning a (reader, |
| 13 | writer) pair. |
| 14 | |
| 15 | The reader returned is a :class:`StreamReader` instance; the writer is |
| 16 | a :class:`StreamWriter` instance. |
| 17 | |
| 18 | The arguments are all the usual arguments to |
| 19 | :meth:`BaseEventLoop.create_connection` except *protocol_factory*; most |
| 20 | common are positional host and port, with various optional keyword arguments |
| 21 | following. |
| 22 | |
| 23 | Additional optional keyword arguments are *loop* (to set the event loop |
| 24 | instance to use) and *limit* (to set the buffer limit passed to the |
| 25 | :class:`StreamReader`). |
| 26 | |
| 27 | (If you want to customize the :class:`StreamReader` and/or |
| 28 | :class:`StreamReaderProtocol` classes, just copy the code -- there's really |
| 29 | nothing special here except some convenience.) |
| 30 | |
| 31 | This function returns a :ref:`coroutine object <coroutine>`. |
| 32 | |
| 33 | .. function:: start_server(client_connected_cb, host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds) |
| 34 | |
| 35 | Start a socket server, call back for each client connected. |
| 36 | |
| 37 | The first parameter, *client_connected_cb*, takes two parameters: |
| 38 | *client_reader*, *client_writer*. *client_reader* is a |
| 39 | :class:`StreamReader` object, while *client_writer* is a |
| 40 | :class:`StreamWriter` object. This parameter can either be a plain callback |
| 41 | function or a :ref:`coroutine function <coroutine>`; if it is a coroutine |
| 42 | function, it will be automatically converted into a :class:`Task`. |
| 43 | |
| 44 | The rest of the arguments are all the usual arguments to |
| 45 | :meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most |
| 46 | common are positional host and port, with various optional keyword arguments |
| 47 | following. The return value is the same as |
| 48 | :meth:`~BaseEventLoop.create_server()`. |
| 49 | |
| 50 | Additional optional keyword arguments are *loop* (to set the event loop |
| 51 | instance to use) and *limit* (to set the buffer limit passed to the |
| 52 | :class:`StreamReader`). |
| 53 | |
| 54 | The return value is the same as :meth:`~BaseEventLoop.create_server()`, i.e. |
| 55 | a :class:`AbstractServer` object which can be used to stop the service. |
| 56 | |
| 57 | This function returns a :ref:`coroutine object <coroutine>`. |
| 58 | |
| 59 | |
| 60 | StreamReader |
| 61 | ============ |
| 62 | |
| 63 | .. class:: StreamReader(limit=_DEFAULT_LIMIT, loop=None) |
| 64 | |
| 65 | .. method:: exception() |
| 66 | |
| 67 | Get the exception. |
| 68 | |
| 69 | .. method:: feed_eof() |
| 70 | |
| 71 | XXX |
| 72 | |
| 73 | .. method:: feed_data(data) |
| 74 | |
| 75 | XXX |
| 76 | |
| 77 | .. method:: set_exception(exc) |
| 78 | |
| 79 | Set the exception. |
| 80 | |
| 81 | .. method:: set_transport(transport) |
| 82 | |
| 83 | Set the transport. |
| 84 | |
| 85 | .. method:: read(n=-1) |
| 86 | |
| 87 | XXX |
| 88 | |
| 89 | This method returns a :ref:`coroutine object <coroutine>`. |
| 90 | |
| 91 | .. method:: readline() |
| 92 | |
| 93 | XXX |
| 94 | |
| 95 | This method returns a :ref:`coroutine object <coroutine>`. |
| 96 | |
| 97 | .. method:: readexactly(n) |
| 98 | |
| 99 | XXX |
| 100 | |
| 101 | This method returns a :ref:`coroutine object <coroutine>`. |
| 102 | |
| 103 | |
| 104 | StreamWriter |
| 105 | ============ |
| 106 | |
| 107 | .. class:: StreamWriter(transport, protocol, reader, loop) |
| 108 | |
| 109 | Wraps a Transport. |
| 110 | |
| 111 | This exposes :meth:`write`, :meth:`writelines`, :meth:`can_write_eof()`, |
| 112 | :meth:`write_eof`, :meth:`get_extra_info` and :meth:`close`. It adds |
| 113 | :meth:`drain` which returns an optional :class:`Future` on which you can |
| 114 | wait for flow control. It also adds a transport attribute which references |
| 115 | the :class:`Transport` directly. |
| 116 | |
| 117 | .. attribute:: transport |
| 118 | |
| 119 | Transport. |
| 120 | |
| 121 | .. method:: close() |
| 122 | |
| 123 | Close the transport: see :meth:`BaseTransport.close`. |
| 124 | |
| 125 | .. method:: drain() |
| 126 | |
| 127 | This method has an unusual return value. |
| 128 | |
| 129 | The intended use is to write:: |
| 130 | |
| 131 | w.write(data) |
| 132 | yield from w.drain() |
| 133 | |
| 134 | When there's nothing to wait for, :meth:`drain()` returns ``()``, and the |
| 135 | yield-from continues immediately. When the transport buffer is full (the |
| 136 | protocol is paused), :meth:`drain` creates and returns a |
| 137 | :class:`Future` and the yield-from will block until |
| 138 | that Future is completed, which will happen when the buffer is |
| 139 | (partially) drained and the protocol is resumed. |
| 140 | |
| 141 | .. method:: get_extra_info(name, default=None) |
| 142 | |
| 143 | Return optional transport information: see |
| 144 | :meth:`BaseTransport.get_extra_info`. |
| 145 | |
| 146 | .. method:: write(data) |
| 147 | |
| 148 | Write some *data* bytes to the transport: see |
| 149 | :meth:`WriteTransport.write`. |
| 150 | |
| 151 | .. method:: writelines(data) |
| 152 | |
| 153 | Write a list (or any iterable) of data bytes to the transport: |
| 154 | see :meth:`WriteTransport.writelines`. |
| 155 | |
| 156 | .. method:: can_write_eof() |
| 157 | |
| 158 | Return :const:`True` if the transport supports :meth:`write_eof`, |
| 159 | :const:`False` if not. See :meth:`WriteTransport.can_write_eof`. |
| 160 | |
| 161 | .. method:: write_eof() |
| 162 | |
| 163 | Close the write end of the transport after flushing buffered data: |
| 164 | see :meth:`WriteTransport.write_eof`. |
| 165 | |
| 166 | |
| 167 | StreamReaderProtocol |
| 168 | ==================== |
| 169 | |
| 170 | .. class:: StreamReaderProtocol(stream_reader, client_connected_cb=None, loop=None) |
| 171 | |
| 172 | Trivial helper class to adapt between :class:`Protocol` and |
| 173 | :class:`StreamReader`. Sublclass of :class:`Protocol`. |
| 174 | |
| 175 | *stream_reader* is a :class:`StreamReader` instance, *client_connected_cb* |
| 176 | is an optional function called with (stream_reader, stream_writer) when a |
| 177 | connection is made, *loop* is the event loop instance to use. |
| 178 | |
| 179 | (This is a helper class instead of making :class:`StreamReader` itself a |
| 180 | :class:`Protocol` subclass, because the :class:`StreamReader` has other |
| 181 | potential uses, and to prevent the user of the :class:`StreamReader` to |
| 182 | accidentally call inappropriate methods of the protocol.) |
| 183 | |
| 184 | .. method:: connection_made(transport) |
| 185 | |
| 186 | XXX |
| 187 | |
| 188 | .. method:: connection_lost(exc) |
| 189 | |
| 190 | XXX |
| 191 | |
| 192 | .. method:: data_received(data) |
| 193 | |
| 194 | XXX |
| 195 | |
| 196 | .. method:: eof_received() |
| 197 | |
| 198 | XXX |
| 199 | |
| 200 | .. method:: pause_writing() |
| 201 | |
| 202 | XXX |
| 203 | |
| 204 | .. method:: resume_writing() |
| 205 | |
| 206 | XXX |
| 207 | |