class selectors.BaseSelector
A 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 DefaultSelector
instead, or one of SelectSelector
, KqueueSelector
etc. if you want to specifically use an implementation, and your platform supports it. BaseSelector
and its concrete implementations support the 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
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. A file 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 nofileno()
method or itsfileno()
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)()
followed byBaseSelector.register(fileobj, events, data)()
, except that it can 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 timeout expires.
If
timeout > 0
, this specifies the maximum wait time, in seconds. Iftimeout <= 0
, the call won’t block, and will report the currently ready file objects. If timeout isNone
, 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
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.
Changed in version 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.
-
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.
-
get_key(fileobj)
-
Return the key associated with a registered file object.
This returns the
SelectorKey
instance associated to this file object, or raisesKeyError
if the file object is not registered.
-
abstractmethod get_map()
-
Return a mapping of file objects to selector keys.
This returns a
Mapping
instance mapping registered file objects to their associatedSelectorKey
instance.
Please login to continue.