summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-01-28 21:30:26 (GMT)
committerGitHub <noreply@github.com>2018-01-28 21:30:26 (GMT)
commit631fd38dbf04dbf0127881f3977982e401a849e4 (patch)
tree1d252eeef90eced0770b2d906b8540ec28012060 /Doc/library
parent0ceb717689b04c0540d78c1ba93c0572c66c0994 (diff)
downloadcpython-631fd38dbf04dbf0127881f3977982e401a849e4.zip
cpython-631fd38dbf04dbf0127881f3977982e401a849e4.tar.gz
cpython-631fd38dbf04dbf0127881f3977982e401a849e4.tar.bz2
bpo-32251: Implement asyncio.BufferedProtocol. (#4755)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/asyncio-protocol.rst75
1 files changed, 71 insertions, 4 deletions
diff --git a/Doc/library/asyncio-protocol.rst b/Doc/library/asyncio-protocol.rst
index 3aa1f2f..a08c4d3 100644
--- a/Doc/library/asyncio-protocol.rst
+++ b/Doc/library/asyncio-protocol.rst
@@ -333,6 +333,16 @@ Protocol classes
The base class for implementing streaming protocols (for use with
e.g. TCP and SSL transports).
+.. class:: BufferedProtocol
+
+ A base class for implementing streaming protocols with manual
+ control of the receive buffer.
+
+ .. versionadded:: 3.7
+ **Important:** this has been been added to asyncio in Python 3.7
+ *on a provisional basis*! Treat it as an experimental API that
+ might be changed or removed in Python 3.8.
+
.. class:: DatagramProtocol
The base class for implementing datagram protocols (for use with
@@ -428,10 +438,67 @@ and, if called, :meth:`data_received` won't be called after it.
State machine:
- start -> :meth:`~BaseProtocol.connection_made`
- [-> :meth:`~Protocol.data_received` \*]
- [-> :meth:`~Protocol.eof_received` ?]
- -> :meth:`~BaseProtocol.connection_lost` -> end
+.. code-block:: none
+
+ start -> connection_made
+ [-> data_received]*
+ [-> eof_received]?
+ -> connection_lost -> end
+
+
+Streaming protocols with manual receive buffer control
+------------------------------------------------------
+
+.. versionadded:: 3.7
+ **Important:** :class:`BufferedProtocol` has been been added to
+ asyncio in Python 3.7 *on a provisional basis*! Treat it as an
+ experimental API that might be changed or removed in Python 3.8.
+
+
+Event methods, such as :meth:`AbstractEventLoop.create_server` and
+:meth:`AbstractEventLoop.create_connection`, accept factories that
+return protocols that implement this interface.
+
+The idea of BufferedProtocol is that it allows to manually allocate
+and control the receive buffer. Event loops can then use the buffer
+provided by the protocol to avoid unnecessary data copies. This
+can result in noticeable performance improvement for protocols that
+receive big amounts of data. Sophisticated protocols can allocate
+the buffer only once at creation time.
+
+The following callbacks are called on :class:`BufferedProtocol`
+instances:
+
+.. method:: BufferedProtocol.get_buffer()
+
+ Called to allocate a new receive buffer. Must return an object
+ that implements the :ref:`buffer protocol <bufferobjects>`.
+
+.. 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()
+
+ See the documentation of the :meth:`Protocol.eof_received` method.
+
+
+:meth:`get_buffer` can be called an arbitrary number of times during
+a connection. However, :meth:`eof_received` is called at most once
+and, if called, :meth:`data_received` won't be called after it.
+
+State machine:
+
+.. code-block:: none
+
+ start -> connection_made
+ [-> get_buffer
+ [-> buffer_updated]?
+ ]*
+ [-> eof_received]?
+ -> connection_lost -> end
Datagram protocols