• selectors —- 高级 I/O 复用库
    • 概述
    • 示例

    selectors —- 高级 I/O 复用库

    3.4 新版功能.

    源码:Lib/selectors.py


    概述

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

    It defines a BaseSelector abstract base class, along with severalconcrete implementations (KqueueSelector, EpollSelector…),that can be used to wait for I/O readiness notification on multiple fileobjects. In the following, "file object" refers to any object with afileno() method, or a raw file descriptor. See file object.

    DefaultSelector is an alias to the most efficient implementationavailable on the current platform: this should be the default choice for mostusers.

    注解

    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 filedevices).

    参见

    • select
    • Low-level I/O multiplexing module.

    Classes hierarchy:

    1. BaseSelector
    2. +-- SelectSelector
    3. +-- PollSelector
    4. +-- EpollSelector
    5. +-- DevpollSelector
    6. +-- KqueueSelector

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

    常数

    意义

    EVENT_READ

    可读

    EVENT_WRITE

    可写

    • class selectors.SelectorKey
    • A SelectorKey is a namedtuple used toassociate a file object to its underlying file descriptor, selected eventmask and attached data. It is returned by several BaseSelectormethods.

      • fileobj
      • File object registered.

      • fd

      • Underlying file descriptor.

      • events

      • Events that must be waited for on this file object.

      • data

      • Optional opaque data associated to this file object: for example, thiscould be used to store a per-client session ID.
    • class selectors.BaseSelector

    • A BaseSelector is used to wait for I/O event readiness on multiplefile objects. It supports file stream registration, unregistration, and amethod to wait for I/O events on those streams, with an optional timeout.It's an abstract base class, so cannot be instantiated. UseDefaultSelector instead, or one of SelectSelector,KqueueSelector etc. if you want to specifically use animplementation, and your platform supports it.BaseSelector and its concrete implementations support thecontext 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 integerfile 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 SelectorKey instance, or raises aValueError in case of invalid event mask or file descriptor, orKeyError if the file object is already registered.

    • abstractmethod unregister(fileobj)
    • Unregister a file object from selection, removing it from monitoring. Afile object shall be unregistered prior to being closed.

    fileobj must be a file object previously registered.

    This returns the associated SelectorKey instance, or raises aKeyError if fileobj is not registered. It will raiseValueError if fileobj is invalid (e.g. it has no fileno()method or its fileno() method has an invalid return value).

    • modify(fileobj, events, data=None)
    • Change a registered file object's monitored events or attached data.

    This is equivalent to BaseSelector.unregister(fileobj)() followedby BaseSelector.register(fileobj, events, data)(), except that itcan be implemented more efficiently.

    This returns a new SelectorKey instance, or raises aValueError in case of invalid event mask or file descriptor, orKeyError if the file object is not registered.

    • abstractmethod select(timeout=None)
    • Wait until some registered file objects become ready, or the timeoutexpires.

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

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

    key is the SelectorKey instance corresponding to a ready fileobject.events is a bitmask of events ready on this file object.

    注解

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

    在 3.5 版更改: The selector is now retried with a recomputed timeout when interruptedby a signal if the signal handler did not raise an exception (seePEP 475 for the rationale), instead of returning an empty listof events before the timeout.

    • 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.

    • getkey(_fileobj)
    • Return the key associated with a registered file object.

    This returns the SelectorKey instance associated to this fileobject, or raises KeyError if the file object is not registered.

    • abstractmethod get_map()
    • Return a mapping of file objects to selector keys.

    This returns a Mapping instance mappingregistered file objects to their associated SelectorKeyinstance.

    • class selectors.DefaultSelector
    • The default selector class, using the most efficient implementationavailable on the current platform. This should be the default choice formost users.

    • class selectors.SelectSelector

    • select.select()-based selector.

    • class selectors.PollSelector

    • select.poll()-based selector.

    • class selectors.EpollSelector

    • select.epoll()-based selector.

      • fileno()
      • This returns the file descriptor used by the underlyingselect.epoll() object.
    • class selectors.DevpollSelector

    • select.devpoll()-based selector.

      • fileno()
      • This returns the file descriptor used by the underlyingselect.devpoll() object.

    3.5 新版功能.

    • class selectors.KqueueSelector
    • select.kqueue()-based selector.

      • fileno()
      • This returns the file descriptor used by the underlyingselect.kqueue() object.

    示例

    Here is a simple echo server implementation:

    1. import selectors
    2. import socket
    3.  
    4. sel = selectors.DefaultSelector()
    5.  
    6. def accept(sock, mask):
    7. conn, addr = sock.accept() # Should be ready
    8. print('accepted', conn, 'from', addr)
    9. conn.setblocking(False)
    10. sel.register(conn, selectors.EVENT_READ, read)
    11.  
    12. def read(conn, mask):
    13. data = conn.recv(1000) # Should be ready
    14. if data:
    15. print('echoing', repr(data), 'to', conn)
    16. conn.send(data) # Hope it won't block
    17. else:
    18. print('closing', conn)
    19. sel.unregister(conn)
    20. conn.close()
    21.  
    22. sock = socket.socket()
    23. sock.bind(('localhost', 1234))
    24. sock.listen(100)
    25. sock.setblocking(False)
    26. sel.register(sock, selectors.EVENT_READ, accept)
    27.  
    28. while True:
    29. events = sel.select()
    30. for key, mask in events:
    31. callback = key.data
    32. callback(key.fileobj, mask)