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