events

  • Handle

    • 基本属性: loop, context, call_back, args
    • run: 执行call_back
  • TimeHandle: 带有when到期时间的handle,也用于来比较大小

  • AbstractServer: 抽象server, 定义start_server、server_forever等方法

  • AbstractEventLoop:抽象loop, 定义run_forever、run_until_complte、stop、create_tast、call_later、create_connection、add_reader、sock_recv等方法

base_events

  • Server

    • 属性: loop,sockets, protocl_factory
    • _start_serving: 为各个socket启动一个serveing
  • BaseEventLoop

    • 基本属性
    • _ready: dequeue类型,存放已经ready的task
    • _scheduled: list类型, 存放time handle,使用heapq排序
    • selector: IO复用模型

    • _run_once方法:

      • 移除已经取消的sheduled handle
      • 设置timeout: ready有值设为0,否则取第一个schedule的delay时间
      • selector获取事件列表
      • process_events 处理事件(如没取消加入ready)
      • 将scheduled中已经到时间的handle移植ready
      • 依次运行ready中的handle
    • run_forever: 不断执行_run_once直到stop

    • run_until_complete: run_forever后返回指定future的结果

    • call_soon: 构造一个handle加入ready尾部

    • call_later:构造一个time handle加入scheduled

    • add_reader: 将fd 和callback注册到selector的read列表里

base_futures

  • isfuture: hasattr(obj.class, ‘_asyncio_future_blocking’)

futures

  • Future
    • 基本属性:_state, _result, _excetption, _loop, _asyncio_future_blocking, _callbacks
    • cancel: call_soon当前的call_back并将_state设为_CANCELLED
    • set_result: 设置_result并将_state设为_FINISHED,调用__schedule_callbacks

tasks

  • Task(继承自Future)
    • 基本属性: _coro, _name, _fut_waiter
    • __step:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 
try:   
    result = coro.send(None) 
except StopIteration as exc:
    # coro finished
    super().set_result(exc.value)
else:
    if isinstance(result, Future):
        if result._blocking:
            self._blocking = False
            result.add_done_callback(self._wakeup, result)
        else:
            pass
    elif result is None:
        self._loop.call_soon(self._step)
    else:
       pass
  • wait: 等超时或return_when条件满足之后返回(donw=e, pengding)两类futures

Awaitables

  • coroutines
  • Task: schedule coroutines concurrently
  • Future: eventual result of an asynchronous operation.

transports:

  • 理解:the transport is concerned with how bytes are transmitted. a different way of saying the same thing: a transport is an abstraction for a socket
  • 主要类:

    • BaseTransport
    • ReadTransport
    • WriteTransport
    • Transport: a bidirectional transport
  • selector.Transport

protocols:

  • 理解:the protocol determines which bytes to transmit (and to some extent when). a different way of saying the same thing: a protocol is an abstraction for an application
  • 主要类:
    • BaseProtocol:
      • connnection_made: start a connect base on a specified transport
      • connection_lost: called when the connection is lost or closed
      • pause_writing: called when transport’s buffer goes over the high-water mark
      • resume_writing: called when transport’s drains below the low-water mark
    • Protocol: stream protocol
      • data_receied
      • eof_received

streams

  • StreamReader
    • read
    • readexactly
  • StreamWriter
    • write

参考