传输和协议

前言

传输和协议用于 低级 事件循环 API 譬如 loop.create_connection() 。它们使用基于回调的编程风格,并支持网络或 IPC 协议 (如 HTTP) 的高性能实现。

本质上,传输和协议只应用于库和框架,且从不应该用于高级 asyncio 应用程序。

此文档编制页面涵盖 传输 and 协议 .

介绍

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.

Yet another view is the transport and protocol interfaces 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 loop.create_connection() ) 通常接受 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) .

内容

此文档页面包含下列章节:

传输

源代码: Lib/asyncio/transports.py


传输是类提供通过 asyncio 为抽象各种通信通道。

传输对象始终被实例化通过 asyncio 事件循环 .

asyncio implements transports for TCP, UDP, SSL, and subprocess pipes. The methods available on a transport depend on the transport’s kind.

传输类是 非线程安全 .

传输层次结构

class asyncio. BaseTransport

Base class for all transports. Contains methods that all asyncio transports share.

class asyncio. WriteTransport ( BaseTransport )

A base transport for write-only connections.

实例化的 WriteTransport class are returned from the loop.connect_write_pipe() event loop method and are also used by subprocess-related methods like loop.subprocess_exec() .

class asyncio. ReadTransport ( BaseTransport )

A base transport for read-only connections.

实例化的 ReadTransport class are returned from the loop.connect_read_pipe() event loop method and are also used by subprocess-related methods like loop.subprocess_exec() .

class asyncio. Transport ( WriteTransport , ReadTransport )

Interface representing a bidirectional transport, such as a TCP connection.

The user does not instantiate a transport directly; they call a utility function, passing it a protocol factory and other information necessary to create the transport and protocol.

实例化的 Transport class are returned from or used by event loop methods like loop.create_connection() , loop.create_unix_connection() , loop.create_server() , loop.sendfile() ,等。

class asyncio. DatagramTransport ( BaseTransport )

A transport for datagram (UDP) connections.

实例化的 DatagramTransport class are returned from the loop.create_datagram_endpoint() event loop method.

class asyncio. SubprocessTransport ( BaseTransport )

An abstraction to represent a connection between a parent and its child OS process.

实例化的 SubprocessTransport class are returned from event loop methods loop.subprocess_shell() and loop.subprocess_exec() .

基传输

BaseTransport. close ( )

Close the transport.

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 protocol.connection_lost() method will be called with None as its argument. The transport should not be used once it is closed.

BaseTransport. is_closing ( )

返回 True if the transport is closing or is closed.

BaseTransport. get_extra_info ( 名称 , default = None )

Return information about the transport or underlying resources it uses.

name is a string representing the piece of transport-specific information to get.

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.

For example, the following code attempts to get the underlying socket object of the transport:

sock = transport.get_extra_info('socket')
if sock is not None:
    print(sock.getsockopt(...))