示例#1
0
    def _notify_pending(self, state):
        """Used to clear a pending response queue and request queue
        during connection drops."""
        if state == KeeperState.AUTH_FAILED:
            exc = AuthFailedError()
        elif state == KeeperState.EXPIRED_SESSION:
            exc = SessionExpiredError()
        else:
            exc = ConnectionLoss()

        while True:
            try:
                request, async_object, xid = self._pending.popleft()
                if async_object:
                    async_object.set_exception(exc)
            except IndexError:
                break

        while True:
            try:
                request, async_object = self._queue.popleft()
                if async_object:
                    async_object.set_exception(exc)
            except IndexError:
                break
示例#2
0
    def command(self, cmd=b'ruok'):
        """Sent a management command to the current ZK server.

        Examples are `ruok`, `envi` or `stat`.

        :returns: An unstructured textual response.
        :rtype: str

        :raises:
            :exc:`ConnectionLoss` if there is no connection open, or
            possibly a :exc:`socket.error` if there's a problem with
            the connection used just for this command.

        .. versionadded:: 0.5

        """
        if not self._live.is_set():
            raise ConnectionLoss("No connection to server")

        peer = self._connection._socket.getpeername()
        sock = self.handler.create_connection(peer,
                                              timeout=self._session_timeout /
                                              1000.0)
        sock.sendall(cmd)
        result = sock.recv(8192)
        sock.close()
        return result.decode('utf-8', 'replace')
示例#3
0
文件: zk.py 项目: mackong/izk
    def command(self, cmd='ruok'):
        """Sends a commmand to the ZK node.

        Overrides methode defined at
        https://github.com/python-zk/kazoo/blob/release/2.4/kazoo/client.py#L637
        as it could leave some data unread from the socket.

        """
        if not self._live.is_set():
            raise ConnectionLoss("No connection to server")
        out = []
        peer = self._connection._socket.getpeername()
        if len(peer) > 2:
            peer = peer[:2]
        sock = self.handler.create_connection(peer,
                                              timeout=self._session_timeout /
                                              1000.0)
        sock.sendall(cmd)
        while True:
            data = sock.recv(8192)
            if not data:
                break
            out.append(data)

        sock.close()
        result = b''.join(out)
        return result.decode('utf-8', 'replace')