mio::poll文档 —— 翻译和注解

翻译自mio文档: Poll

Struct mio::Poll

1
pub struct Poll { /* fields omitted */ }

Polls for readiness events on all registered values.

Poll allows a program to monitor a large number of Evented types, waiting until one or more become “ready” for some class of operations; e.g. reading and writing. An Evented type is considered ready if it is possible to immediately perform a corresponding operation; e.g. read or write.

To use Poll, an Evented type must first be registered with the Poll instance using the register method, supplying readiness interest. The readiness interest tells Poll which specific operations on the handle to monitor for readiness. A Token is also passed to the register function. When Poll returns a readiness event, it will include this token. This associates the event with the Evented handle that generated the event.

Poll用于从所有注册的value里poll处于就绪状态的事件。这个跟Java里NIO里的概念可以类比下:

  • Selector <-> Poll。可以把Evented注册到Poll上,使用它来监听Evented有关的IO事件。是实现异步IO的关键组件。
  • Evented <-> SelectionKey。可以通过SelectionKey获取底层的channel进行读写。Evented可以直接进行读写。
  • Token <-> SelectionKey里attach的对象。用于将事件跟Evented关联起来。
阅读更多