Class CompletionQueue

Class Documentation

class CompletionQueue

Notifies an event loop of completed futures, without the Python GIL.

Worker threads push the ID of each completed future. Each push signals either a file descriptor that the event loop watches for readability (an eventfd on Linux, a pipe on other POSIX systems) or, for a queue constructed with a socket, sends one byte on that socket (one end of a socket pair whose other end the event loop reads; this also works on Windows). The event loop then takes the IDs with drain, and looks up the futures it is waiting for.

A CompletionQueue is a handle: copies share one queue, which is closed when close is called or the last copy is destroyed. Futures keep a copy, so worker threads never refer to Python objects. close closes the descriptor or the socket under the queue’s lock, so a worker never signals a closed or reused descriptor, and later pushes are ignored.

Public Functions

CompletionQueue()

Signals completions through a file descriptor. Not available on Windows; use a socket there.

explicit CompletionQueue(uintptr_t socket)

Signals completions by sending one byte on socket, whose ownership is taken. The socket is made non-blocking.

int fileno() const

The file descriptor that becomes readable when futures complete. -1 when signalling through a socket, or after close.

void push(uint64_t id) const

Records the ID of a completed future and signals the event loop. Called by worker threads. Ignored after close.

std::vector<uint64_t> drain() const

Takes the IDs recorded since the last call, in completion order.

void close() const

Closes the descriptor (or the socket) and drops the IDs not drained.