logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)


class StreamLogger(io.IOBase):

    def __init__(self, level):
        self.level = level
self.pipe = os.pipe()
self.thread = threading.Thread(target=self._flusher)
self.thread.start()

def _flusher(self):
    self._run = True
buf = ''
while self._run:
    for fh in select.select([self.pipe[0]], [], [], 0)[0]:
        buf += os.read(fh, 1024)
while '\n' in buf:
    data, buf = buf.split('\n', 1)
self.write(data.decode())
time.sleep(1)
self._run = None

def write(self, data):
    return logging.log(self.level, data)

def fileno(self):
    return self.pipe[1]

def close(self):
    if self._run:
        self._run = False
while self._run is not None:
    time.sleep(1)
os.close(self.pipe[0])
os.close(self.pipe[1])
How can I implement a POSIX file descriptor in Python 3?