开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.8.1

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
selectors.rst
python3.8.1
/
Doc
/
library
/
selectors.rst
selectors.rst 8.72 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`selectors` --- High-level I/O multiplexing

.. module:: selectors
 :synopsis: High-level I/O multiplexing.

.. versionadded:: 3.4

Source code: :source:`Lib/selectors.py`


Introduction

This module allows high-level and efficient I/O multiplexing, built upon the :mod:`select` module primitives. Users are encouraged to use this module instead, unless they want precise control over the OS-level primitives used.

It defines a :class:`BaseSelector` abstract base class, along with several concrete implementations (:class:`KqueueSelector`, :class:`EpollSelector`...), that can be used to wait for I/O readiness notification on multiple file objects. In the following, "file object" refers to any object with a :meth:`fileno()` method, or a raw file descriptor. See :term:`file object`.

:class:`DefaultSelector` is an alias to the most efficient implementation available on the current platform: this should be the default choice for most users.

Note

The type of file objects supported depends on the platform: on Windows, sockets are supported, but not pipes, whereas on Unix, both are supported (some other types may be supported as well, such as fifos or special file devices).

.. seealso::

 :mod:`select`
 Low-level I/O multiplexing module.


Classes

Classes hierarchy:

BaseSelector
+-- SelectSelector
+-- PollSelector
+-- EpollSelector
+-- DevpollSelector
+-- KqueueSelector

In the following, events is a bitwise mask indicating which I/O events should be waited for on a given file object. It can be a combination of the modules constants below:

Constant Meaning
:const:`EVENT_READ` Available for read
:const:`EVENT_WRITE` Available for write

A :class:`SelectorKey` is a :class:`~collections.namedtuple` used to associate a file object to its underlying file descriptor, selected event mask and attached data. It is returned by several :class:`BaseSelector` methods.

.. attribute:: fileobj

 File object registered.

.. attribute:: fd

 Underlying file descriptor.

.. attribute:: events

 Events that must be waited for on this file object.

.. attribute:: data

 Optional opaque data associated to this file object: for example, this
 could be used to store a per-client session ID.

A :class:`BaseSelector` is used to wait for I/O event readiness on multiple file objects. It supports file stream registration, unregistration, and a method to wait for I/O events on those streams, with an optional timeout. It's an abstract base class, so cannot be instantiated. Use :class:`DefaultSelector` instead, or one of :class:`SelectSelector`, :class:`KqueueSelector` etc. if you want to specifically use an implementation, and your platform supports it. :class:`BaseSelector` and its concrete implementations support the :term:`context manager` protocol.

.. abstractmethod:: register(fileobj, events, data=None)

 Register a file object for selection, monitoring it for I/O events.

 *fileobj* is the file object to monitor. It may either be an integer
 file descriptor or an object with a ``fileno()`` method.
 *events* is a bitwise mask of events to monitor.
 *data* is an opaque object.

 This returns a new :class:`SelectorKey` instance, or raises a
 :exc:`ValueError` in case of invalid event mask or file descriptor, or
 :exc:`KeyError` if the file object is already registered.

.. abstractmethod:: unregister(fileobj)

 Unregister a file object from selection, removing it from monitoring. A
 file object shall be unregistered prior to being closed.

 *fileobj* must be a file object previously registered.

 This returns the associated :class:`SelectorKey` instance, or raises a
 :exc:`KeyError` if *fileobj* is not registered. It will raise
 :exc:`ValueError` if *fileobj* is invalid (e.g. it has no ``fileno()``
 method or its ``fileno()`` method has an invalid return value).

.. method:: modify(fileobj, events, data=None)

 Change a registered file object's monitored events or attached data.

 This is equivalent to :meth:`BaseSelector.unregister(fileobj)` followed
 by :meth:`BaseSelector.register(fileobj, events, data)`, except that it
 can be implemented more efficiently.

 This returns a new :class:`SelectorKey` instance, or raises a
 :exc:`ValueError` in case of invalid event mask or file descriptor, or
 :exc:`KeyError` if the file object is not registered.

.. abstractmethod:: select(timeout=None)

 Wait until some registered file objects become ready, or the timeout
 expires.

 If ``timeout > 0``, this specifies the maximum wait time, in seconds.
 If ``timeout <= 0``, the call won't block, and will report the currently
 ready file objects.
 If *timeout* is ``None``, the call will block until a monitored file object
 becomes ready.

 This returns a list of ``(key, events)`` tuples, one for each ready file
 object.

 *key* is the :class:`SelectorKey` instance corresponding to a ready file
 object.
 *events* is a bitmask of events ready on this file object.

 .. note::
 This method can return before any file object becomes ready or the
 timeout has elapsed if the current process receives a signal: in this
 case, an empty list will be returned.

 .. versionchanged:: 3.5
 The selector is now retried with a recomputed timeout when interrupted
 by a signal if the signal handler did not raise an exception (see
 :pep:`475` for the rationale), instead of returning an empty list
 of events before the timeout.

.. method:: close()

 Close the selector.

 This must be called to make sure that any underlying resource is freed.
 The selector shall not be used once it has been closed.

.. method:: get_key(fileobj)

 Return the key associated with a registered file object.

 This returns the :class:`SelectorKey` instance associated to this file
 object, or raises :exc:`KeyError` if the file object is not registered.

.. abstractmethod:: get_map()

 Return a mapping of file objects to selector keys.

 This returns a :class:`~collections.abc.Mapping` instance mapping
 registered file objects to their associated :class:`SelectorKey`
 instance.

The default selector class, using the most efficient implementation available on the current platform. This should be the default choice for most users.

:func:`select.select`-based selector.

:func:`select.poll`-based selector.

:func:`select.epoll`-based selector.

.. method:: fileno()

 This returns the file descriptor used by the underlying
 :func:`select.epoll` object.

:func:`select.devpoll`-based selector.

.. method:: fileno()

 This returns the file descriptor used by the underlying
 :func:`select.devpoll` object.

.. versionadded:: 3.5

:func:`select.kqueue`-based selector.

.. method:: fileno()

 This returns the file descriptor used by the underlying
 :func:`select.kqueue` object.

Examples

Here is a simple echo server implementation:

import selectors
import socket

sel = selectors.DefaultSelector()

def accept(sock, mask):
 conn, addr = sock.accept() # Should be ready
 print('accepted', conn, 'from', addr)
 conn.setblocking(False)
 sel.register(conn, selectors.EVENT_READ, read)

def read(conn, mask):
 data = conn.recv(1000) # Should be ready
 if data:
 print('echoing', repr(data), 'to', conn)
 conn.send(data) # Hope it won't block
 else:
 print('closing', conn)
 sel.unregister(conn)
 conn.close()

sock = socket.socket()
sock.bind(('localhost', 1234))
sock.listen(100)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)

while True:
 events = sel.select()
 for key, mask in events:
 callback = key.data
 callback(key.fileobj, mask)
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /