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 | |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 3 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 4 | .. _asyncio-transports-protocols: |
| 5 | |
| 6 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 7 | ======================== |
| 8 | Transports and Protocols |
| 9 | ======================== |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 10 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 11 | .. rubric:: Preface |
| 12 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 13 | Transports and Protocols are used by the **low-level** event loop |
| 14 | APIs such as :meth:`loop.create_connection`. They use |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 15 | callback-based programming style and enable high-performance |
| 16 | implementations of network or IPC protocols (e.g. HTTP). |
| 17 | |
| 18 | Essentially, transports and protocols should only be used in |
| 19 | libraries and frameworks and never in high-level asyncio |
| 20 | applications. |
| 21 | |
| 22 | This documentation page covers both `Transports`_ and `Protocols`_. |
| 23 | |
| 24 | .. rubric:: Introduction |
| 25 | |
| 26 | At the highest level, the transport is concerned with *how* bytes |
| 27 | are transmitted, while the protocol determines *which* bytes to |
| 28 | transmit (and to some extent when). |
| 29 | |
| 30 | A different way of saying the same thing: a transport is an |
| 31 | abstraction for a socket (or similar I/O endpoint) while a protocol |
| 32 | is an abstraction for an application, from the transport's point |
| 33 | of view. |
| 34 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 35 | Yet another view is the transport and protocol interfaces |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 36 | together define an abstract interface for using network I/O and |
| 37 | interprocess I/O. |
| 38 | |
| 39 | There is always a 1:1 relationship between transport and protocol |
| 40 | objects: the protocol calls transport methods to send data, |
| 41 | while the transport calls protocol methods to pass it data that |
| 42 | has been received. |
| 43 | |
| 44 | Most of connection oriented event loop methods |
| 45 | (such as :meth:`loop.create_connection`) usually accept a |
| 46 | *protocol_factory* argument used to create a *Protocol* object |
| 47 | for an accepted connection, represented by a *Transport* object. |
| 48 | Such methods usually return a tuple of ``(transport, protocol)``. |
| 49 | |
| 50 | .. rubric:: Contents |
| 51 | |
| 52 | This documentation page contains the following sections: |
| 53 | |
| 54 | * The `Transports`_ section documents asyncio :class:`BaseTransport`, |
| 55 | :class:`ReadTransport`, :class:`WriteTransport`, :class:`Transport`, |
| 56 | :class:`DatagramTransport`, and :class:`SubprocessTransport` |
| 57 | classes. |
| 58 | |
| 59 | * The `Protocols`_ section documents asyncio :class:`BaseProtocol`, |
| 60 | :class:`Protocol`, :class:`BufferedProtocol`, |
| 61 | :class:`DatagramProtocol`, and :class:`SubprocessProtocol` classes. |
| 62 | |
| 63 | * The `Examples`_ section showcases how to work with transports, |
| 64 | protocols, and low-level event loop APIs. |
| 65 | |
Victor Stinner | 1ca5ba6 | 2013-12-03 01:49:43 +0100 | [diff] [blame] | 66 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 67 | .. _asyncio-transport: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 68 | |
| 69 | Transports |
| 70 | ========== |
| 71 | |
Kyle Stanley | f900064 | 2019-10-10 19:18:46 -0400 | [diff] [blame] | 72 | **Source code:** :source:`Lib/asyncio/transports.py` |
| 73 | |
| 74 | ---------------------------------------------------- |
| 75 | |
Guido van Rossum | 589872c | 2014-03-29 21:14:04 -0700 | [diff] [blame] | 76 | Transports are classes provided by :mod:`asyncio` in order to abstract |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 77 | various kinds of communication channels. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 78 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 79 | Transport objects are always instantiated by an |
Pablo Galindo | 5033e31 | 2019-02-10 00:21:37 +0000 | [diff] [blame] | 80 | :ref:`asyncio event loop <asyncio-event-loop>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 81 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 82 | asyncio implements transports for TCP, UDP, SSL, and subprocess pipes. |
| 83 | The methods available on a transport depend on the transport's kind. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 84 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 85 | The transport classes are :ref:`not thread safe <asyncio-multithreading>`. |
| 86 | |
Yury Selivanov | 3432f2f | 2016-12-12 16:44:58 -0500 | [diff] [blame] | 87 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 88 | Transports Hierarchy |
| 89 | -------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 90 | |
| 91 | .. class:: BaseTransport |
| 92 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 93 | Base class for all transports. Contains methods that all |
| 94 | asyncio transports share. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 95 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 96 | .. class:: WriteTransport(BaseTransport) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 97 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 98 | A base transport for write-only connections. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 99 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 100 | Instances of the *WriteTransport* class are returned from |
| 101 | the :meth:`loop.connect_write_pipe` event loop method and |
| 102 | are also used by subprocess-related methods like |
| 103 | :meth:`loop.subprocess_exec`. |
Yury Selivanov | 1744d53 | 2015-11-16 12:46:41 -0500 | [diff] [blame] | 104 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 105 | .. class:: ReadTransport(BaseTransport) |
Yury Selivanov | 1744d53 | 2015-11-16 12:46:41 -0500 | [diff] [blame] | 106 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 107 | A base transport for read-only connections. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 108 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 109 | Instances of the *ReadTransport* class are returned from |
| 110 | the :meth:`loop.connect_read_pipe` event loop method and |
| 111 | are also used by subprocess-related methods like |
| 112 | :meth:`loop.subprocess_exec`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 113 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 114 | .. class:: Transport(WriteTransport, ReadTransport) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 115 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 116 | Interface representing a bidirectional transport, such as a |
| 117 | TCP connection. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 118 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 119 | The user does not instantiate a transport directly; they call a |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 120 | utility function, passing it a protocol factory and other |
| 121 | information necessary to create the transport and protocol. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 122 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 123 | Instances of the *Transport* class are returned from or used by |
| 124 | event loop methods like :meth:`loop.create_connection`, |
| 125 | :meth:`loop.create_unix_connection`, |
| 126 | :meth:`loop.create_server`, :meth:`loop.sendfile`, etc. |
Victor Stinner | f7dc7fb | 2015-09-21 18:06:17 +0200 | [diff] [blame] | 127 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 128 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 129 | .. class:: DatagramTransport(BaseTransport) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 130 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 131 | A transport for datagram (UDP) connections. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 132 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 133 | Instances of the *DatagramTransport* class are returned from |
| 134 | the :meth:`loop.create_datagram_endpoint` event loop method. |
Yury Selivanov | d757aaf | 2017-12-18 17:03:23 -0500 | [diff] [blame] | 135 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 136 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 137 | .. class:: SubprocessTransport(BaseTransport) |
| 138 | |
| 139 | An abstraction to represent a connection between a parent and its |
| 140 | child OS process. |
| 141 | |
| 142 | Instances of the *SubprocessTransport* class are returned from |
| 143 | event loop methods :meth:`loop.subprocess_shell` and |
| 144 | :meth:`loop.subprocess_exec`. |
| 145 | |
| 146 | |
| 147 | Base Transport |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 148 | -------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 149 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 150 | .. method:: BaseTransport.close() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 151 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 152 | Close the transport. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 153 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 154 | If the transport has a buffer for outgoing |
| 155 | data, buffered data will be flushed asynchronously. No more data |
| 156 | will be received. After all buffered data is flushed, the |
| 157 | protocol's :meth:`protocol.connection_lost() |
| 158 | <BaseProtocol.connection_lost>` method will be called with |
| 159 | :const:`None` as its argument. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 160 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 161 | .. method:: BaseTransport.is_closing() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 162 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 163 | Return ``True`` if the transport is closing or is closed. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 164 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 165 | .. method:: BaseTransport.get_extra_info(name, default=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 166 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 167 | Return information about the transport or underlying resources |
| 168 | it uses. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 169 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 170 | *name* is a string representing the piece of transport-specific |
| 171 | information to get. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 172 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 173 | *default* is the value to return if the information is not |
| 174 | available, or if the transport does not support querying it |
| 175 | with the given third-party event loop implementation or on the |
| 176 | current platform. |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 177 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 178 | For example, the following code attempts to get the underlying |
| 179 | socket object of the transport:: |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 180 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 181 | sock = transport.get_extra_info('socket') |
| 182 | if sock is not None: |
| 183 | print(sock.getsockopt(...)) |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 184 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 185 | Categories of information that can be queried on some transports: |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 186 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 187 | * socket: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 188 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 189 | - ``'peername'``: the remote address to which the socket is |
| 190 | connected, result of :meth:`socket.socket.getpeername` |
| 191 | (``None`` on error) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 192 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 193 | - ``'socket'``: :class:`socket.socket` instance |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 194 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 195 | - ``'sockname'``: the socket's own address, |
| 196 | result of :meth:`socket.socket.getsockname` |
Kojo Idrissa | 5200a7c | 2017-06-20 14:32:00 -0500 | [diff] [blame] | 197 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 198 | * SSL socket: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 199 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 200 | - ``'compression'``: the compression algorithm being used as a |
| 201 | string, or ``None`` if the connection isn't compressed; result |
| 202 | of :meth:`ssl.SSLSocket.compression` |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 203 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 204 | - ``'cipher'``: a three-value tuple containing the name of the |
| 205 | cipher being used, the version of the SSL protocol that defines |
| 206 | its use, and the number of secret bits being used; result of |
| 207 | :meth:`ssl.SSLSocket.cipher` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 208 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 209 | - ``'peercert'``: peer certificate; result of |
| 210 | :meth:`ssl.SSLSocket.getpeercert` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 211 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 212 | - ``'sslcontext'``: :class:`ssl.SSLContext` instance |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 213 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 214 | - ``'ssl_object'``: :class:`ssl.SSLObject` or |
| 215 | :class:`ssl.SSLSocket` instance |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 216 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 217 | * pipe: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 218 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 219 | - ``'pipe'``: pipe object |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 220 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 221 | * subprocess: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 222 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 223 | - ``'subprocess'``: :class:`subprocess.Popen` instance |
| 224 | |
| 225 | .. method:: BaseTransport.set_protocol(protocol) |
| 226 | |
| 227 | Set a new protocol. |
| 228 | |
| 229 | Switching protocol should only be done when both |
| 230 | protocols are documented to support the switch. |
| 231 | |
| 232 | .. method:: BaseTransport.get_protocol() |
| 233 | |
| 234 | Return the current protocol. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 235 | |
| 236 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 237 | Read-only Transports |
| 238 | -------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 239 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 240 | .. method:: ReadTransport.is_reading() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 241 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 242 | Return ``True`` if the transport is receiving new data. |
| 243 | |
| 244 | .. versionadded:: 3.7 |
| 245 | |
| 246 | .. method:: ReadTransport.pause_reading() |
| 247 | |
| 248 | Pause the receiving end of the transport. No data will be passed to |
| 249 | the protocol's :meth:`protocol.data_received() <Protocol.data_received>` |
| 250 | method until :meth:`resume_reading` is called. |
| 251 | |
| 252 | .. versionchanged:: 3.7 |
| 253 | The method is idempotent, i.e. it can be called when the |
| 254 | transport is already paused or closed. |
| 255 | |
| 256 | .. method:: ReadTransport.resume_reading() |
| 257 | |
| 258 | Resume the receiving end. The protocol's |
| 259 | :meth:`protocol.data_received() <Protocol.data_received>` method |
| 260 | will be called once again if some data is available for reading. |
| 261 | |
| 262 | .. versionchanged:: 3.7 |
| 263 | The method is idempotent, i.e. it can be called when the |
| 264 | transport is already reading. |
| 265 | |
| 266 | |
| 267 | Write-only Transports |
| 268 | --------------------- |
| 269 | |
| 270 | .. method:: WriteTransport.abort() |
| 271 | |
| 272 | Close the transport immediately, without waiting for pending operations |
| 273 | to complete. Buffered data will be lost. No more data will be received. |
| 274 | The protocol's :meth:`protocol.connection_lost() |
| 275 | <BaseProtocol.connection_lost>` method will eventually be |
| 276 | called with :const:`None` as its argument. |
| 277 | |
| 278 | .. method:: WriteTransport.can_write_eof() |
| 279 | |
| 280 | Return :const:`True` if the transport supports |
| 281 | :meth:`~WriteTransport.write_eof`, :const:`False` if not. |
| 282 | |
| 283 | .. method:: WriteTransport.get_write_buffer_size() |
| 284 | |
| 285 | Return the current size of the output buffer used by the transport. |
| 286 | |
| 287 | .. method:: WriteTransport.get_write_buffer_limits() |
| 288 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 289 | Get the *high* and *low* watermarks for write flow control. Return a |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 290 | tuple ``(low, high)`` where *low* and *high* are positive number of |
| 291 | bytes. |
| 292 | |
| 293 | Use :meth:`set_write_buffer_limits` to set the limits. |
| 294 | |
| 295 | .. versionadded:: 3.4.2 |
| 296 | |
| 297 | .. method:: WriteTransport.set_write_buffer_limits(high=None, low=None) |
| 298 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 299 | Set the *high* and *low* watermarks for write flow control. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 300 | |
| 301 | These two values (measured in number of |
| 302 | bytes) control when the protocol's |
| 303 | :meth:`protocol.pause_writing() <BaseProtocol.pause_writing>` |
| 304 | and :meth:`protocol.resume_writing() <BaseProtocol.resume_writing>` |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 305 | methods are called. If specified, the low watermark must be less |
| 306 | than or equal to the high watermark. Neither *high* nor *low* |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 307 | can be negative. |
| 308 | |
| 309 | :meth:`~BaseProtocol.pause_writing` is called when the buffer size |
| 310 | becomes greater than or equal to the *high* value. If writing has |
| 311 | been paused, :meth:`~BaseProtocol.resume_writing` is called when |
| 312 | the buffer size becomes less than or equal to the *low* value. |
| 313 | |
| 314 | The defaults are implementation-specific. If only the |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 315 | high watermark is given, the low watermark defaults to an |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 316 | implementation-specific value less than or equal to the |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 317 | high watermark. Setting *high* to zero forces *low* to zero as |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 318 | well, and causes :meth:`~BaseProtocol.pause_writing` to be called |
| 319 | whenever the buffer becomes non-empty. Setting *low* to zero causes |
| 320 | :meth:`~BaseProtocol.resume_writing` to be called only once the |
| 321 | buffer is empty. Use of zero for either limit is generally |
| 322 | sub-optimal as it reduces opportunities for doing I/O and |
| 323 | computation concurrently. |
| 324 | |
| 325 | Use :meth:`~WriteTransport.get_write_buffer_limits` |
| 326 | to get the limits. |
| 327 | |
| 328 | .. method:: WriteTransport.write(data) |
| 329 | |
| 330 | Write some *data* bytes to the transport. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 331 | |
| 332 | This method does not block; it buffers the data and arranges for it |
| 333 | to be sent out asynchronously. |
| 334 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 335 | .. method:: WriteTransport.writelines(list_of_data) |
| 336 | |
| 337 | Write a list (or any iterable) of data bytes to the transport. |
| 338 | This is functionally equivalent to calling :meth:`write` on each |
| 339 | element yielded by the iterable, but may be implemented more |
| 340 | efficiently. |
| 341 | |
| 342 | .. method:: WriteTransport.write_eof() |
| 343 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 344 | Close the write end of the transport after flushing all buffered data. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 345 | Data may still be received. |
| 346 | |
| 347 | This method can raise :exc:`NotImplementedError` if the transport |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 348 | (e.g. SSL) doesn't support half-closed connections. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 349 | |
| 350 | |
| 351 | Datagram Transports |
| 352 | ------------------- |
| 353 | |
| 354 | .. method:: DatagramTransport.sendto(data, addr=None) |
| 355 | |
| 356 | Send the *data* bytes to the remote peer given by *addr* (a |
| 357 | transport-dependent target address). If *addr* is :const:`None`, |
| 358 | the data is sent to the target address given on transport |
| 359 | creation. |
| 360 | |
| 361 | This method does not block; it buffers the data and arranges |
| 362 | for it to be sent out asynchronously. |
| 363 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 364 | .. method:: DatagramTransport.abort() |
| 365 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 366 | Close the transport immediately, without waiting for pending |
| 367 | operations to complete. Buffered data will be lost. |
| 368 | No more data will be received. The protocol's |
| 369 | :meth:`protocol.connection_lost() <BaseProtocol.connection_lost>` |
| 370 | method will eventually be called with :const:`None` as its argument. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 371 | |
| 372 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 373 | .. _asyncio-subprocess-transports: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 374 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 375 | Subprocess Transports |
| 376 | --------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 377 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 378 | .. method:: SubprocessTransport.get_pid() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 379 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 380 | Return the subprocess process id as an integer. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 381 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 382 | .. method:: SubprocessTransport.get_pipe_transport(fd) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 383 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 384 | Return the transport for the communication pipe corresponding to the |
| 385 | integer file descriptor *fd*: |
Victor Stinner | 4270a24 | 2014-10-13 23:56:43 +0200 | [diff] [blame] | 386 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 387 | * ``0``: readable streaming transport of the standard input (*stdin*), |
| 388 | or :const:`None` if the subprocess was not created with ``stdin=PIPE`` |
| 389 | * ``1``: writable streaming transport of the standard output (*stdout*), |
| 390 | or :const:`None` if the subprocess was not created with ``stdout=PIPE`` |
| 391 | * ``2``: writable streaming transport of the standard error (*stderr*), |
| 392 | or :const:`None` if the subprocess was not created with ``stderr=PIPE`` |
| 393 | * other *fd*: :const:`None` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 394 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 395 | .. method:: SubprocessTransport.get_returncode() |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 396 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 397 | Return the subprocess return code as an integer or :const:`None` |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 398 | if it hasn't returned, which is similar to the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 399 | :attr:`subprocess.Popen.returncode` attribute. |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 400 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 401 | .. method:: SubprocessTransport.kill() |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 402 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 403 | Kill the subprocess. |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 404 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 405 | On POSIX systems, the function sends SIGKILL to the subprocess. |
| 406 | On Windows, this method is an alias for :meth:`terminate`. |
Victor Stinner | 933a8c8 | 2013-12-03 01:59:38 +0100 | [diff] [blame] | 407 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 408 | See also :meth:`subprocess.Popen.kill`. |
| 409 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 410 | .. method:: SubprocessTransport.send_signal(signal) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 411 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 412 | Send the *signal* number to the subprocess, as in |
| 413 | :meth:`subprocess.Popen.send_signal`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 414 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 415 | .. method:: SubprocessTransport.terminate() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 416 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 417 | Stop the subprocess. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 418 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 419 | On POSIX systems, this method sends SIGTERM to the subprocess. |
| 420 | On Windows, the Windows API function TerminateProcess() is called to |
| 421 | stop the subprocess. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 422 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 423 | See also :meth:`subprocess.Popen.terminate`. |
| 424 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 425 | .. method:: SubprocessTransport.close() |
Victor Stinner | 4270a24 | 2014-10-13 23:56:43 +0200 | [diff] [blame] | 426 | |
Yury Selivanov | 3faaa88 | 2018-09-14 13:32:07 -0700 | [diff] [blame] | 427 | Kill the subprocess by calling the :meth:`kill` method. |
| 428 | |
| 429 | If the subprocess hasn't returned yet, and close transports of |
| 430 | *stdin*, *stdout*, and *stderr* pipes. |
Victor Stinner | 4270a24 | 2014-10-13 23:56:43 +0200 | [diff] [blame] | 431 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 432 | |
Victor Stinner | 9592edb | 2014-02-02 15:03:02 +0100 | [diff] [blame] | 433 | .. _asyncio-protocol: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 434 | |
| 435 | Protocols |
| 436 | ========= |
| 437 | |
Kyle Stanley | f900064 | 2019-10-10 19:18:46 -0400 | [diff] [blame] | 438 | **Source code:** :source:`Lib/asyncio/protocols.py` |
| 439 | |
| 440 | --------------------------------------------------- |
| 441 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 442 | asyncio provides a set of abstract base classes that should be used |
| 443 | to implement network protocols. Those classes are meant to be used |
| 444 | together with :ref:`transports <asyncio-transport>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 445 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 446 | Subclasses of abstract base protocol classes may implement some or |
| 447 | all methods. All these methods are callbacks: they are called by |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 448 | transports on certain events, for example when some data is received. |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 449 | A base protocol method should be called by the corresponding transport. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 450 | |
| 451 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 452 | Base Protocols |
| 453 | -------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 454 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 455 | .. class:: BaseProtocol |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 456 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 457 | Base protocol with methods that all protocols share. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 458 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 459 | .. class:: Protocol(BaseProtocol) |
| 460 | |
| 461 | The base class for implementing streaming protocols |
| 462 | (TCP, Unix sockets, etc). |
| 463 | |
| 464 | .. class:: BufferedProtocol(BaseProtocol) |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 465 | |
| 466 | A base class for implementing streaming protocols with manual |
| 467 | control of the receive buffer. |
| 468 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 469 | .. class:: DatagramProtocol(BaseProtocol) |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 470 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 471 | The base class for implementing datagram (UDP) protocols. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 472 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 473 | .. class:: SubprocessProtocol(BaseProtocol) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 474 | |
| 475 | The base class for implementing protocols communicating with child |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 476 | processes (unidirectional pipes). |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 477 | |
| 478 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 479 | Base Protocol |
| 480 | ------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 481 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 482 | All asyncio protocols can implement Base Protocol callbacks. |
| 483 | |
| 484 | .. rubric:: Connection Callbacks |
| 485 | |
| 486 | Connection callbacks are called on all protocols, exactly once per |
| 487 | a successful connection. All other protocol callbacks can only be |
| 488 | called between those two methods. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 489 | |
| 490 | .. method:: BaseProtocol.connection_made(transport) |
| 491 | |
| 492 | Called when a connection is made. |
| 493 | |
| 494 | The *transport* argument is the transport representing the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 495 | connection. The protocol is responsible for storing the reference |
| 496 | to its transport. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 497 | |
| 498 | .. method:: BaseProtocol.connection_lost(exc) |
| 499 | |
| 500 | Called when the connection is lost or closed. |
| 501 | |
| 502 | The argument is either an exception object or :const:`None`. |
| 503 | The latter means a regular EOF is received, or the connection was |
| 504 | aborted or closed by this side of the connection. |
| 505 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 506 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 507 | .. rubric:: Flow Control Callbacks |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 508 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 509 | Flow control callbacks can be called by transports to pause or |
| 510 | resume writing performed by the protocol. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 511 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 512 | See the documentation of the :meth:`~WriteTransport.set_write_buffer_limits` |
| 513 | method for more details. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 514 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 515 | .. method:: BaseProtocol.pause_writing() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 516 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 517 | Called when the transport's buffer goes over the high watermark. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 518 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 519 | .. method:: BaseProtocol.resume_writing() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 520 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 521 | Called when the transport's buffer drains below the low watermark. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 522 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 523 | If the buffer size equals the high watermark, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 524 | :meth:`~BaseProtocol.pause_writing` is not called: the buffer size must |
| 525 | go strictly over. |
| 526 | |
| 527 | Conversely, :meth:`~BaseProtocol.resume_writing` is called when the |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 528 | buffer size is equal or lower than the low watermark. These end |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 529 | conditions are important to ensure that things go as expected when |
| 530 | either mark is zero. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 531 | |
| 532 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 533 | Streaming Protocols |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 534 | ------------------- |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 535 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 536 | Event methods, such as :meth:`loop.create_server`, |
| 537 | :meth:`loop.create_unix_server`, :meth:`loop.create_connection`, |
| 538 | :meth:`loop.create_unix_connection`, :meth:`loop.connect_accepted_socket`, |
| 539 | :meth:`loop.connect_read_pipe`, and :meth:`loop.connect_write_pipe` |
| 540 | accept factories that return streaming protocols. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 541 | |
| 542 | .. method:: Protocol.data_received(data) |
| 543 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 544 | Called when some data is received. *data* is a non-empty bytes |
| 545 | object containing the incoming data. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 546 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 547 | Whether the data is buffered, chunked or reassembled depends on |
| 548 | the transport. In general, you shouldn't rely on specific semantics |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 549 | and instead make your parsing generic and flexible. However, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 550 | data is always received in the correct order. |
| 551 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 552 | The method can be called an arbitrary number of times while |
| 553 | a connection is open. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 554 | |
| 555 | However, :meth:`protocol.eof_received() <Protocol.eof_received>` |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 556 | is called at most once. Once `eof_received()` is called, |
| 557 | ``data_received()`` is not called anymore. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 558 | |
| 559 | .. method:: Protocol.eof_received() |
| 560 | |
Barry Warsaw | dd9a0a1 | 2017-04-07 14:18:14 -0400 | [diff] [blame] | 561 | Called when the other end signals it won't send any more data |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 562 | (for example by calling :meth:`transport.write_eof() |
| 563 | <WriteTransport.write_eof>`, if the other end also uses |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 564 | asyncio). |
| 565 | |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 566 | This method may return a false value (including ``None``), in which case |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 567 | the transport will close itself. Conversely, if this method returns a |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 568 | true value, the protocol used determines whether to close the transport. |
| 569 | Since the default implementation returns ``None``, it implicitly closes the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 570 | connection. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 571 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 572 | Some transports, including SSL, don't support half-closed connections, |
| 573 | in which case returning true from this method will result in the connection |
| 574 | being closed. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 575 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 576 | |
Victor Stinner | 54a231d | 2015-01-29 13:33:15 +0100 | [diff] [blame] | 577 | State machine: |
| 578 | |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 579 | .. code-block:: none |
| 580 | |
| 581 | start -> connection_made |
| 582 | [-> data_received]* |
| 583 | [-> eof_received]? |
| 584 | -> connection_lost -> end |
| 585 | |
| 586 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 587 | Buffered Streaming Protocols |
| 588 | ---------------------------- |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 589 | |
| 590 | .. versionadded:: 3.7 |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 591 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 592 | Buffered Protocols can be used with any event loop method |
| 593 | that supports `Streaming Protocols`_. |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 594 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 595 | ``BufferedProtocol`` implementations allow explicit manual allocation |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 596 | and control of the receive buffer. Event loops can then use the buffer |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 597 | provided by the protocol to avoid unnecessary data copies. This |
| 598 | can result in noticeable performance improvement for protocols that |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 599 | receive big amounts of data. Sophisticated protocol implementations |
| 600 | can significantly reduce the number of buffer allocations. |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 601 | |
| 602 | The following callbacks are called on :class:`BufferedProtocol` |
| 603 | instances: |
| 604 | |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 605 | .. method:: BufferedProtocol.get_buffer(sizehint) |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 606 | |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 607 | Called to allocate a new receive buffer. |
| 608 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 609 | *sizehint* is the recommended minimum size for the returned |
| 610 | buffer. It is acceptable to return smaller or larger buffers |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 611 | than what *sizehint* suggests. When set to -1, the buffer size |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 612 | can be arbitrary. It is an error to return a buffer with a zero size. |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 613 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 614 | ``get_buffer()`` must return an object implementing the |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 615 | :ref:`buffer protocol <bufferobjects>`. |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 616 | |
| 617 | .. method:: BufferedProtocol.buffer_updated(nbytes) |
| 618 | |
| 619 | Called when the buffer was updated with the received data. |
| 620 | |
| 621 | *nbytes* is the total number of bytes that were written to the buffer. |
| 622 | |
| 623 | .. method:: BufferedProtocol.eof_received() |
| 624 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 625 | See the documentation of the :meth:`protocol.eof_received() |
| 626 | <Protocol.eof_received>` method. |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 627 | |
| 628 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 629 | :meth:`~BufferedProtocol.get_buffer` can be called an arbitrary number |
| 630 | of times during a connection. However, :meth:`protocol.eof_received() |
| 631 | <Protocol.eof_received>` is called at most once |
| 632 | and, if called, :meth:`~BufferedProtocol.get_buffer` and |
| 633 | :meth:`~BufferedProtocol.buffer_updated` won't be called after it. |
Yury Selivanov | 631fd38 | 2018-01-28 16:30:26 -0500 | [diff] [blame] | 634 | |
| 635 | State machine: |
| 636 | |
| 637 | .. code-block:: none |
| 638 | |
| 639 | start -> connection_made |
| 640 | [-> get_buffer |
| 641 | [-> buffer_updated]? |
| 642 | ]* |
| 643 | [-> eof_received]? |
| 644 | -> connection_lost -> end |
Victor Stinner | 54a231d | 2015-01-29 13:33:15 +0100 | [diff] [blame] | 645 | |
| 646 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 647 | Datagram Protocols |
Victor Stinner | 0c6f1ca | 2013-12-03 01:46:39 +0100 | [diff] [blame] | 648 | ------------------ |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 649 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 650 | Datagram Protocol instances should be constructed by protocol |
| 651 | factories passed to the :meth:`loop.create_datagram_endpoint` method. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 652 | |
| 653 | .. method:: DatagramProtocol.datagram_received(data, addr) |
| 654 | |
| 655 | Called when a datagram is received. *data* is a bytes object containing |
| 656 | the incoming data. *addr* is the address of the peer sending the data; |
| 657 | the exact format depends on the transport. |
| 658 | |
| 659 | .. method:: DatagramProtocol.error_received(exc) |
| 660 | |
| 661 | Called when a previous send or receive operation raises an |
| 662 | :class:`OSError`. *exc* is the :class:`OSError` instance. |
| 663 | |
| 664 | This method is called in rare conditions, when the transport (e.g. UDP) |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 665 | detects that a datagram could not be delivered to its recipient. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 666 | In many conditions though, undeliverable datagrams will be silently |
| 667 | dropped. |
| 668 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 669 | .. note:: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 670 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 671 | On BSD systems (macOS, FreeBSD, etc.) flow control is not supported |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 672 | for datagram protocols, because there is no reliable way to detect send |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 673 | failures caused by writing too many packets. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 674 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 675 | The socket always appears 'ready' and excess packets are dropped. An |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 676 | :class:`OSError` with ``errno`` set to :const:`errno.ENOBUFS` may |
| 677 | or may not be raised; if it is raised, it will be reported to |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 678 | :meth:`DatagramProtocol.error_received` but otherwise ignored. |
| 679 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 680 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 681 | .. _asyncio-subprocess-protocols: |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 682 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 683 | Subprocess Protocols |
| 684 | -------------------- |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 685 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 686 | Datagram Protocol instances should be constructed by protocol |
| 687 | factories passed to the :meth:`loop.subprocess_exec` and |
| 688 | :meth:`loop.subprocess_shell` methods. |
| 689 | |
| 690 | .. method:: SubprocessProtocol.pipe_data_received(fd, data) |
| 691 | |
| 692 | Called when the child process writes data into its stdout or stderr |
| 693 | pipe. |
| 694 | |
| 695 | *fd* is the integer file descriptor of the pipe. |
| 696 | |
| 697 | *data* is a non-empty bytes object containing the received data. |
| 698 | |
| 699 | .. method:: SubprocessProtocol.pipe_connection_lost(fd, exc) |
| 700 | |
| 701 | Called when one of the pipes communicating with the child process |
| 702 | is closed. |
| 703 | |
| 704 | *fd* is the integer file descriptor that was closed. |
| 705 | |
| 706 | .. method:: SubprocessProtocol.process_exited() |
| 707 | |
| 708 | Called when the child process has exited. |
Victor Stinner | 4b4f9eb | 2014-01-24 17:33:20 +0100 | [diff] [blame] | 709 | |
| 710 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 711 | Examples |
| 712 | ======== |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 713 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 714 | .. _asyncio_example_tcp_echo_server_protocol: |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 715 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 716 | TCP Echo Server |
| 717 | --------------- |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 718 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 719 | Create a TCP echo server using the :meth:`loop.create_server` method, send back |
| 720 | received data, and close the connection:: |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 721 | |
| 722 | import asyncio |
| 723 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 724 | |
Braden Groom | 43a5bd7 | 2018-10-15 14:39:16 -0700 | [diff] [blame] | 725 | class EchoServerProtocol(asyncio.Protocol): |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 726 | def connection_made(self, transport): |
| 727 | peername = transport.get_extra_info('peername') |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 728 | print('Connection from {}'.format(peername)) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 729 | self.transport = transport |
| 730 | |
| 731 | def data_received(self, data): |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 732 | message = data.decode() |
| 733 | print('Data received: {!r}'.format(message)) |
| 734 | |
| 735 | print('Send: {!r}'.format(message)) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 736 | self.transport.write(data) |
| 737 | |
Victor Stinner | 5366434 | 2014-10-12 11:35:09 +0200 | [diff] [blame] | 738 | print('Close the client socket') |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 739 | self.transport.close() |
| 740 | |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 741 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 742 | async def main(): |
| 743 | # Get a reference to the event loop as we plan to use |
| 744 | # low-level APIs. |
| 745 | loop = asyncio.get_running_loop() |
Victor Stinner | c2721b4 | 2014-10-12 11:13:40 +0200 | [diff] [blame] | 746 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 747 | server = await loop.create_server( |
Braden Groom | 43a5bd7 | 2018-10-15 14:39:16 -0700 | [diff] [blame] | 748 | lambda: EchoServerProtocol(), |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 749 | '127.0.0.1', 8888) |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 750 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 751 | async with server: |
| 752 | await server.serve_forever() |
| 753 | |
| 754 | |
| 755 | asyncio.run(main()) |
| 756 | |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 757 | |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 758 | .. seealso:: |
| 759 | |
| 760 | The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 761 | example uses the high-level :func:`asyncio.start_server` function. |
| 762 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 763 | .. _asyncio_example_tcp_echo_client_protocol: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 764 | |
| 765 | TCP Echo Client |
| 766 | --------------- |
| 767 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 768 | A TCP echo client using the :meth:`loop.create_connection` method, sends |
| 769 | data, and waits until the connection is closed:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 770 | |
| 771 | import asyncio |
| 772 | |
| 773 | |
| 774 | class EchoClientProtocol(asyncio.Protocol): |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 775 | def __init__(self, message, on_con_lost): |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 776 | self.message = message |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 777 | self.on_con_lost = on_con_lost |
| 778 | |
| 779 | def connection_made(self, transport): |
| 780 | transport.write(self.message.encode()) |
| 781 | print('Data sent: {!r}'.format(self.message)) |
| 782 | |
| 783 | def data_received(self, data): |
| 784 | print('Data received: {!r}'.format(data.decode())) |
| 785 | |
| 786 | def connection_lost(self, exc): |
| 787 | print('The server closed the connection') |
| 788 | self.on_con_lost.set_result(True) |
| 789 | |
| 790 | |
| 791 | async def main(): |
| 792 | # Get a reference to the event loop as we plan to use |
| 793 | # low-level APIs. |
| 794 | loop = asyncio.get_running_loop() |
| 795 | |
| 796 | on_con_lost = loop.create_future() |
| 797 | message = 'Hello World!' |
| 798 | |
| 799 | transport, protocol = await loop.create_connection( |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 800 | lambda: EchoClientProtocol(message, on_con_lost), |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 801 | '127.0.0.1', 8888) |
| 802 | |
| 803 | # Wait until the protocol signals that the connection |
| 804 | # is lost and close the transport. |
| 805 | try: |
| 806 | await on_con_lost |
| 807 | finally: |
| 808 | transport.close() |
| 809 | |
| 810 | |
| 811 | asyncio.run(main()) |
| 812 | |
| 813 | |
| 814 | .. seealso:: |
| 815 | |
| 816 | The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 817 | example uses the high-level :func:`asyncio.open_connection` function. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 818 | |
| 819 | |
| 820 | .. _asyncio-udp-echo-server-protocol: |
| 821 | |
| 822 | UDP Echo Server |
| 823 | --------------- |
| 824 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 825 | A UDP echo server, using the :meth:`loop.create_datagram_endpoint` |
| 826 | method, sends back received data:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 827 | |
| 828 | import asyncio |
| 829 | |
| 830 | |
| 831 | class EchoServerProtocol: |
| 832 | def connection_made(self, transport): |
| 833 | self.transport = transport |
| 834 | |
| 835 | def datagram_received(self, data, addr): |
| 836 | message = data.decode() |
| 837 | print('Received %r from %s' % (message, addr)) |
| 838 | print('Send %r to %s' % (message, addr)) |
| 839 | self.transport.sendto(data, addr) |
| 840 | |
| 841 | |
| 842 | async def main(): |
| 843 | print("Starting UDP server") |
| 844 | |
| 845 | # Get a reference to the event loop as we plan to use |
| 846 | # low-level APIs. |
| 847 | loop = asyncio.get_running_loop() |
| 848 | |
| 849 | # One protocol instance will be created to serve all |
| 850 | # client requests. |
| 851 | transport, protocol = await loop.create_datagram_endpoint( |
| 852 | lambda: EchoServerProtocol(), |
| 853 | local_addr=('127.0.0.1', 9999)) |
| 854 | |
| 855 | try: |
| 856 | await asyncio.sleep(3600) # Serve for 1 hour. |
| 857 | finally: |
| 858 | transport.close() |
| 859 | |
| 860 | |
| 861 | asyncio.run(main()) |
Victor Stinner | ed05159 | 2014-10-12 20:18:16 +0200 | [diff] [blame] | 862 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 863 | |
| 864 | .. _asyncio-udp-echo-client-protocol: |
| 865 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 866 | UDP Echo Client |
| 867 | --------------- |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 868 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 869 | A UDP echo client, using the :meth:`loop.create_datagram_endpoint` |
| 870 | method, sends data and closes the transport when it receives the answer:: |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 871 | |
| 872 | import asyncio |
| 873 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 874 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 875 | class EchoClientProtocol: |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 876 | def __init__(self, message, on_con_lost): |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 877 | self.message = message |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 878 | self.on_con_lost = on_con_lost |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 879 | self.transport = None |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 880 | |
| 881 | def connection_made(self, transport): |
| 882 | self.transport = transport |
| 883 | print('Send:', self.message) |
| 884 | self.transport.sendto(self.message.encode()) |
| 885 | |
| 886 | def datagram_received(self, data, addr): |
| 887 | print("Received:", data.decode()) |
| 888 | |
| 889 | print("Close the socket") |
| 890 | self.transport.close() |
| 891 | |
| 892 | def error_received(self, exc): |
| 893 | print('Error received:', exc) |
| 894 | |
| 895 | def connection_lost(self, exc): |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 896 | print("Connection closed") |
| 897 | self.on_con_lost.set_result(True) |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 898 | |
| 899 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 900 | async def main(): |
| 901 | # Get a reference to the event loop as we plan to use |
| 902 | # low-level APIs. |
| 903 | loop = asyncio.get_running_loop() |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 904 | |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 905 | on_con_lost = loop.create_future() |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 906 | message = "Hello World!" |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 907 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 908 | transport, protocol = await loop.create_datagram_endpoint( |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 909 | lambda: EchoClientProtocol(message, on_con_lost), |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 910 | remote_addr=('127.0.0.1', 9999)) |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 911 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 912 | try: |
Hrvoje Nikšić | c717c73 | 2019-09-15 19:06:02 +0200 | [diff] [blame] | 913 | await on_con_lost |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 914 | finally: |
| 915 | transport.close() |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 916 | |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 917 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 918 | asyncio.run(main()) |
Victor Stinner | c7edffd | 2014-10-12 11:24:26 +0200 | [diff] [blame] | 919 | |
| 920 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 921 | .. _asyncio_example_create_connection: |
Victor Stinner | a881a7f | 2013-12-09 13:19:23 +0100 | [diff] [blame] | 922 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 923 | Connecting Existing Sockets |
| 924 | --------------------------- |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 925 | |
| 926 | Wait until a socket receives data using the |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 927 | :meth:`loop.create_connection` method with a protocol:: |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 928 | |
| 929 | import asyncio |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 930 | import socket |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 931 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 932 | |
| 933 | class MyProtocol(asyncio.Protocol): |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 934 | |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 935 | def __init__(self, on_con_lost): |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 936 | self.transport = None |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 937 | self.on_con_lost = on_con_lost |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 938 | |
| 939 | def connection_made(self, transport): |
| 940 | self.transport = transport |
| 941 | |
| 942 | def data_received(self, data): |
| 943 | print("Received:", data.decode()) |
| 944 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 945 | # We are done: close the transport; |
| 946 | # connection_lost() will be called automatically. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 947 | self.transport.close() |
| 948 | |
| 949 | def connection_lost(self, exc): |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 950 | # The socket has been closed |
| 951 | self.on_con_lost.set_result(True) |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 952 | |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 953 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 954 | async def main(): |
| 955 | # Get a reference to the event loop as we plan to use |
| 956 | # low-level APIs. |
| 957 | loop = asyncio.get_running_loop() |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 958 | on_con_lost = loop.create_future() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 959 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 960 | # Create a pair of connected sockets |
| 961 | rsock, wsock = socket.socketpair() |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 962 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 963 | # Register the socket to wait for data. |
| 964 | transport, protocol = await loop.create_connection( |
Hrvoje Nikšić | 5d359cc | 2019-09-17 09:16:43 +0200 | [diff] [blame] | 965 | lambda: MyProtocol(on_con_lost), sock=rsock) |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 966 | |
| 967 | # Simulate the reception of data from the network. |
| 968 | loop.call_soon(wsock.send, 'abc'.encode()) |
| 969 | |
| 970 | try: |
| 971 | await protocol.on_con_lost |
| 972 | finally: |
| 973 | transport.close() |
| 974 | wsock.close() |
| 975 | |
| 976 | asyncio.run(main()) |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 977 | |
| 978 | .. seealso:: |
| 979 | |
| 980 | The :ref:`watch a file descriptor for read events |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 981 | <asyncio_example_watch_fd>` example uses the low-level |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 982 | :meth:`loop.add_reader` method to register an FD. |
Victor Stinner | 04e6df3 | 2014-10-11 16:16:27 +0200 | [diff] [blame] | 983 | |
| 984 | The :ref:`register an open socket to wait for data using streams |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 985 | <asyncio_example_create_connection-streams>` example uses high-level streams |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 986 | created by the :func:`open_connection` function in a coroutine. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 987 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 988 | .. _asyncio_example_subprocess_proto: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 989 | |
| 990 | loop.subprocess_exec() and SubprocessProtocol |
| 991 | --------------------------------------------- |
| 992 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 993 | An example of a subprocess protocol used to get the output of a |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 994 | subprocess and to wait for the subprocess exit. |
| 995 | |
marload | 6fc732a | 2020-07-11 00:43:31 +0900 | [diff] [blame] | 996 | The subprocess is created by the :meth:`loop.subprocess_exec` method:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 997 | |
| 998 | import asyncio |
| 999 | import sys |
| 1000 | |
| 1001 | class DateProtocol(asyncio.SubprocessProtocol): |
| 1002 | def __init__(self, exit_future): |
| 1003 | self.exit_future = exit_future |
| 1004 | self.output = bytearray() |
| 1005 | |
| 1006 | def pipe_data_received(self, fd, data): |
| 1007 | self.output.extend(data) |
| 1008 | |
| 1009 | def process_exited(self): |
| 1010 | self.exit_future.set_result(True) |
| 1011 | |
| 1012 | async def get_date(): |
| 1013 | # Get a reference to the event loop as we plan to use |
| 1014 | # low-level APIs. |
| 1015 | loop = asyncio.get_running_loop() |
| 1016 | |
| 1017 | code = 'import datetime; print(datetime.datetime.now())' |
| 1018 | exit_future = asyncio.Future(loop=loop) |
| 1019 | |
| 1020 | # Create the subprocess controlled by DateProtocol; |
| 1021 | # redirect the standard output into a pipe. |
| 1022 | transport, protocol = await loop.subprocess_exec( |
| 1023 | lambda: DateProtocol(exit_future), |
| 1024 | sys.executable, '-c', code, |
| 1025 | stdin=None, stderr=None) |
| 1026 | |
| 1027 | # Wait for the subprocess exit using the process_exited() |
| 1028 | # method of the protocol. |
| 1029 | await exit_future |
| 1030 | |
| 1031 | # Close the stdout pipe. |
| 1032 | transport.close() |
| 1033 | |
| 1034 | # Read the output which was collected by the |
| 1035 | # pipe_data_received() method of the protocol. |
| 1036 | data = bytes(protocol.output) |
| 1037 | return data.decode('ascii').rstrip() |
| 1038 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1039 | date = asyncio.run(get_date()) |
| 1040 | print(f"Current date: {date}") |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 1041 | |
| 1042 | See also the :ref:`same example <asyncio_example_create_subprocess_exec>` |
| 1043 | written using high-level APIs. |