Пример #1
0
    def __init__(self, streamer, name, callback_time=1., io_loop=None):
        super(SocketStatsCollector, self).__init__(streamer, name,
                callback_time, io_loop)
        # if gevent is installed, we'll use a greenlet,
        # otherwise we'll use a thread
        try:
            import gevent       # NOQA
            self.greenlet = True
        except ImportError:
            self.greenlet = False

        self._rstats = defaultdict(int)
        if self.greenlet:
            self.poller = SelectIOWait()
        else:
            self.poller = IOWait()

        for sock, address in self.streamer.get_sockets():
            self.poller.watch(sock, read=True, write=False)

        self._p = ioloop.PeriodicCallback(self._select, 1, io_loop=io_loop)
Пример #2
0
    def __init__(self, streamer, name, callback_time=1., io_loop=None):
        super(SocketStatsCollector, self).__init__(streamer, name,
                                                   callback_time, io_loop)
        # if gevent is installed, we'll use a greenlet,
        # otherwise we'll use a thread
        try:
            import gevent  # NOQA
            self.greenlet = True
        except ImportError:
            self.greenlet = False

        self._rstats = defaultdict(int)
        if self.greenlet:
            self.poller = SelectIOWait()
        else:
            self.poller = IOWait()

        for sock, address, fd in self.streamer.get_sockets():
            self.poller.watch(sock, read=True, write=False)

        self._p = ioloop.PeriodicCallback(self._select,
                                          _LOOP_RES,
                                          io_loop=io_loop)
Пример #3
0
class SocketStatsCollector(BaseStatsCollector):

    def __init__(self, streamer, name, callback_time=1., io_loop=None):
        super(SocketStatsCollector, self).__init__(streamer, name,
                callback_time, io_loop)
        # if gevent is installed, we'll use a greenlet,
        # otherwise we'll use a thread
        try:
            import gevent       # NOQA
            self.greenlet = True
        except ImportError:
            self.greenlet = False

        self._rstats = defaultdict(int)
        if self.greenlet:
            self.poller = SelectIOWait()
        else:
            self.poller = IOWait()

        for sock, address in self.streamer.get_sockets():
            self.poller.watch(sock, read=True, write=False)

        self._p = ioloop.PeriodicCallback(self._select, 1, io_loop=io_loop)

    def start(self):
        # starting the thread or greenlet
        self._p.start()
        super(SocketStatsCollector, self).start()

    def stop(self):
        self._p.stop()
        BaseStatsCollector.stop(self)

    def _select(self):
        # polling for events
        try:
            events = self.poller.wait(_RESOLUTION)
        except ValueError:
            return

        if len(events) == 0:
            return

        for socket, read, write in events:
            if read:
                self._rstats[socket.fileno()] += 1

    def _aggregate(self, aggregate):
        raise NotImplementedError()

    def collect_stats(self):
        # sending hits by sockets
        sockets = self.streamer.get_sockets()

        if len(sockets) == 0:
            yield None
        else:
            fds = [(address, sock.fileno()) for sock, address in sockets]
            total = {'addresses': [], 'reads': 0}

            # we might lose a few hits here but it's ok
            for address, fd in fds:
                info = {}
                info['fd'] = info['subtopic'] = fd
                info['reads'] = self._rstats[fd]
                total['reads'] += info['reads']
                total['addresses'].append(address)
                info['address'] = address
                self._rstats[fd] = 0
                yield info

            yield total
Пример #4
0
class SocketStatsCollector(BaseStatsCollector):
    def __init__(self, streamer, name, callback_time=1., io_loop=None):
        super(SocketStatsCollector, self).__init__(streamer, name,
                                                   callback_time, io_loop)
        # if gevent is installed, we'll use a greenlet,
        # otherwise we'll use a thread
        try:
            import gevent  # NOQA
            self.greenlet = True
        except ImportError:
            self.greenlet = False

        self._rstats = defaultdict(int)
        if self.greenlet:
            self.poller = SelectIOWait()
        else:
            self.poller = IOWait()

        for sock, address, fd in self.streamer.get_sockets():
            self.poller.watch(sock, read=True, write=False)

        self._p = ioloop.PeriodicCallback(self._select,
                                          _LOOP_RES,
                                          io_loop=io_loop)

    def start(self):
        # starting the thread or greenlet
        self._p.start()
        super(SocketStatsCollector, self).start()

    def stop(self):
        self._p.stop()
        BaseStatsCollector.stop(self)

    def _select(self):
        # polling for events
        try:
            events = self.poller.wait(_RESOLUTION)
        except ValueError:
            return

        if len(events) == 0:
            return

        for socket, read, write in events:
            if read:
                self._rstats[socket.fileno()] += 1

    def _aggregate(self, aggregate):
        raise NotImplementedError()

    def collect_stats(self):
        # sending hits by sockets
        sockets = self.streamer.get_sockets()

        if len(sockets) == 0:
            yield None
        else:
            fds = [(address, sock.fileno(), fd)
                   for sock, address, fd in sockets]
            total = {'addresses': [], 'reads': 0}

            # we might lose a few hits here but it's ok
            for address, monitored_fd, fd in fds:
                info = {}
                info['fd'] = info['subtopic'] = fd
                info['reads'] = self._rstats[monitored_fd]
                total['reads'] += info['reads']
                total['addresses'].append(address)
                info['address'] = address
                self._rstats[monitored_fd] = 0
                yield info

            yield total