select module

This module supports asynchronous I/O on multiple file descriptors.

* IMPORTANT NOTICE * On Windows, only sockets are supported; on Unix, all file descriptors.

class select.kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)

Bases: object

This object is the equivalent of the struct kevent for the C API.

See the kqueue manpage for more detailed information about the meaning of the arguments.

One minor note: while you might hope that udata could store a reference to a python object, it cannot, because it is impossible to keep a proper reference count of the object once it’s passed into the kernel. Therefore, I have restricted it to only storing an integer. I recommend ignoring it and simply using the ‘ident’ field to key off of. You could also set up a dictionary on the python side to store a udata->object mapping.

data
fflags
filter
flags
ident
udata
class select.kqueue

Bases: object

Kqueue syscall wrapper.

For example, to start watching a socket for input: >>> kq = kqueue() >>> sock = socket() >>> sock.connect((host, port)) >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)

To wait one second for it to become writeable: >>> kq.control(None, 1, 1000)

To stop listening: >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)

close()

Close the kqueue control file descriptor.

Further operations on the kqueue object will raise an exception.

closed

True if the kqueue handler is closed

control(changelist, maxevents, timeout=None, /)

Calls the kernel kevent function.

changelist

Must be an iterable of kevent objects describing the changes to be made to the kernel’s watch list or None.

maxevents

The maximum number of events that the kernel will return.

timeout

The maximum time to wait in seconds, or else None to wait forever. This accepts floats for smaller timeouts, too.

fileno()

Return the kqueue control file descriptor.

fromfd()

Create a kqueue object from a given control fd.

select.poll()

Returns a polling object.

This object supports registering and unregistering file descriptors, and then polling them for I/O events.

select.select(rlist, wlist, xlist, timeout=None, /)

Wait until one or more file descriptors are ready for some kind of I/O.

The first three arguments are iterables of file descriptors to be waited for: rlist – wait until ready for reading wlist – wait until ready for writing xlist – wait for an “exceptional condition” If only one kind of condition is required, pass [] for the other lists.

A file descriptor is either a socket or file object, or a small integer gotten from a fileno() method call on one of those.

The optional 4th argument specifies a timeout in seconds; it may be a floating point number to specify fractions of seconds. If it is absent or None, the call will never time out.

The return value is a tuple of three lists corresponding to the first three arguments; each contains the subset of the corresponding file descriptors that are ready.

* IMPORTANT NOTICE * On Windows, only sockets are supported; on Unix, all file descriptors can be used.