import fcntl import os import select NUM_WRITERS = 20 MAXEVENTS = 5 def set_nonblocking(fd): flags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK|flags) print("Working with {} FDs, {} maxevents".format(NUM_WRITERS, MAXEVENTS)) all_writers = set() seen_writers = set() ep = select.epoll() try: for i in range(NUM_WRITERS): r, w = os.pipe() all_writers.add(w) set_nonblocking(r) set_nonblocking(w) ep.register(w, select.EPOLLOUT) while all_writers - seen_writers: print(sorted(all_writers - seen_writers)) for (fd, event) in ep.poll(-1, MAXEVENTS): if event & select.POLLOUT: seen_writers.add(fd) try: while True: os.write(fd, b'X' * 512) except OSError: pass finally: ep.close() assert seen_writers == all_writers