| 
									
										
										
										
											2013-12-20 14:37:39 -05:00
										 |  |  | .. currentmodule:: asyncio
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-25 17:03:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  | .. _asyncio-transports-protocols:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | ========================
 | 
					
						
							|  |  |  | Transports and Protocols
 | 
					
						
							|  |  |  | ========================
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. rubric:: Preface
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  | Transports and Protocols are used by the **low-level** event loop
 | 
					
						
							|  |  |  | APIs such as :meth:`loop.create_connection`.  They use
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | callback-based programming style and enable high-performance
 | 
					
						
							|  |  |  | implementations of network or IPC protocols (e.g. HTTP).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Essentially, transports and protocols should only be used in
 | 
					
						
							|  |  |  | libraries and frameworks and never in high-level asyncio
 | 
					
						
							|  |  |  | applications.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This documentation page covers both `Transports`_ and `Protocols`_.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. rubric:: Introduction
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | At the highest level, the transport is concerned with *how* bytes
 | 
					
						
							|  |  |  | are transmitted, while the protocol determines *which* bytes to
 | 
					
						
							|  |  |  | transmit (and to some extent when).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A different way of saying the same thing: a transport is an
 | 
					
						
							|  |  |  | abstraction for a socket (or similar I/O endpoint) while a protocol
 | 
					
						
							|  |  |  | is an abstraction for an application, from the transport's point
 | 
					
						
							|  |  |  | of view.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | Yet another view is the transport and protocol interfaces
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | together define an abstract interface for using network I/O and
 | 
					
						
							|  |  |  | interprocess I/O.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There is always a 1:1 relationship between transport and protocol
 | 
					
						
							|  |  |  | objects: the protocol calls transport methods to send data,
 | 
					
						
							|  |  |  | while the transport calls protocol methods to pass it data that
 | 
					
						
							|  |  |  | has been received.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Most of connection oriented event loop methods
 | 
					
						
							|  |  |  | (such as :meth:`loop.create_connection`) usually accept a
 | 
					
						
							|  |  |  | *protocol_factory* argument used to create a *Protocol* object
 | 
					
						
							|  |  |  | for an accepted connection, represented by a *Transport* object.
 | 
					
						
							|  |  |  | Such methods usually return a tuple of ``(transport, protocol)``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. rubric:: Contents
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This documentation page contains the following sections:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * The `Transports`_ section documents asyncio :class:`BaseTransport`,
 | 
					
						
							|  |  |  |   :class:`ReadTransport`, :class:`WriteTransport`, :class:`Transport`,
 | 
					
						
							|  |  |  |   :class:`DatagramTransport`, and :class:`SubprocessTransport`
 | 
					
						
							|  |  |  |   classes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * The `Protocols`_ section documents asyncio :class:`BaseProtocol`,
 | 
					
						
							|  |  |  |   :class:`Protocol`, :class:`BufferedProtocol`,
 | 
					
						
							|  |  |  |   :class:`DatagramProtocol`, and :class:`SubprocessProtocol` classes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * The `Examples`_ section showcases how to work with transports,
 | 
					
						
							|  |  |  |   protocols, and low-level event loop APIs.
 | 
					
						
							| 
									
										
										
										
											2017-07-25 17:03:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:49:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 15:03:02 +01:00
										 |  |  | .. _asyncio-transport:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Transports
 | 
					
						
							|  |  |  | ==========
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-29 21:14:04 -07:00
										 |  |  | Transports are classes provided by :mod:`asyncio` in order to abstract
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | various kinds of communication channels.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Transport objects are always instantiated by an
 | 
					
						
							|  |  |  | ref:`asyncio event loop <asyncio-event-loop>`.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | asyncio implements transports for TCP, UDP, SSL, and subprocess pipes.
 | 
					
						
							|  |  |  | The methods available on a transport depend on the transport's kind.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 14:24:15 +01:00
										 |  |  | The transport classes are :ref:`not thread safe <asyncio-multithreading>`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-12 16:44:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Transports Hierarchy
 | 
					
						
							|  |  |  | --------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. class:: BaseTransport
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Base class for all transports.  Contains methods that all
 | 
					
						
							|  |  |  |    asyncio transports share.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: WriteTransport(BaseTransport)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    A base transport for write-only connections.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Instances of the *WriteTransport* class are returned from
 | 
					
						
							|  |  |  |    the :meth:`loop.connect_write_pipe` event loop method and
 | 
					
						
							|  |  |  |    are also used by subprocess-related methods like
 | 
					
						
							|  |  |  |    :meth:`loop.subprocess_exec`.
 | 
					
						
							| 
									
										
										
										
											2015-11-16 12:46:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: ReadTransport(BaseTransport)
 | 
					
						
							| 
									
										
										
										
											2015-11-16 12:46:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    A base transport for read-only connections.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Instances of the *ReadTransport* class are returned from
 | 
					
						
							|  |  |  |    the :meth:`loop.connect_read_pipe` event loop method and
 | 
					
						
							|  |  |  |    are also used by subprocess-related methods like
 | 
					
						
							|  |  |  |    :meth:`loop.subprocess_exec`.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: Transport(WriteTransport, ReadTransport)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Interface representing a bidirectional transport, such as a
 | 
					
						
							|  |  |  |    TCP connection.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    The user does not instantiate a transport directly; they call a
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    utility function, passing it a protocol factory and other
 | 
					
						
							|  |  |  |    information necessary to create the transport and protocol.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Instances of the *Transport* class are returned from or used by
 | 
					
						
							|  |  |  |    event loop methods like :meth:`loop.create_connection`,
 | 
					
						
							|  |  |  |    :meth:`loop.create_unix_connection`,
 | 
					
						
							|  |  |  |    :meth:`loop.create_server`, :meth:`loop.sendfile`, etc.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: DatagramTransport(BaseTransport)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    A transport for datagram (UDP) connections.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Instances of the *DatagramTransport* class are returned from
 | 
					
						
							|  |  |  |    the :meth:`loop.create_datagram_endpoint` event loop method.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: SubprocessTransport(BaseTransport)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    An abstraction to represent a connection between a parent and its
 | 
					
						
							|  |  |  |    child OS process.
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:33:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Instances of the *SubprocessTransport* class are returned from
 | 
					
						
							|  |  |  |    event loop methods :meth:`loop.subprocess_shell` and
 | 
					
						
							|  |  |  |    :meth:`loop.subprocess_exec`.
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:33:47 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Base Transport
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:33:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseTransport.close()
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:33:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Close the transport.
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:33:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    If the transport has a buffer for outgoing
 | 
					
						
							|  |  |  |    data, buffered data will be flushed asynchronously.  No more data
 | 
					
						
							|  |  |  |    will be received.  After all buffered data is flushed, the
 | 
					
						
							|  |  |  |    protocol's :meth:`protocol.connection_lost()
 | 
					
						
							|  |  |  |    <BaseProtocol.connection_lost>` method will be called with
 | 
					
						
							|  |  |  |    :const:`None` as its argument.
 | 
					
						
							| 
									
										
										
										
											2015-09-21 18:06:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseTransport.is_closing()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return ``True`` if the transport is closing or is closed.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseTransport.get_extra_info(name, default=None)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return information about the transport or underlying resources
 | 
					
						
							|  |  |  |    it uses.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    *name* is a string representing the piece of transport-specific
 | 
					
						
							|  |  |  |    information to get.
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    *default* is the value to return if the information is not
 | 
					
						
							|  |  |  |    available, or if the transport does not support querying it
 | 
					
						
							|  |  |  |    with the given third-party event loop implementation or on the
 | 
					
						
							|  |  |  |    current platform.
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    For example, the following code attempts to get the underlying
 | 
					
						
							|  |  |  |    socket object of the transport::
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |       sock = transport.get_extra_info('socket')
 | 
					
						
							|  |  |  |       if sock is not None:
 | 
					
						
							|  |  |  |           print(sock.getsockopt(...))
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Categories of information that can be queried on some transports:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    * socket:
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'peername'``: the remote address to which the socket is
 | 
					
						
							|  |  |  |        connected, result of :meth:`socket.socket.getpeername`
 | 
					
						
							|  |  |  |        (``None`` on error)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'socket'``: :class:`socket.socket` instance
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'sockname'``: the socket's own address,
 | 
					
						
							|  |  |  |        result of :meth:`socket.socket.getsockname`
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    * SSL socket:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'compression'``: the compression algorithm being used as a
 | 
					
						
							|  |  |  |        string, or ``None`` if the connection isn't compressed; result
 | 
					
						
							|  |  |  |        of :meth:`ssl.SSLSocket.compression`
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'cipher'``: a three-value tuple containing the name of the
 | 
					
						
							|  |  |  |        cipher being used, the version of the SSL protocol that defines
 | 
					
						
							|  |  |  |        its use, and the number of secret bits being used; result of
 | 
					
						
							|  |  |  |        :meth:`ssl.SSLSocket.cipher`
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'peercert'``: peer certificate; result of
 | 
					
						
							|  |  |  |        :meth:`ssl.SSLSocket.getpeercert`
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'sslcontext'``: :class:`ssl.SSLContext` instance
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'ssl_object'``: :class:`ssl.SSLObject` or
 | 
					
						
							|  |  |  |        :class:`ssl.SSLSocket` instance
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    * pipe:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'pipe'``: pipe object
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    * subprocess:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |      - ``'subprocess'``: :class:`subprocess.Popen` instance
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseTransport.set_protocol(protocol)
 | 
					
						
							| 
									
										
										
										
											2014-08-26 00:22:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Set a new protocol.
 | 
					
						
							| 
									
										
										
										
											2014-08-26 00:22:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Switching protocol should only be done when both
 | 
					
						
							|  |  |  |    protocols are documented to support the switch.
 | 
					
						
							| 
									
										
										
										
											2014-08-26 00:22:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseTransport.get_protocol()
 | 
					
						
							| 
									
										
										
										
											2014-08-26 00:22:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return the current protocol.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Read-only Transports
 | 
					
						
							|  |  |  | --------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: ReadTransport.is_reading()
 | 
					
						
							| 
									
										
										
										
											2017-06-20 14:32:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return ``True`` if the transport is receiving new data.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    .. versionadded:: 3.7
 | 
					
						
							| 
									
										
										
										
											2014-08-26 00:22:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: ReadTransport.pause_reading()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Pause the receiving end of the transport.  No data will be passed to
 | 
					
						
							|  |  |  |    the protocol's :meth:`protocol.data_received() <Protocol.data_received>`
 | 
					
						
							|  |  |  |    method until :meth:`resume_reading` is called.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    .. versionchanged:: 3.7
 | 
					
						
							|  |  |  |       The method is idempotent, i.e. it can be called when the
 | 
					
						
							|  |  |  |       transport is already paused or closed.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: ReadTransport.resume_reading()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Resume the receiving end.  The protocol's
 | 
					
						
							|  |  |  |    :meth:`protocol.data_received() <Protocol.data_received>` method
 | 
					
						
							|  |  |  |    will be called once again if some data is available for reading.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    .. versionchanged:: 3.7
 | 
					
						
							|  |  |  |       The method is idempotent, i.e. it can be called when the
 | 
					
						
							|  |  |  |       transport is already reading.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Write-only Transports
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: WriteTransport.abort()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Close the transport immediately, without waiting for pending operations
 | 
					
						
							|  |  |  |    to complete.  Buffered data will be lost.  No more data will be received.
 | 
					
						
							|  |  |  |    The protocol's :meth:`protocol.connection_lost()
 | 
					
						
							|  |  |  |    <BaseProtocol.connection_lost>` method will eventually be
 | 
					
						
							|  |  |  |    called with :const:`None` as its argument.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: WriteTransport.can_write_eof()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return :const:`True` if the transport supports
 | 
					
						
							|  |  |  |    :meth:`~WriteTransport.write_eof`, :const:`False` if not.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: WriteTransport.get_write_buffer_size()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return the current size of the output buffer used by the transport.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: WriteTransport.get_write_buffer_limits()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Get the *high* and *low* watermarks for write flow control. Return a
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    tuple ``(low, high)`` where *low* and *high* are positive number of
 | 
					
						
							|  |  |  |    bytes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Use :meth:`set_write_buffer_limits` to set the limits.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 3.4.2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: WriteTransport.set_write_buffer_limits(high=None, low=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Set the *high* and *low* watermarks for write flow control.
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |    These two values (measured in number of
 | 
					
						
							|  |  |  |    bytes) control when the protocol's
 | 
					
						
							|  |  |  |    :meth:`protocol.pause_writing() <BaseProtocol.pause_writing>`
 | 
					
						
							|  |  |  |    and :meth:`protocol.resume_writing() <BaseProtocol.resume_writing>`
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    methods are called. If specified, the low watermark must be less
 | 
					
						
							|  |  |  |    than or equal to the high watermark.  Neither *high* nor *low*
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    can be negative.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :meth:`~BaseProtocol.pause_writing` is called when the buffer size
 | 
					
						
							|  |  |  |    becomes greater than or equal to the *high* value. If writing has
 | 
					
						
							|  |  |  |    been paused, :meth:`~BaseProtocol.resume_writing` is called when
 | 
					
						
							|  |  |  |    the buffer size becomes less than or equal to the *low* value.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The defaults are implementation-specific.  If only the
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    high watermark is given, the low watermark defaults to an
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    implementation-specific value less than or equal to the
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    high watermark.  Setting *high* to zero forces *low* to zero as
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    well, and causes :meth:`~BaseProtocol.pause_writing` to be called
 | 
					
						
							|  |  |  |    whenever the buffer becomes non-empty.  Setting *low* to zero causes
 | 
					
						
							|  |  |  |    :meth:`~BaseProtocol.resume_writing` to be called only once the
 | 
					
						
							|  |  |  |    buffer is empty. Use of zero for either limit is generally
 | 
					
						
							|  |  |  |    sub-optimal as it reduces opportunities for doing I/O and
 | 
					
						
							|  |  |  |    computation concurrently.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Use :meth:`~WriteTransport.get_write_buffer_limits`
 | 
					
						
							|  |  |  |    to get the limits.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: WriteTransport.write(data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Write some *data* bytes to the transport.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This method does not block; it buffers the data and arranges for it
 | 
					
						
							|  |  |  |    to be sent out asynchronously.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: WriteTransport.writelines(list_of_data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Write a list (or any iterable) of data bytes to the transport.
 | 
					
						
							|  |  |  |    This is functionally equivalent to calling :meth:`write` on each
 | 
					
						
							|  |  |  |    element yielded by the iterable, but may be implemented more
 | 
					
						
							|  |  |  |    efficiently.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: WriteTransport.write_eof()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Close the write end of the transport after flushing all buffered data.
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Data may still be received.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This method can raise :exc:`NotImplementedError` if the transport
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    (e.g. SSL) doesn't support half-closed connections.
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Datagram Transports
 | 
					
						
							|  |  |  | -------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: DatagramTransport.sendto(data, addr=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Send the *data* bytes to the remote peer given by *addr* (a
 | 
					
						
							|  |  |  |    transport-dependent target address).  If *addr* is :const:`None`,
 | 
					
						
							|  |  |  |    the data is sent to the target address given on transport
 | 
					
						
							|  |  |  |    creation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This method does not block; it buffers the data and arranges
 | 
					
						
							|  |  |  |    for it to be sent out asynchronously.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | .. method:: DatagramTransport.abort()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Close the transport immediately, without waiting for pending
 | 
					
						
							|  |  |  |    operations to complete.  Buffered data will be lost.
 | 
					
						
							|  |  |  |    No more data will be received.  The protocol's
 | 
					
						
							|  |  |  |    :meth:`protocol.connection_lost() <BaseProtocol.connection_lost>`
 | 
					
						
							|  |  |  |    method will eventually be called with :const:`None` as its argument.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. _asyncio-subprocess-transports:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Subprocess Transports
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.get_pid()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return the subprocess process id as an integer.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.get_pipe_transport(fd)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return the transport for the communication pipe corresponding to the
 | 
					
						
							|  |  |  |    integer file descriptor *fd*:
 | 
					
						
							| 
									
										
										
										
											2014-10-13 23:56:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    * ``0``: readable streaming transport of the standard input (*stdin*),
 | 
					
						
							|  |  |  |      or :const:`None` if the subprocess was not created with ``stdin=PIPE``
 | 
					
						
							|  |  |  |    * ``1``: writable streaming transport of the standard output (*stdout*),
 | 
					
						
							|  |  |  |      or :const:`None` if the subprocess was not created with ``stdout=PIPE``
 | 
					
						
							|  |  |  |    * ``2``: writable streaming transport of the standard error (*stderr*),
 | 
					
						
							|  |  |  |      or :const:`None` if the subprocess was not created with ``stderr=PIPE``
 | 
					
						
							|  |  |  |    * other *fd*: :const:`None`
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.get_returncode()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Return the subprocess return code as an integer or :const:`None`
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    if it hasn't returned, which is similar to the
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    :attr:`subprocess.Popen.returncode` attribute.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.kill()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  |    Kill the subprocess.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    On POSIX systems, the function sends SIGKILL to the subprocess.
 | 
					
						
							|  |  |  |    On Windows, this method is an alias for :meth:`terminate`.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  |    See also :meth:`subprocess.Popen.kill`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.send_signal(signal)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Send the *signal* number to the subprocess, as in
 | 
					
						
							|  |  |  |    :meth:`subprocess.Popen.send_signal`.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.terminate()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  |    Stop the subprocess.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    On POSIX systems, this method sends SIGTERM to the subprocess.
 | 
					
						
							|  |  |  |    On Windows, the Windows API function TerminateProcess() is called to
 | 
					
						
							|  |  |  |    stop the subprocess.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  |    See also :meth:`subprocess.Popen.terminate`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessTransport.close()
 | 
					
						
							| 
									
										
										
										
											2014-10-13 23:56:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 13:32:07 -07:00
										 |  |  |    Kill the subprocess by calling the :meth:`kill` method.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If the subprocess hasn't returned yet, and close transports of
 | 
					
						
							|  |  |  |    *stdin*, *stdout*, and *stderr* pipes.
 | 
					
						
							| 
									
										
										
										
											2014-10-13 23:56:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 15:03:02 +01:00
										 |  |  | .. _asyncio-protocol:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Protocols
 | 
					
						
							|  |  |  | =========
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | asyncio provides a set of abstract base classes that should be used
 | 
					
						
							|  |  |  | to implement network protocols.  Those classes are meant to be used
 | 
					
						
							|  |  |  | together with :ref:`transports <asyncio-transport>`.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | Subclasses of abstract base protocol classes may implement some or
 | 
					
						
							|  |  |  | all methods.  All these methods are callbacks: they are called by
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | transports on certain events, for example when some data is received.
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | A base protocol method should be called by the corresponding transport.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Base Protocols
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: BaseProtocol
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Base protocol with methods that all protocols share.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: Protocol(BaseProtocol)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    The base class for implementing streaming protocols
 | 
					
						
							|  |  |  |    (TCP, Unix sockets, etc).
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: BufferedProtocol(BaseProtocol)
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |    A base class for implementing streaming protocols with manual
 | 
					
						
							|  |  |  |    control of the receive buffer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: DatagramProtocol(BaseProtocol)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    The base class for implementing datagram (UDP) protocols.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. class:: SubprocessProtocol(BaseProtocol)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The base class for implementing protocols communicating with child
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    processes (unidirectional pipes).
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Base Protocol
 | 
					
						
							|  |  |  | -------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | All asyncio protocols can implement Base Protocol callbacks.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. rubric:: Connection Callbacks
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Connection callbacks are called on all protocols, exactly once per
 | 
					
						
							|  |  |  | a successful connection.  All other protocol callbacks can only be
 | 
					
						
							|  |  |  | called between those two methods.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: BaseProtocol.connection_made(transport)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when a connection is made.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The *transport* argument is the transport representing the
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    connection.  The protocol is responsible for storing the reference
 | 
					
						
							|  |  |  |    to its transport.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: BaseProtocol.connection_lost(exc)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when the connection is lost or closed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The argument is either an exception object or :const:`None`.
 | 
					
						
							|  |  |  |    The latter means a regular EOF is received, or the connection was
 | 
					
						
							|  |  |  |    aborted or closed by this side of the connection.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. rubric:: Flow Control Callbacks
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Flow control callbacks can be called by transports to pause or
 | 
					
						
							|  |  |  | resume writing performed by the protocol.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | See the documentation of the :meth:`~WriteTransport.set_write_buffer_limits`
 | 
					
						
							|  |  |  | method for more details.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseProtocol.pause_writing()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Called when the transport's buffer goes over the high watermark.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: BaseProtocol.resume_writing()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Called when the transport's buffer drains below the low watermark.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  | If the buffer size equals the high watermark,
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | :meth:`~BaseProtocol.pause_writing` is not called: the buffer size must
 | 
					
						
							|  |  |  | go strictly over.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Conversely, :meth:`~BaseProtocol.resume_writing` is called when the
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  | buffer size is equal or lower than the low watermark.  These end
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | conditions are important to ensure that things go as expected when
 | 
					
						
							|  |  |  | either mark is zero.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Streaming Protocols
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | -------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Event methods, such as :meth:`loop.create_server`,
 | 
					
						
							|  |  |  | :meth:`loop.create_unix_server`, :meth:`loop.create_connection`,
 | 
					
						
							|  |  |  | :meth:`loop.create_unix_connection`, :meth:`loop.connect_accepted_socket`,
 | 
					
						
							|  |  |  | :meth:`loop.connect_read_pipe`, and :meth:`loop.connect_write_pipe`
 | 
					
						
							|  |  |  | accept factories that return streaming protocols.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: Protocol.data_received(data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Called when some data is received.  *data* is a non-empty bytes
 | 
					
						
							|  |  |  |    object containing the incoming data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Whether the data is buffered, chunked or reassembled depends on
 | 
					
						
							|  |  |  |    the transport.  In general, you shouldn't rely on specific semantics
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    and instead make your parsing generic and flexible. However,
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    data is always received in the correct order.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    The method can be called an arbitrary number of times while
 | 
					
						
							|  |  |  |    a connection is open.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    However, :meth:`protocol.eof_received() <Protocol.eof_received>`
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    is called at most once.  Once `eof_received()` is called,
 | 
					
						
							|  |  |  |    ``data_received()`` is not called anymore.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: Protocol.eof_received()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 14:18:14 -04:00
										 |  |  |    Called when the other end signals it won't send any more data
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    (for example by calling :meth:`transport.write_eof()
 | 
					
						
							|  |  |  |    <WriteTransport.write_eof>`, if the other end also uses
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  |    asyncio).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 16:29:26 +03:00
										 |  |  |    This method may return a false value (including ``None``), in which case
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  |    the transport will close itself.  Conversely, if this method returns a
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    true value, the protocol used determines whether to close the transport.
 | 
					
						
							|  |  |  |    Since the default implementation returns ``None``, it implicitly closes the
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    connection.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    Some transports, including SSL, don't support half-closed connections,
 | 
					
						
							|  |  |  |    in which case returning true from this method will result in the connection
 | 
					
						
							|  |  |  |    being closed.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 13:33:15 +01:00
										 |  |  | State machine:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | .. code-block:: none
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     start -> connection_made
 | 
					
						
							|  |  |  |         [-> data_received]*
 | 
					
						
							|  |  |  |         [-> eof_received]?
 | 
					
						
							|  |  |  |     -> connection_lost -> end
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Buffered Streaming Protocols
 | 
					
						
							|  |  |  | ----------------------------
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 3.7
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    **Important:** this has been added to asyncio in Python 3.7
 | 
					
						
							|  |  |  |    *on a provisional basis*!  This is as an experimental API that
 | 
					
						
							|  |  |  |    might be changed or removed completely in Python 3.8.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Buffered Protocols can be used with any event loop method
 | 
					
						
							|  |  |  | that supports `Streaming Protocols`_.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  | ``BufferedProtocol`` implementations allow explicit manual allocation
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | and control of the receive buffer.  Event loops can then use the buffer
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | provided by the protocol to avoid unnecessary data copies.  This
 | 
					
						
							|  |  |  | can result in noticeable performance improvement for protocols that
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  | receive big amounts of data.  Sophisticated protocol implementations
 | 
					
						
							|  |  |  | can significantly reduce the number of buffer allocations.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | The following callbacks are called on :class:`BufferedProtocol`
 | 
					
						
							|  |  |  | instances:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  | .. method:: BufferedProtocol.get_buffer(sizehint)
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |    Called to allocate a new receive buffer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    *sizehint* is the recommended minimum size for the returned
 | 
					
						
							|  |  |  |    buffer.  It is acceptable to return smaller or larger buffers
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |    than what *sizehint* suggests.  When set to -1, the buffer size
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    can be arbitrary. It is an error to return a buffer with a zero size.
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    ``get_buffer()`` must return an object implementing the
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |    :ref:`buffer protocol <bufferobjects>`.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: BufferedProtocol.buffer_updated(nbytes)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when the buffer was updated with the received data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    *nbytes* is the total number of bytes that were written to the buffer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: BufferedProtocol.eof_received()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    See the documentation of the :meth:`protocol.eof_received()
 | 
					
						
							|  |  |  |    <Protocol.eof_received>` method.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | :meth:`~BufferedProtocol.get_buffer` can be called an arbitrary number
 | 
					
						
							|  |  |  | of times during a connection.  However, :meth:`protocol.eof_received()
 | 
					
						
							|  |  |  | <Protocol.eof_received>` is called at most once
 | 
					
						
							|  |  |  | and, if called, :meth:`~BufferedProtocol.get_buffer` and
 | 
					
						
							|  |  |  | :meth:`~BufferedProtocol.buffer_updated` won't be called after it.
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | State machine:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code-block:: none
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     start -> connection_made
 | 
					
						
							|  |  |  |         [-> get_buffer
 | 
					
						
							|  |  |  |             [-> buffer_updated]?
 | 
					
						
							|  |  |  |         ]*
 | 
					
						
							|  |  |  |         [-> eof_received]?
 | 
					
						
							|  |  |  |     -> connection_lost -> end
 | 
					
						
							| 
									
										
										
										
											2015-01-29 13:33:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Datagram Protocols
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | ------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Datagram Protocol instances should be constructed by protocol
 | 
					
						
							|  |  |  | factories passed to the :meth:`loop.create_datagram_endpoint` method.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: DatagramProtocol.datagram_received(data, addr)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when a datagram is received.  *data* is a bytes object containing
 | 
					
						
							|  |  |  |    the incoming data.  *addr* is the address of the peer sending the data;
 | 
					
						
							|  |  |  |    the exact format depends on the transport.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: DatagramProtocol.error_received(exc)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when a previous send or receive operation raises an
 | 
					
						
							|  |  |  |    :class:`OSError`.  *exc* is the :class:`OSError` instance.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This method is called in rare conditions, when the transport (e.g. UDP)
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    detects that a datagram could not be delivered to its recipient.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  |    In many conditions though, undeliverable datagrams will be silently
 | 
					
						
							|  |  |  |    dropped.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. note::
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    On BSD systems (macOS, FreeBSD, etc.) flow control is not supported
 | 
					
						
							| 
									
										
										
										
											2018-09-17 19:16:44 -04:00
										 |  |  |    for datagram protocols, because there is no reliable way to detect send
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    failures caused by writing too many packets.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  |    The socket always appears 'ready' and excess packets are dropped. An
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    :class:`OSError` with ``errno`` set to :const:`errno.ENOBUFS` may
 | 
					
						
							|  |  |  |    or may not be raised; if it is raised, it will be reported to
 | 
					
						
							|  |  |  |    :meth:`DatagramProtocol.error_received` but otherwise ignored.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. _asyncio-subprocess-protocols:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Subprocess Protocols
 | 
					
						
							|  |  |  | --------------------
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Datagram Protocol instances should be constructed by protocol
 | 
					
						
							|  |  |  | factories passed to the :meth:`loop.subprocess_exec` and
 | 
					
						
							|  |  |  | :meth:`loop.subprocess_shell` methods.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. method:: SubprocessProtocol.pipe_data_received(fd, data)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    Called when the child process writes data into its stdout or stderr
 | 
					
						
							|  |  |  |    pipe.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    *fd* is the integer file descriptor of the pipe.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    *data* is a non-empty bytes object containing the received data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: SubprocessProtocol.pipe_connection_lost(fd, exc)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when one of the pipes communicating with the child process
 | 
					
						
							|  |  |  |    is closed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    *fd* is the integer file descriptor that was closed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: SubprocessProtocol.process_exited()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Called when the child process has exited.
 | 
					
						
							| 
									
										
										
										
											2014-03-15 21:13:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Examples
 | 
					
						
							|  |  |  | ========
 | 
					
						
							| 
									
										
										
										
											2014-01-24 17:33:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  | .. _asyncio_example_tcp_echo_server_protocol:
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | TCP Echo Server
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | Create a TCP echo server using the :meth:`loop.create_server` method, send back
 | 
					
						
							|  |  |  | received data, and close the connection::
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 14:39:16 -07:00
										 |  |  |     class EchoServerProtocol(asyncio.Protocol):
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         def connection_made(self, transport):
 | 
					
						
							|  |  |  |             peername = transport.get_extra_info('peername')
 | 
					
						
							|  |  |  |             print('Connection from {}'.format(peername))
 | 
					
						
							|  |  |  |             self.transport = transport
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def data_received(self, data):
 | 
					
						
							|  |  |  |             message = data.decode()
 | 
					
						
							|  |  |  |             print('Data received: {!r}'.format(message))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             print('Send: {!r}'.format(message))
 | 
					
						
							|  |  |  |             self.transport.write(data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             print('Close the client socket')
 | 
					
						
							|  |  |  |             self.transport.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def main():
 | 
					
						
							|  |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         server = await loop.create_server(
 | 
					
						
							| 
									
										
										
										
											2018-10-15 14:39:16 -07:00
										 |  |  |             lambda: EchoServerProtocol(),
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             '127.0.0.1', 8888)
 | 
					
						
							| 
									
										
										
										
											2014-01-24 17:33:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         async with server:
 | 
					
						
							|  |  |  |             await server.serve_forever()
 | 
					
						
							| 
									
										
										
										
											2014-01-24 17:33:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     asyncio.run(main())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :ref:`TCP echo server using streams <asyncio-tcp-echo-server-streams>`
 | 
					
						
							|  |  |  |    example uses the high-level :func:`asyncio.start_server` function.
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  | .. _asyncio_example_tcp_echo_client_protocol:
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | TCP Echo Client
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | A TCP echo client using the :meth:`loop.create_connection` method, sends
 | 
					
						
							|  |  |  | data, and waits until the connection is closed::
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:30:17 +02:00
										 |  |  |     class EchoClientProtocol(asyncio.Protocol):
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         def __init__(self, message, on_con_lost, loop):
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:35:09 +02:00
										 |  |  |             self.message = message
 | 
					
						
							|  |  |  |             self.loop = loop
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             self.on_con_lost = on_con_lost
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def connection_made(self, transport):
 | 
					
						
							|  |  |  |             transport.write(self.message.encode())
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:35:09 +02:00
										 |  |  |             print('Data sent: {!r}'.format(self.message))
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def data_received(self, data):
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:35:09 +02:00
										 |  |  |             print('Data received: {!r}'.format(data.decode()))
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def connection_lost(self, exc):
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:35:09 +02:00
										 |  |  |             print('The server closed the connection')
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             self.on_con_lost.set_result(True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def main():
 | 
					
						
							|  |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         on_con_lost = loop.create_future()
 | 
					
						
							|  |  |  |         message = 'Hello World!'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         transport, protocol = await loop.create_connection(
 | 
					
						
							|  |  |  |             lambda: EchoClientProtocol(message, on_con_lost, loop),
 | 
					
						
							|  |  |  |             '127.0.0.1', 8888)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Wait until the protocol signals that the connection
 | 
					
						
							|  |  |  |         # is lost and close the transport.
 | 
					
						
							|  |  |  |         try:
 | 
					
						
							|  |  |  |             await on_con_lost
 | 
					
						
							|  |  |  |         finally:
 | 
					
						
							|  |  |  |             transport.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     asyncio.run(main())
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 13:19:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :ref:`TCP echo client using streams <asyncio-tcp-echo-client-streams>`
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    example uses the high-level :func:`asyncio.open_connection` function.
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:13:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | .. _asyncio-udp-echo-server-protocol:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | UDP Echo Server
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | A UDP echo server, using the :meth:`loop.create_datagram_endpoint`
 | 
					
						
							|  |  |  | method, sends back received data::
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class EchoServerProtocol:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  |         def connection_made(self, transport):
 | 
					
						
							|  |  |  |             self.transport = transport
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         def datagram_received(self, data, addr):
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:13:40 +02:00
										 |  |  |             message = data.decode()
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             print('Received %r from %s' % (message, addr))
 | 
					
						
							|  |  |  |             print('Send %r to %s' % (message, addr))
 | 
					
						
							|  |  |  |             self.transport.sendto(data, addr)
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:46:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 15:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     async def main():
 | 
					
						
							|  |  |  |         print("Starting UDP server")
 | 
					
						
							| 
									
										
										
										
											2013-12-04 11:16:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:13:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         # One protocol instance will be created to serve all
 | 
					
						
							|  |  |  |         # client requests.
 | 
					
						
							|  |  |  |         transport, protocol = await loop.create_datagram_endpoint(
 | 
					
						
							|  |  |  |             lambda: EchoServerProtocol(),
 | 
					
						
							|  |  |  |             local_addr=('127.0.0.1', 9999))
 | 
					
						
							| 
									
										
										
										
											2013-12-04 11:16:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         try:
 | 
					
						
							|  |  |  |             await asyncio.sleep(3600)  # Serve for 1 hour.
 | 
					
						
							|  |  |  |         finally:
 | 
					
						
							|  |  |  |             transport.close()
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     asyncio.run(main())
 | 
					
						
							| 
									
										
										
										
											2014-10-12 20:18:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. _asyncio-udp-echo-client-protocol:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | UDP Echo Client
 | 
					
						
							|  |  |  | ---------------
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | A UDP echo client, using the :meth:`loop.create_datagram_endpoint`
 | 
					
						
							|  |  |  | method, sends data and closes the transport when it receives the answer::
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  |     class EchoClientProtocol:
 | 
					
						
							|  |  |  |         def __init__(self, message, loop):
 | 
					
						
							|  |  |  |             self.message = message
 | 
					
						
							|  |  |  |             self.loop = loop
 | 
					
						
							|  |  |  |             self.transport = None
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             self.on_con_lost = loop.create_future()
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def connection_made(self, transport):
 | 
					
						
							|  |  |  |             self.transport = transport
 | 
					
						
							|  |  |  |             print('Send:', self.message)
 | 
					
						
							|  |  |  |             self.transport.sendto(self.message.encode())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def datagram_received(self, data, addr):
 | 
					
						
							|  |  |  |             print("Received:", data.decode())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             print("Close the socket")
 | 
					
						
							|  |  |  |             self.transport.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def error_received(self, exc):
 | 
					
						
							|  |  |  |             print('Error received:', exc)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def connection_lost(self, exc):
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             print("Connection closed")
 | 
					
						
							|  |  |  |             self.on_con_lost.set_result(True)
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     async def main():
 | 
					
						
							|  |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         message = "Hello World!"
 | 
					
						
							|  |  |  |         transport, protocol = await loop.create_datagram_endpoint(
 | 
					
						
							|  |  |  |             lambda: EchoClientProtocol(message, loop),
 | 
					
						
							|  |  |  |             remote_addr=('127.0.0.1', 9999))
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         try:
 | 
					
						
							|  |  |  |             await protocol.on_con_lost
 | 
					
						
							|  |  |  |         finally:
 | 
					
						
							|  |  |  |             transport.close()
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     asyncio.run(main())
 | 
					
						
							| 
									
										
										
										
											2014-10-12 11:24:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  | .. _asyncio_example_create_connection:
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | Connecting Existing Sockets
 | 
					
						
							|  |  |  | ---------------------------
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Wait until a socket receives data using the
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | :meth:`loop.create_connection` method with a protocol::
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |     import socket
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class MyProtocol(asyncio.Protocol):
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def __init__(self, loop):
 | 
					
						
							|  |  |  |             self.transport = None
 | 
					
						
							|  |  |  |             self.on_con_lost = loop.create_future()
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def connection_made(self, transport):
 | 
					
						
							|  |  |  |             self.transport = transport
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def data_received(self, data):
 | 
					
						
							|  |  |  |             print("Received:", data.decode())
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             # We are done: close the transport;
 | 
					
						
							|  |  |  |             # connection_lost() will be called automatically.
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  |             self.transport.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def connection_lost(self, exc):
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |             # The socket has been closed
 | 
					
						
							|  |  |  |             self.on_con_lost.set_result(True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def main():
 | 
					
						
							|  |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         # Create a pair of connected sockets
 | 
					
						
							|  |  |  |         rsock, wsock = socket.socketpair()
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         # Register the socket to wait for data.
 | 
					
						
							|  |  |  |         transport, protocol = await loop.create_connection(
 | 
					
						
							|  |  |  |             lambda: MyProtocol(loop), sock=rsock)
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         # Simulate the reception of data from the network.
 | 
					
						
							|  |  |  |         loop.call_soon(wsock.send, 'abc'.encode())
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |         try:
 | 
					
						
							|  |  |  |             await protocol.on_con_lost
 | 
					
						
							|  |  |  |         finally:
 | 
					
						
							|  |  |  |             transport.close()
 | 
					
						
							|  |  |  |             wsock.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     asyncio.run(main())
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :ref:`watch a file descriptor for read events
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  |    <asyncio_example_watch_fd>` example uses the low-level
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  |    :meth:`loop.add_reader` method to register an FD.
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The :ref:`register an open socket to wait for data using streams
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  |    <asyncio_example_create_connection-streams>` example uses high-level streams
 | 
					
						
							| 
									
										
										
										
											2014-10-11 16:16:27 +02:00
										 |  |  |    created by the :func:`open_connection` function in a coroutine.
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  | .. _asyncio_example_subprocess_proto:
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | loop.subprocess_exec() and SubprocessProtocol
 | 
					
						
							|  |  |  | ---------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 10:06:55 -07:00
										 |  |  | An example of a subprocess protocol used to get the output of a
 | 
					
						
							| 
									
										
										
										
											2018-09-11 09:54:40 -07:00
										 |  |  | subprocess and to wait for the subprocess exit.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The subprocess is created by th :meth:`loop.subprocess_exec` method::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import asyncio
 | 
					
						
							|  |  |  |     import sys
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class DateProtocol(asyncio.SubprocessProtocol):
 | 
					
						
							|  |  |  |         def __init__(self, exit_future):
 | 
					
						
							|  |  |  |             self.exit_future = exit_future
 | 
					
						
							|  |  |  |             self.output = bytearray()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def pipe_data_received(self, fd, data):
 | 
					
						
							|  |  |  |             self.output.extend(data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def process_exited(self):
 | 
					
						
							|  |  |  |             self.exit_future.set_result(True)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def get_date():
 | 
					
						
							|  |  |  |         # Get a reference to the event loop as we plan to use
 | 
					
						
							|  |  |  |         # low-level APIs.
 | 
					
						
							|  |  |  |         loop = asyncio.get_running_loop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         code = 'import datetime; print(datetime.datetime.now())'
 | 
					
						
							|  |  |  |         exit_future = asyncio.Future(loop=loop)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Create the subprocess controlled by DateProtocol;
 | 
					
						
							|  |  |  |         # redirect the standard output into a pipe.
 | 
					
						
							|  |  |  |         transport, protocol = await loop.subprocess_exec(
 | 
					
						
							|  |  |  |             lambda: DateProtocol(exit_future),
 | 
					
						
							|  |  |  |             sys.executable, '-c', code,
 | 
					
						
							|  |  |  |             stdin=None, stderr=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Wait for the subprocess exit using the process_exited()
 | 
					
						
							|  |  |  |         # method of the protocol.
 | 
					
						
							|  |  |  |         await exit_future
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Close the stdout pipe.
 | 
					
						
							|  |  |  |         transport.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Read the output which was collected by the
 | 
					
						
							|  |  |  |         # pipe_data_received() method of the protocol.
 | 
					
						
							|  |  |  |         data = bytes(protocol.output)
 | 
					
						
							|  |  |  |         return data.decode('ascii').rstrip()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     date = asyncio.run(get_date())
 | 
					
						
							|  |  |  |     print(f"Current date: {date}")
 | 
					
						
							| 
									
										
										
										
											2018-09-17 15:35:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | See also the :ref:`same example <asyncio_example_create_subprocess_exec>`
 | 
					
						
							|  |  |  | written using high-level APIs.
 |