Back to module index
Go to module by name
selectors
Selectors module.
This module allows high-level and efficient I/O multiplexing, built upon the
`select` module primitives.
Classes
ABCMeta
Metaclass for defining Abstract Base Classes (ABCs).
Use this metaclass to create an ABC. An ABC can be subclassed
directly, and then acts as a mix-in class. You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).
mro(self, /)
Return a type's method resolution order.
register(cls, subclass)
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
BaseSelector
Selector abstract base class.
A selector supports registering file objects to be monitored for specific
I/O events.
A file object is a file descriptor or any object with a `fileno()` method.
An arbitrary object can be attached to the file object, which can be used
for example to store context information, a callback, etc.
A selector can use various implementations (select(), poll(), epoll()...)
depending on the platform. The default `Selector` class uses the most
efficient implementation on the current platform.
close(self)
Close the selector.
This must be called to make sure that any underlying resource is freed.
get_key(self, fileobj)
Return the key associated to a registered file object.
Returns:
SelectorKey for this file object
get_map(self)
Return a mapping of file objects to selector keys.
modify(self, fileobj, events, data=None)
Change a registered file object monitored events or attached data.
Parameters:
fileobj -- file object or file descriptor
events -- events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE)
data -- attached data
Returns:
SelectorKey instance
Raises:
Anything that unregister() or register() raises
register(self, fileobj, events, data=None)
Register a file object.
Parameters:
fileobj -- file object or file descriptor
events -- events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE)
data -- attached data
Returns:
SelectorKey instance
Raises:
ValueError if events is invalid
KeyError if fileobj is already registered
OSError if fileobj is closed or otherwise is unacceptable to
the underlying system call (if a system call is made)
Note:
OSError may or may not be raised
select(self, timeout=None)
Perform the actual selection, until some monitored file objects are
ready or a timeout expires.
Parameters:
timeout -- if timeout > 0, this specifies the maximum wait time, in
seconds
if timeout <= 0, the select() call won't block, and will
report the currently ready file objects
if timeout is None, select() will block until a monitored
file object becomes ready
Returns:
list of (key, events) for ready file objects
`events` is a bitwise mask of EVENT_READ|EVENT_WRITE
unregister(self, fileobj)
Unregister a file object.
Parameters:
fileobj -- file object or file descriptor
Returns:
SelectorKey instance
Raises:
KeyError if fileobj is not registered
Note:
If fileobj is registered but has since been closed this does
*not* raise OSError (even if the wrapped syscall does)
EpollSelector
Epoll-based selector.
close(self)
fileno(self)
get_key(self, fileobj)
Return the key associated to a registered file object.
Returns:
SelectorKey for this file object
get_map(self)
modify(self, fileobj, events, data=None)
register(self, fileobj, events, data=None)
select(self, timeout=None)
unregister(self, fileobj)
EpollSelector
Epoll-based selector.
close(self)
fileno(self)
get_key(self, fileobj)
Return the key associated to a registered file object.
Returns:
SelectorKey for this file object
get_map(self)
modify(self, fileobj, events, data=None)
register(self, fileobj, events, data=None)
select(self, timeout=None)
unregister(self, fileobj)
Mapping
A Mapping is a generic container for associating key/value
pairs.
This class provides concrete generic implementations of all
methods except for __getitem__, __iter__, and __len__.
get(self, key, default=None)
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
items(self)
D.items() -> a set-like object providing a view on D's items
keys(self)
D.keys() -> a set-like object providing a view on D's keys
values(self)
D.values() -> an object providing a view on D's values
PollSelector
Poll-based selector.
close(self)
get_key(self, fileobj)
Return the key associated to a registered file object.
Returns:
SelectorKey for this file object
get_map(self)
modify(self, fileobj, events, data=None)
register(self, fileobj, events, data=None)
select(self, timeout=None)
unregister(self, fileobj)
SelectSelector
Select-based selector.
close(self)
get_key(self, fileobj)
Return the key associated to a registered file object.
Returns:
SelectorKey for this file object
get_map(self)
modify(self, fileobj, events, data=None)
register(self, fileobj, events, data=None)
select(self, timeout=None)
unregister(self, fileobj)
SelectorKey
SelectorKey(fileobj, fd, events, data)
Object used to associate a file object to its backing
file descriptor, selected event mask, and attached data.
count(self, value, /)
Return number of occurrences of value.
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
data = _tuplegetter(3, 'Optional opaque data associated to this file object.\n For example, this could be used to store a per-client session ID.')
Optional opaque data associated to this file object.
For example, this could be used to store a per-client session ID.
events = _tuplegetter(2, 'Events that must be waited for on this file object.')
Events that must be waited for on this file object.
fd = _tuplegetter(1, 'Underlying file descriptor.')
Underlying file descriptor.
fileobj = _tuplegetter(0, 'File object registered.')
File object registered.
Functions
abstractmethod
abstractmethod(funcobj)
A decorator indicating abstract methods.
Requires that the metaclass is ABCMeta or derived from it. A
class that has a metaclass derived from ABCMeta cannot be
instantiated unless all of its abstract methods are overridden.
The abstract methods can be called using any of the normal
'super' call mechanisms. abstractmethod() may be used to declare
abstract methods for properties and descriptors.
Usage:
class C(metaclass=ABCMeta):
@abstractmethod
def my_abstract_method(self, ...):
...
namedtuple
namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain tuple
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> d = p._asdict() # convert to a dictionary
>>> d['x']
11
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
Other members
EVENT_READ = 1
EVENT_WRITE = 2
Modules
math
select
sys