blob: 348ced57772f24ea299555dd6cb2314aa4d16c5a [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
lf627d2c82017-07-25 17:03:51 -06003
Yury Selivanov7c7605f2018-09-11 09:54:40 -07004========================
5Transports and Protocols
6========================
lf627d2c82017-07-25 17:03:51 -06007
Yury Selivanov7c7605f2018-09-11 09:54:40 -07008.. rubric:: Preface
9
10Transports and Protocols are used by **low-level** event loop
11APIs such as :meth:`loop.create_connection`. They require using
12callback-based programming style and enable high-performance
13implementations of network or IPC protocols (e.g. HTTP).
14
15Essentially, transports and protocols should only be used in
16libraries and frameworks and never in high-level asyncio
17applications.
18
19This documentation page covers both `Transports`_ and `Protocols`_.
20
21.. rubric:: Introduction
22
23At the highest level, the transport is concerned with *how* bytes
24are transmitted, while the protocol determines *which* bytes to
25transmit (and to some extent when).
26
27A different way of saying the same thing: a transport is an
28abstraction for a socket (or similar I/O endpoint) while a protocol
29is an abstraction for an application, from the transport's point
30of view.
31
32Yet another view is simply that the transport and protocol interfaces
33together define an abstract interface for using network I/O and
34interprocess I/O.
35
36There is always a 1:1 relationship between transport and protocol
37objects: the protocol calls transport methods to send data,
38while the transport calls protocol methods to pass it data that
39has been received.
40
41Most 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
44for an accepted connection, represented by a *Transport* object.
45Such methods usually return a tuple of ``(transport, protocol)``.
46
47.. rubric:: Contents
48
49This 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 Stinner1ca5ba62013-12-03 01:49:43 +010063
Victor Stinner9592edb2014-02-02 15:03:02 +010064.. _asyncio-transport:
Victor Stinnerea3183f2013-12-03 01:08:00 +010065
66Transports
67==========
68
Guido van Rossum589872c2014-03-29 21:14:04 -070069Transports are classes provided by :mod:`asyncio` in order to abstract
Yury Selivanov7c7605f2018-09-11 09:54:40 -070070various kinds of communication channels.
Victor Stinnerea3183f2013-12-03 01:08:00 +010071
Yury Selivanov7c7605f2018-09-11 09:54:40 -070072Transport objects are always instantiated by an
73ref:`asyncio event loop <asyncio-event-loop>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010074
Yury Selivanov7c7605f2018-09-11 09:54:40 -070075asyncio implements transports for TCP, UDP, SSL, and subprocess pipes.
76The methods available on a transport depend on the transport's kind.
Victor Stinnerea3183f2013-12-03 01:08:00 +010077
Victor Stinner83704962015-02-25 14:24:15 +010078The transport classes are :ref:`not thread safe <asyncio-multithreading>`.
79
Yury Selivanov3432f2f2016-12-12 16:44:58 -050080
Yury Selivanov7c7605f2018-09-11 09:54:40 -070081Transports Hierarchy
82--------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +010083
84.. class:: BaseTransport
85
Yury Selivanov7c7605f2018-09-11 09:54:40 -070086 Base class for all transports. Contains methods that all
87 asyncio transports share.
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Yury Selivanov7c7605f2018-09-11 09:54:40 -070089.. class:: WriteTransport(BaseTransport)
Victor Stinnerea3183f2013-12-03 01:08:00 +010090
Yury Selivanov7c7605f2018-09-11 09:54:40 -070091 A base transport for write-only connections.
Victor Stinnerea3183f2013-12-03 01:08:00 +010092
Yury Selivanov7c7605f2018-09-11 09:54:40 -070093 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 Selivanov1744d532015-11-16 12:46:41 -050097
Yury Selivanov7c7605f2018-09-11 09:54:40 -070098.. class:: ReadTransport(BaseTransport)
Yury Selivanov1744d532015-11-16 12:46:41 -050099
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700100 A base transport for read-only connections.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100101
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700102 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 Stinnerea3183f2013-12-03 01:08:00 +0100106
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700107.. class:: Transport(WriteTransport, ReadTransport)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100108
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700109 Interface representing a bidirectional transport, such as a
110 TCP connection.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100111
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700112 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 Stinnerea3183f2013-12-03 01:08:00 +0100115
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700116 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 Stinnerf7dc7fb2015-09-21 18:06:17 +0200120
Victor Stinnerea3183f2013-12-03 01:08:00 +0100121
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700122.. class:: DatagramTransport(BaseTransport)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100123
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700124 A transport for datagram (UDP) connections.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100125
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700126 Instances of the *DatagramTransport* class are returned from
127 the :meth:`loop.create_datagram_endpoint` event loop method.
Yury Selivanovd757aaf2017-12-18 17:03:23 -0500128
Victor Stinnerea3183f2013-12-03 01:08:00 +0100129
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700130.. 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
140Base Transport
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100141--------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700143.. method:: BaseTransport.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100144
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700145 Close the transport.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100146
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700147 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 Stinnerea3183f2013-12-03 01:08:00 +0100153
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700154.. method:: BaseTransport.is_closing()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100155
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700156 Return ``True`` if the transport is closing or is closed.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100157
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700158.. method:: BaseTransport.get_extra_info(name, default=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100159
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700160 Return information about the transport or underlying resources
161 it uses.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100162
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700163 *name* is a string representing the piece of transport-specific
164 information to get.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100165
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700166 *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 Stinner52bb9492014-08-26 00:22:28 +0200170
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700171 For example, the following code attempts to get the underlying
172 socket object of the transport::
Victor Stinner52bb9492014-08-26 00:22:28 +0200173
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700174 sock = transport.get_extra_info('socket')
175 if sock is not None:
176 print(sock.getsockopt(...))
Victor Stinner52bb9492014-08-26 00:22:28 +0200177
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700178 Categories of information that can be queried on some transports:
Victor Stinner52bb9492014-08-26 00:22:28 +0200179
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700180 * socket:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100181
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700182 - ``'peername'``: the remote address to which the socket is
183 connected, result of :meth:`socket.socket.getpeername`
184 (``None`` on error)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100185
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700186 - ``'socket'``: :class:`socket.socket` instance
Victor Stinnerea3183f2013-12-03 01:08:00 +0100187
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700188 - ``'sockname'``: the socket's own address,
189 result of :meth:`socket.socket.getsockname`
Kojo Idrissa5200a7c2017-06-20 14:32:00 -0500190
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700191 * SSL socket:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100192
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700193 - ``'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 Stinner52bb9492014-08-26 00:22:28 +0200196
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700197 - ``'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 Stinnerea3183f2013-12-03 01:08:00 +0100201
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700202 - ``'peercert'``: peer certificate; result of
203 :meth:`ssl.SSLSocket.getpeercert`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100204
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700205 - ``'sslcontext'``: :class:`ssl.SSLContext` instance
Victor Stinnerea3183f2013-12-03 01:08:00 +0100206
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700207 - ``'ssl_object'``: :class:`ssl.SSLObject` or
208 :class:`ssl.SSLSocket` instance
Victor Stinnerea3183f2013-12-03 01:08:00 +0100209
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700210 * pipe:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100211
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700212 - ``'pipe'``: pipe object
Victor Stinnerea3183f2013-12-03 01:08:00 +0100213
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700214 * subprocess:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100215
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700216 - ``'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 Stinnerea3183f2013-12-03 01:08:00 +0100228
229
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700230Read-only Transports
231--------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100232
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700233.. method:: ReadTransport.is_reading()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100234
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700235 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
260Write-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 Stinnerea3183f2013-12-03 01:08:00 +0100324
325 This method does not block; it buffers the data and arranges for it
326 to be sent out asynchronously.
327
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700328.. 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
344Datagram 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 Stinnerea3183f2013-12-03 01:08:00 +0100357.. method:: DatagramTransport.abort()
358
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700359 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 Stinnerea3183f2013-12-03 01:08:00 +0100364
365
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700366.. _asyncio-subprocess-transports:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100367
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700368Subprocess Transports
369---------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100370
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700371.. method:: SubprocessTransport.get_pid()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100372
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700373 Return the subprocess process id as an integer.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100374
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700375.. method:: SubprocessTransport.get_pipe_transport(fd)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100376
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700377 Return the transport for the communication pipe corresponding to the
378 integer file descriptor *fd*:
Victor Stinner4270a242014-10-13 23:56:43 +0200379
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700380 * ``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 Stinnerea3183f2013-12-03 01:08:00 +0100387
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700388.. method:: SubprocessTransport.get_returncode()
Victor Stinner933a8c82013-12-03 01:59:38 +0100389
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700390 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 Stinner933a8c82013-12-03 01:59:38 +0100393
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700394.. method:: SubprocessTransport.kill()
Victor Stinner933a8c82013-12-03 01:59:38 +0100395
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700396 Kill the subprocess, as in :meth:`subprocess.Popen.kill`.
Victor Stinner933a8c82013-12-03 01:59:38 +0100397
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700398 On POSIX systems, the function sends SIGKILL to the subprocess.
399 On Windows, this method is an alias for :meth:`terminate`.
Victor Stinner933a8c82013-12-03 01:59:38 +0100400
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700401.. method:: SubprocessTransport.send_signal(signal)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100402
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700403 Send the *signal* number to the subprocess, as in
404 :meth:`subprocess.Popen.send_signal`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100405
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700406.. method:: SubprocessTransport.terminate()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100407
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700408 Ask the subprocess to stop, as in :meth:`subprocess.Popen.terminate`.
409 This method is an alias for the :meth:`close` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100410
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700411 On POSIX systems, this method sends SIGTERM to the subprocess.
412 On Windows, the Windows API function TerminateProcess() is called to
413 stop the subprocess.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100414
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700415.. method:: SubprocessTransport.close()
Victor Stinner4270a242014-10-13 23:56:43 +0200416
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700417 Ask the subprocess to stop by calling the :meth:`terminate` method
418 if the subprocess hasn't returned yet, and close transports of all
419 pipes (*stdin*, *stdout* and *stderr*).
Victor Stinner4270a242014-10-13 23:56:43 +0200420
Victor Stinnerea3183f2013-12-03 01:08:00 +0100421
Victor Stinner9592edb2014-02-02 15:03:02 +0100422.. _asyncio-protocol:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100423
424Protocols
425=========
426
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700427asyncio provides a set of abstract base classes that should be used
428to implement network protocols. Those classes are meant to be used
429together with :ref:`transports <asyncio-transport>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100430
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700431Subclasses of abstract base protocol classes can implement some or
432all methods. All those methods are callbacks: they are called by
433transports on certain events, for example when some data is received.
434Base protocol methods are not supposed to be called by anything but
435the corresponding transport.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100436
437
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700438Base Protocols
439--------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100440
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700441.. class:: BaseProtocol
Victor Stinnerea3183f2013-12-03 01:08:00 +0100442
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700443 Base protocol with methods that all protocols share.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100444
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700445.. class:: Protocol(BaseProtocol)
446
447 The base class for implementing streaming protocols
448 (TCP, Unix sockets, etc).
449
450.. class:: BufferedProtocol(BaseProtocol)
Yury Selivanov631fd382018-01-28 16:30:26 -0500451
452 A base class for implementing streaming protocols with manual
453 control of the receive buffer.
454
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700455.. class:: DatagramProtocol(BaseProtocol)
Yury Selivanov631fd382018-01-28 16:30:26 -0500456
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700457 The base class for implementing datagram (UDP) protocols.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100458
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700459.. class:: SubprocessProtocol(BaseProtocol)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100460
461 The base class for implementing protocols communicating with child
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700462 processes (unidirectional pipes).
Victor Stinnerea3183f2013-12-03 01:08:00 +0100463
464
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700465Base Protocol
466-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100467
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700468All asyncio protocols can implement Base Protocol callbacks.
469
470.. rubric:: Connection Callbacks
471
472Connection callbacks are called on all protocols, exactly once per
473a successful connection. All other protocol callbacks can only be
474called between those two methods.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100475
476.. method:: BaseProtocol.connection_made(transport)
477
478 Called when a connection is made.
479
480 The *transport* argument is the transport representing the
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700481 connection. The protocol is responsible for storing the reference
482 to its transport.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100483
484.. method:: BaseProtocol.connection_lost(exc)
485
486 Called when the connection is lost or closed.
487
488 The argument is either an exception object or :const:`None`.
489 The latter means a regular EOF is received, or the connection was
490 aborted or closed by this side of the connection.
491
Victor Stinnerea3183f2013-12-03 01:08:00 +0100492
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700493.. rubric:: Flow Control Callbacks
Victor Stinnerea3183f2013-12-03 01:08:00 +0100494
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700495Flow control callbacks can be called by transports to pause or
496resume writing performed by the protocol.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100497
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700498See the documentation of the :meth:`~WriteTransport.set_write_buffer_limits`
499method for more details.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100500
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700501.. method:: BaseProtocol.pause_writing()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100502
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700503 Called when the transport's buffer goes over the high-water mark.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100504
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700505.. method:: BaseProtocol.resume_writing()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100506
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700507 Called when the transport's buffer drains below the low-water mark.
508
509If the buffer size equals the high-water mark,
510:meth:`~BaseProtocol.pause_writing` is not called: the buffer size must
511go strictly over.
512
513Conversely, :meth:`~BaseProtocol.resume_writing` is called when the
514buffer size is equal or lower than the low-water mark. These end
515conditions are important to ensure that things go as expected when
516either mark is zero.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100517
518
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700519Streaming Protocols
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100520-------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100521
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700522Event methods, such as :meth:`loop.create_server`,
523:meth:`loop.create_unix_server`, :meth:`loop.create_connection`,
524:meth:`loop.create_unix_connection`, :meth:`loop.connect_accepted_socket`,
525:meth:`loop.connect_read_pipe`, and :meth:`loop.connect_write_pipe`
526accept factories that return streaming protocols.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100527
528.. method:: Protocol.data_received(data)
529
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700530 Called when some data is received. *data* is a non-empty bytes
531 object containing the incoming data.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100532
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700533 Whether the data is buffered, chunked or reassembled depends on
534 the transport. In general, you shouldn't rely on specific semantics
535 and instead make your parsing generic and flexible enough. However,
536 data is always received in the correct order.
537
538 The method can be called an arbitrary number of times during
539 a connection.
540
541 However, :meth:`protocol.eof_received() <Protocol.eof_received>`
542 is called at most once and, if called,
543 :meth:`protocol.data_received() <Protocol.data_received>`
544 won't be called after it.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100545
546.. method:: Protocol.eof_received()
547
Barry Warsawdd9a0a12017-04-07 14:18:14 -0400548 Called when the other end signals it won't send any more data
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700549 (for example by calling :meth:`transport.write_eof()
550 <WriteTransport.write_eof>`, if the other end also uses
Victor Stinnerea3183f2013-12-03 01:08:00 +0100551 asyncio).
552
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300553 This method may return a false value (including ``None``), in which case
Victor Stinnerea3183f2013-12-03 01:08:00 +0100554 the transport will close itself. Conversely, if this method returns a
555 true value, closing the transport is up to the protocol. Since the
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700556 default implementation returns ``None``, it implicitly closes the
557 connection.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100558
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700559 Some transports such as SSL don't support half-closed connections,
560 in which case returning true from this method will not prevent closing
561 the connection.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100562
Victor Stinnerea3183f2013-12-03 01:08:00 +0100563
Victor Stinner54a231d2015-01-29 13:33:15 +0100564State machine:
565
Yury Selivanov631fd382018-01-28 16:30:26 -0500566.. code-block:: none
567
568 start -> connection_made
569 [-> data_received]*
570 [-> eof_received]?
571 -> connection_lost -> end
572
573
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700574Buffered Streaming Protocols
575----------------------------
Yury Selivanov631fd382018-01-28 16:30:26 -0500576
577.. versionadded:: 3.7
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700578 **Important:** this has been added to asyncio in Python 3.7
579 *on a provisional basis*! This is as an experimental API that
580 might be changed or removed completely in Python 3.8.
Yury Selivanov631fd382018-01-28 16:30:26 -0500581
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700582Buffered Protocols can be used with any event loop method
583that supports `Streaming Protocols`_.
Yury Selivanov631fd382018-01-28 16:30:26 -0500584
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700585The idea of ``BufferedProtocol`` is that it allows to manually allocate
Yury Selivanov631fd382018-01-28 16:30:26 -0500586and control the receive buffer. Event loops can then use the buffer
587provided by the protocol to avoid unnecessary data copies. This
588can result in noticeable performance improvement for protocols that
Yury Selivanovdbf10222018-05-28 14:31:28 -0400589receive big amounts of data. Sophisticated protocols implementations
590can allocate the buffer only once at creation time.
Yury Selivanov631fd382018-01-28 16:30:26 -0500591
592The following callbacks are called on :class:`BufferedProtocol`
593instances:
594
Yury Selivanovdbf10222018-05-28 14:31:28 -0400595.. method:: BufferedProtocol.get_buffer(sizehint)
Yury Selivanov631fd382018-01-28 16:30:26 -0500596
Yury Selivanovdbf10222018-05-28 14:31:28 -0400597 Called to allocate a new receive buffer.
598
599 *sizehint* is a recommended minimal size for the returned
600 buffer. It is acceptable to return smaller or bigger buffers
601 than what *sizehint* suggests. When set to -1, the buffer size
602 can be arbitrary. It is an error to return a zero-sized buffer.
603
604 Must return an object that implements the
605 :ref:`buffer protocol <bufferobjects>`.
Yury Selivanov631fd382018-01-28 16:30:26 -0500606
607.. method:: BufferedProtocol.buffer_updated(nbytes)
608
609 Called when the buffer was updated with the received data.
610
611 *nbytes* is the total number of bytes that were written to the buffer.
612
613.. method:: BufferedProtocol.eof_received()
614
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700615 See the documentation of the :meth:`protocol.eof_received()
616 <Protocol.eof_received>` method.
Yury Selivanov631fd382018-01-28 16:30:26 -0500617
618
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700619:meth:`~BufferedProtocol.get_buffer` can be called an arbitrary number
620of times during a connection. However, :meth:`protocol.eof_received()
621<Protocol.eof_received>` is called at most once
622and, if called, :meth:`~BufferedProtocol.get_buffer` and
623:meth:`~BufferedProtocol.buffer_updated` won't be called after it.
Yury Selivanov631fd382018-01-28 16:30:26 -0500624
625State machine:
626
627.. code-block:: none
628
629 start -> connection_made
630 [-> get_buffer
631 [-> buffer_updated]?
632 ]*
633 [-> eof_received]?
634 -> connection_lost -> end
Victor Stinner54a231d2015-01-29 13:33:15 +0100635
636
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700637Datagram Protocols
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100638------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100639
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700640Datagram Protocol instances should be constructed by protocol
641factories passed to the :meth:`loop.create_datagram_endpoint` method.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100642
643.. method:: DatagramProtocol.datagram_received(data, addr)
644
645 Called when a datagram is received. *data* is a bytes object containing
646 the incoming data. *addr* is the address of the peer sending the data;
647 the exact format depends on the transport.
648
649.. method:: DatagramProtocol.error_received(exc)
650
651 Called when a previous send or receive operation raises an
652 :class:`OSError`. *exc* is the :class:`OSError` instance.
653
654 This method is called in rare conditions, when the transport (e.g. UDP)
655 detects that a datagram couldn't be delivered to its recipient.
656 In many conditions though, undeliverable datagrams will be silently
657 dropped.
658
Victor Stinnerea3183f2013-12-03 01:08:00 +0100659.. note::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100660
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700661 On BSD systems (macOS, FreeBSD, etc.) flow control is not supported
662 for datagram protocols, because send failures caused by
663 writing too many packets cannot be detected easily.
664
665 The socket always appears 'ready' and excess packets are dropped; an
666 :class:`OSError` with ``errno`` set to :const:`errno.ENOBUFS` may
667 or may not be raised; if it is raised, it will be reported to
Larry Hastings3732ed22014-03-15 21:13:56 -0700668 :meth:`DatagramProtocol.error_received` but otherwise ignored.
669
Victor Stinnerea3183f2013-12-03 01:08:00 +0100670
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700671.. _asyncio-subprocess-protocols:
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100672
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700673Subprocess Protocols
674--------------------
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100675
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700676Datagram Protocol instances should be constructed by protocol
677factories passed to the :meth:`loop.subprocess_exec` and
678:meth:`loop.subprocess_shell` methods.
679
680.. method:: SubprocessProtocol.pipe_data_received(fd, data)
681
682 Called when the child process writes data into its stdout or stderr
683 pipe.
684
685 *fd* is the integer file descriptor of the pipe.
686
687 *data* is a non-empty bytes object containing the received data.
688
689.. method:: SubprocessProtocol.pipe_connection_lost(fd, exc)
690
691 Called when one of the pipes communicating with the child process
692 is closed.
693
694 *fd* is the integer file descriptor that was closed.
695
696.. method:: SubprocessProtocol.process_exited()
697
698 Called when the child process has exited.
Victor Stinner4b4f9eb2014-01-24 17:33:20 +0100699
700
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700701Examples
702========
Victor Stinnered051592014-10-12 20:18:16 +0200703
704.. _asyncio-tcp-echo-server-protocol:
705
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700706TCP Echo Server
707---------------
Victor Stinnered051592014-10-12 20:18:16 +0200708
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700709TCP echo server using the :meth:`loop.create_server` method, send back
Victor Stinnered051592014-10-12 20:18:16 +0200710received data and close the connection::
Victor Stinnera881a7f2013-12-09 13:19:23 +0100711
712 import asyncio
713
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700714
Victor Stinnercfbea3a2014-10-12 11:30:17 +0200715 class EchoServerClientProtocol(asyncio.Protocol):
Victor Stinnera881a7f2013-12-09 13:19:23 +0100716 def connection_made(self, transport):
717 peername = transport.get_extra_info('peername')
Victor Stinnerc2721b42014-10-12 11:13:40 +0200718 print('Connection from {}'.format(peername))
Victor Stinnera881a7f2013-12-09 13:19:23 +0100719 self.transport = transport
720
721 def data_received(self, data):
Victor Stinnerc2721b42014-10-12 11:13:40 +0200722 message = data.decode()
723 print('Data received: {!r}'.format(message))
724
725 print('Send: {!r}'.format(message))
Victor Stinnera881a7f2013-12-09 13:19:23 +0100726 self.transport.write(data)
727
Victor Stinner53664342014-10-12 11:35:09 +0200728 print('Close the client socket')
Victor Stinnera881a7f2013-12-09 13:19:23 +0100729 self.transport.close()
730
Victor Stinnera881a7f2013-12-09 13:19:23 +0100731
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700732 async def main():
733 # Get a reference to the event loop as we plan to use
734 # low-level APIs.
735 loop = asyncio.get_running_loop()
Victor Stinnerc2721b42014-10-12 11:13:40 +0200736
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700737 server = await loop.create_server(
738 lambda: EchoServerClientProtocol(),
739 '127.0.0.1', 8888)
Victor Stinnera881a7f2013-12-09 13:19:23 +0100740
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700741 async with server:
742 await server.serve_forever()
743
744
745 asyncio.run(main())
746
Victor Stinnera881a7f2013-12-09 13:19:23 +0100747
Victor Stinnered051592014-10-12 20:18:16 +0200748.. seealso::
749
750 The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>`
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700751 example uses the high-level :func:`asyncio.start_server` function.
752
753.. _asyncio-tcp-echo-client-protocol:
754
755TCP Echo Client
756---------------
757
758TCP echo client using the :meth:`loop.create_connection` method, send
759data and wait until the connection is closed::
760
761 import asyncio
762
763
764 class EchoClientProtocol(asyncio.Protocol):
765 def __init__(self, message, on_con_lost, loop):
766 self.message = message
767 self.loop = loop
768 self.on_con_lost = on_con_lost
769
770 def connection_made(self, transport):
771 transport.write(self.message.encode())
772 print('Data sent: {!r}'.format(self.message))
773
774 def data_received(self, data):
775 print('Data received: {!r}'.format(data.decode()))
776
777 def connection_lost(self, exc):
778 print('The server closed the connection')
779 self.on_con_lost.set_result(True)
780
781
782 async def main():
783 # Get a reference to the event loop as we plan to use
784 # low-level APIs.
785 loop = asyncio.get_running_loop()
786
787 on_con_lost = loop.create_future()
788 message = 'Hello World!'
789
790 transport, protocol = await loop.create_connection(
791 lambda: EchoClientProtocol(message, on_con_lost, loop),
792 '127.0.0.1', 8888)
793
794 # Wait until the protocol signals that the connection
795 # is lost and close the transport.
796 try:
797 await on_con_lost
798 finally:
799 transport.close()
800
801
802 asyncio.run(main())
803
804
805.. seealso::
806
807 The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>`
808 example uses the high-level :func:`asyncio.open_connection` function.
809
810
811.. _asyncio-udp-echo-server-protocol:
812
813UDP Echo Server
814---------------
815
816UDP echo server using the :meth:`loop.create_datagram_endpoint`
817method, send back received data::
818
819 import asyncio
820
821
822 class EchoServerProtocol:
823 def connection_made(self, transport):
824 self.transport = transport
825
826 def datagram_received(self, data, addr):
827 message = data.decode()
828 print('Received %r from %s' % (message, addr))
829 print('Send %r to %s' % (message, addr))
830 self.transport.sendto(data, addr)
831
832
833 async def main():
834 print("Starting UDP server")
835
836 # Get a reference to the event loop as we plan to use
837 # low-level APIs.
838 loop = asyncio.get_running_loop()
839
840 # One protocol instance will be created to serve all
841 # client requests.
842 transport, protocol = await loop.create_datagram_endpoint(
843 lambda: EchoServerProtocol(),
844 local_addr=('127.0.0.1', 9999))
845
846 try:
847 await asyncio.sleep(3600) # Serve for 1 hour.
848 finally:
849 transport.close()
850
851
852 asyncio.run(main())
Victor Stinnered051592014-10-12 20:18:16 +0200853
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200854
855.. _asyncio-udp-echo-client-protocol:
856
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700857UDP Echo Client
858---------------
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200859
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700860UDP echo client using the :meth:`loop.create_datagram_endpoint`
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200861method, send data and close the transport when we received the answer::
862
863 import asyncio
864
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700865
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200866 class EchoClientProtocol:
867 def __init__(self, message, loop):
868 self.message = message
869 self.loop = loop
870 self.transport = None
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700871 self.on_con_lost = loop.create_future()
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200872
873 def connection_made(self, transport):
874 self.transport = transport
875 print('Send:', self.message)
876 self.transport.sendto(self.message.encode())
877
878 def datagram_received(self, data, addr):
879 print("Received:", data.decode())
880
881 print("Close the socket")
882 self.transport.close()
883
884 def error_received(self, exc):
885 print('Error received:', exc)
886
887 def connection_lost(self, exc):
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700888 print("Connection closed")
889 self.on_con_lost.set_result(True)
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200890
891
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700892 async def main():
893 # Get a reference to the event loop as we plan to use
894 # low-level APIs.
895 loop = asyncio.get_running_loop()
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200896
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700897 message = "Hello World!"
898 transport, protocol = await loop.create_datagram_endpoint(
899 lambda: EchoClientProtocol(message, loop),
900 remote_addr=('127.0.0.1', 9999))
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200901
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700902 try:
903 await protocol.on_con_lost
904 finally:
905 transport.close()
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200906
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200907
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700908 asyncio.run(main())
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200909
910
Victor Stinner04e6df32014-10-11 16:16:27 +0200911.. _asyncio-register-socket:
Victor Stinnera881a7f2013-12-09 13:19:23 +0100912
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700913Connecting Existing Sockets
914---------------------------
Victor Stinner04e6df32014-10-11 16:16:27 +0200915
916Wait until a socket receives data using the
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700917:meth:`loop.create_connection` method with a protocol::
Victor Stinner04e6df32014-10-11 16:16:27 +0200918
919 import asyncio
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700920 import socket
Victor Stinner04e6df32014-10-11 16:16:27 +0200921
Victor Stinner04e6df32014-10-11 16:16:27 +0200922
923 class MyProtocol(asyncio.Protocol):
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700924
925 def __init__(self, loop):
926 self.transport = None
927 self.on_con_lost = loop.create_future()
Victor Stinner04e6df32014-10-11 16:16:27 +0200928
929 def connection_made(self, transport):
930 self.transport = transport
931
932 def data_received(self, data):
933 print("Received:", data.decode())
934
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700935 # We are done: close the transport;
936 # connection_lost() will be called automatically.
Victor Stinner04e6df32014-10-11 16:16:27 +0200937 self.transport.close()
938
939 def connection_lost(self, exc):
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700940 # The socket has been closed
941 self.on_con_lost.set_result(True)
Victor Stinner04e6df32014-10-11 16:16:27 +0200942
Victor Stinner04e6df32014-10-11 16:16:27 +0200943
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700944 async def main():
945 # Get a reference to the event loop as we plan to use
946 # low-level APIs.
947 loop = asyncio.get_running_loop()
Victor Stinner04e6df32014-10-11 16:16:27 +0200948
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700949 # Create a pair of connected sockets
950 rsock, wsock = socket.socketpair()
Victor Stinner04e6df32014-10-11 16:16:27 +0200951
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700952 # Register the socket to wait for data.
953 transport, protocol = await loop.create_connection(
954 lambda: MyProtocol(loop), sock=rsock)
955
956 # Simulate the reception of data from the network.
957 loop.call_soon(wsock.send, 'abc'.encode())
958
959 try:
960 await protocol.on_con_lost
961 finally:
962 transport.close()
963 wsock.close()
964
965 asyncio.run(main())
Victor Stinner04e6df32014-10-11 16:16:27 +0200966
967.. seealso::
968
969 The :ref:`watch a file descriptor for read events
970 <asyncio-watch-read-event>` example uses the low-level
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700971 :meth:`loop.add_reader` method to register an FD.
Victor Stinner04e6df32014-10-11 16:16:27 +0200972
973 The :ref:`register an open socket to wait for data using streams
974 <asyncio-register-socket-streams>` example uses high-level streams
975 created by the :func:`open_connection` function in a coroutine.
Yury Selivanov7c7605f2018-09-11 09:54:40 -0700976
977.. _asyncio-subprocess-proto-example:
978
979loop.subprocess_exec() and SubprocessProtocol
980---------------------------------------------
981
982An example of a subprocess protocol using to get the output of a
983subprocess and to wait for the subprocess exit.
984
985The subprocess is created by th :meth:`loop.subprocess_exec` method::
986
987 import asyncio
988 import sys
989
990 class DateProtocol(asyncio.SubprocessProtocol):
991 def __init__(self, exit_future):
992 self.exit_future = exit_future
993 self.output = bytearray()
994
995 def pipe_data_received(self, fd, data):
996 self.output.extend(data)
997
998 def process_exited(self):
999 self.exit_future.set_result(True)
1000
1001 async def get_date():
1002 # Get a reference to the event loop as we plan to use
1003 # low-level APIs.
1004 loop = asyncio.get_running_loop()
1005
1006 code = 'import datetime; print(datetime.datetime.now())'
1007 exit_future = asyncio.Future(loop=loop)
1008
1009 # Create the subprocess controlled by DateProtocol;
1010 # redirect the standard output into a pipe.
1011 transport, protocol = await loop.subprocess_exec(
1012 lambda: DateProtocol(exit_future),
1013 sys.executable, '-c', code,
1014 stdin=None, stderr=None)
1015
1016 # Wait for the subprocess exit using the process_exited()
1017 # method of the protocol.
1018 await exit_future
1019
1020 # Close the stdout pipe.
1021 transport.close()
1022
1023 # Read the output which was collected by the
1024 # pipe_data_received() method of the protocol.
1025 data = bytes(protocol.output)
1026 return data.decode('ascii').rstrip()
1027
1028 if sys.platform == "win32":
1029 asyncio.set_event_loop_policy(
1030 asyncio.WindowsProactorEventLoopPolicy())
1031
1032 date = asyncio.run(get_date())
1033 print(f"Current date: {date}")