spool.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:ave 作者: sonyxperiadev 项目源码 文件源码
def read(self, timeout=0):
        '''
        Read the contents of the spool. This may cause reads on the registered
        file descriptors.
        '''
        if not self.fds:
            raise Exception('no file descriptors registered')
        if timeout > 0:
            start = datetime.now()
        io = StringIO()
        stop = False
        while (not stop):
            stop = True
            if timeout > 0:
                limit = timeout - ((datetime.now() - start).microseconds / 1000)
                if limit < 0:
                    raise Exception('time out')
            else:
                limit = 0
            events = self.fds.poll(limit)
            if not events:
                raise Exception('time out')
            for e in events:
                if (e[1] & select.POLLIN
                or  e[1] & select.POLLPRI):
                    tmp = os.read(e[0], 4096)
                    if tmp:
                        io.write(tmp)
                        if self.log:
                            self.log.write(tmp)
                    continue
                if e[1] & select.POLLERR:
                    self.fds.unregister(e[0])
                    raise Exception('POLLERR')
                if e[1] & select.POLLHUP:
                    self.fds.unregister(e[0])
                    raise Exception('POLLHUP')
                if e[1] & select.POLLNVAL:
                    self.fds.unregister(e[0])
                    raise Exception('POLLNVAL')
        if self.log:
            self.log.flush()
        return io.getvalue()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号