@@ -333,6 +333,16 @@ Protocol classes
333
333
The base class for implementing streaming protocols (for use with
334
334
e.g. TCP and SSL transports).
335
335
336
+ .. class :: BufferedProtocol
337
+
338
+ A base class for implementing streaming protocols with manual
339
+ control of the receive buffer.
340
+
341
+ .. versionadded :: 3.7
342
+ **Important: ** this has been been added to asyncio in Python 3.7
343
+ *on a provisional basis *! Treat it as an experimental API that
344
+ might be changed or removed in Python 3.8.
345
+
336
346
.. class :: DatagramProtocol
337
347
338
348
The base class for implementing datagram protocols (for use with
@@ -428,9 +438,66 @@ and, if called, :meth:`data_received` won't be called after it.
428
438
429
439
State machine:
430
440
441
+ .. code-block :: none
442
+
443
+ start -> :meth:`~BaseProtocol.connection_made`
444
+ [-> :meth:`~Protocol.data_received`]*
445
+ [-> :meth:`~Protocol.eof_received`]?
446
+ -> :meth:`~BaseProtocol.connection_lost` -> end
447
+
448
+
449
+ Streaming protocols with manual receive buffer control
450
+ ------------------------------------------------------
451
+
452
+ .. versionadded :: 3.7
453
+ **Important: ** :class: `BufferedProtocol ` has been been added to
454
+ asyncio in Python 3.7 *on a provisional basis *! Treat it as an
455
+ experimental API that might be changed or removed in Python 3.8.
456
+
457
+
458
+ Event methods, such as :meth: `AbstractEventLoop.create_server ` and
459
+ :meth: `AbstractEventLoop.create_connection `, accept factories that
460
+ return protocols that implement this interface.
461
+
462
+ The idea of BufferedProtocol is that it allows to manually allocate
463
+ and control the receive buffer. Event loops can then use the buffer
464
+ provided by the protocol to avoid unnecessary data copies. This
465
+ can result in noticeable performance improvement for protocols that
466
+ receive big amounts of data. Sophisticated protocols can allocate
467
+ the buffer only once at creation time.
468
+
469
+ The following callbacks are called on :class: `BufferedProtocol `
470
+ instances:
471
+
472
+ .. method :: BufferedProtocol.get_buffer()
473
+
474
+ Called to allocate a new receive buffer. Must return an object
475
+ that implements the :ref: `buffer protocol <bufferobjects >`.
476
+
477
+ .. method :: BufferedProtocol.buffer_updated(nbytes)
478
+
479
+ Called when the buffer was updated with the received data.
480
+
481
+ *nbytes * is the total number of bytes that were written to the buffer.
482
+
483
+ .. method :: BufferedProtocol.eof_received()
484
+
485
+ See the documentation of the :meth: `Protocol.eof_received ` method.
486
+
487
+
488
+ :meth: `get_buffer ` can be called an arbitrary number of times during
489
+ a connection. However, :meth: `eof_received ` is called at most once
490
+ and, if called, :meth: `data_received ` won't be called after it.
491
+
492
+ State machine:
493
+
494
+ .. code-block :: none
495
+
431
496
start -> :meth:`~BaseProtocol.connection_made`
432
- [-> :meth: `~Protocol.data_received ` \* ]
433
- [-> :meth: `~Protocol.eof_received ` ?]
497
+ [-> :meth:`~BufferedProtocol.get_buffer`
498
+ [-> :meth:`~BufferedProtocol.buffer_updated`]?
499
+ ]*
500
+ [-> :meth:`~Protocol.eof_received`]?
434
501
-> :meth:`~BaseProtocol.connection_lost` -> end
435
502
436
503
0 commit comments