Пример #1
0
def server_bidirectional_stream_call_one_by_one():
    """
    双向流模式,客户端处理,一个请求对应一个响应的情况
    通过队列传递交互传递参数
    """
    # 清空队列
    TEMP_QUEUE.queue.clear()

    # 处理
    _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1',
                                                          port=50051)
    with SimpleGRpcTools.generate_channel(_connect_para) as channel:
        # # 注意队列一定要先传入一个值
        TEMP_QUEUE.put(None)
        _cresult_iterator = SimpleGRpcTools.grpc_call_by_channel(
            channel,
            bidirectional_stream_one_by_one_generator(),
            call_mode=EnumCallMode.BidirectionalStream)
        for _cresult in _cresult_iterator:
            # 放入队列
            s = _cresult.return_json
            TEMP_QUEUE.put(s)
            if not _cresult.is_success():
                return False
    return True
Пример #2
0
def server_bidirectional_stream_call_n_by_one():
    """
    双向流模式,客户端处理,多个请求对应一个响应的情况
    """
    # 处理
    _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1',
                                                          port=50051)
    with SimpleGRpcTools.generate_channel(_connect_para) as channel:
        _cresult_iterator = SimpleGRpcTools.grpc_call_by_channel(
            channel,
            bidirectional_stream_n_by_one_generator(),
            call_mode=EnumCallMode.BidirectionalStream)
        for _cresult in _cresult_iterator:
            # 打印
            print('bidirectional_stream_n_by_one client get: ' +
                  _cresult.return_json)
            if not _cresult.is_success():
                return False
    return True
Пример #3
0
    def __init__(self, connect_para):
        """
        构造函数

        @param {NullObj} connect_para - 具体参数见SimpleGRpcConnection.generate_connect_para函数的定义

        @throws {ConnectionError} - 如果连接时需要检查有效性,当检查失败时抛出该异常
        """
        # 内部变量初始化
        self._trace_info_dict = dict()

        # 初始化参数
        self._connect_para = connect_para
        self._fill_connect_para(self._connect_para)
        self._test_on_connect = connect_para.test_on_connect
        self._test_use_health_check = connect_para.test_use_health_check
        self._servicer_name = connect_para.servicer_name
        self._logger = connect_para.logger
        self._log_level = connect_para.log_level
        self._timeout = connect_para.timeout

        # 日志处理
        if self._logger is None and connect_para.is_use_global_logger:
            # 使用全局logger
            self._logger = RunTool.get_global_logger()

        # 进行连接
        self._channel = SimpleGRpcTools.generate_channel(self._connect_para)
        self._stub = SimpleGRpcTools.generate_call_stub(self._channel)

        # 检查连接有效性
        if self._test_on_connect:
            _check_result = self.test()
            if _check_result.status != msg_pb2.HealthResponse.SERVING:
                # 连接失败,打印日志后抛出异常
                if self._logger is not None:
                    self._logger.log(
                        self._log_level, '[EX:%s]%s%s\n%s' %
                        (_check_result.error,
                         'SimpleGRpcConnection init error: ',
                         _check_result.msg, _check_result.trace_str))
                raise ConnectionError(_check_result.msg)
Пример #4
0
def server_side_stream_call(a, b, *args, **kwargs):
    """
    服务端流模式,真正调用服务端的函数
    """
    # 转换参数
    _para_values = RunTool.get_current_function_parameter_values(
        is_simple_mode=True)
    _para_obj = SimpleGRpcTools.parameters_to_json(_para_values)
    _req_obj = SimpleGRpcTools.generate_request_obj(
        service_name='service_server_side_stream',
        para_json=_para_obj.para_json,
        has_para_bytes=_para_obj.has_para_bytes,
        para_bytes=_para_obj.para_bytes)
    _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1',
                                                          port=50051)
    with SimpleGRpcTools.generate_channel(_connect_para) as channel:
        _cresult_iterator = SimpleGRpcTools.grpc_call_by_channel(
            channel, _req_obj, call_mode=EnumCallMode.ServerSideStream)
        for _cresult in _cresult_iterator:
            if not _cresult.is_success():
                return False
    return True
Пример #5
0
    def reconnect(self):
        """
        重新连接

        @returns {CResult} - 响应对象,判断成功的方法:
            ret.status == msg_pb2.HealthResponse.SERVING
            总共有以下几种状态
            health_pb2.HealthResponse.UNKNOWN
            health_pb2.HealthResponse.SERVICE_UNKNOWN
            health_pb2.HealthResponse.NOT_SERVING
            health_pb2.HealthResponse.SERVING
        """
        # 先关闭连接
        self.close()

        # 进行连接
        self._channel = SimpleGRpcTools.generate_channel(self._connect_para)
        self._stub = SimpleGRpcTools.generate_call_stub(self._channel)

        # 检查连接有效性
        if self._test_on_connect:
            _check_result = self.test()
            if not _check_result.is_success(
            ) or _check_result.status != msg_pb2.HealthResponse.SERVING:
                # 连接失败,打印日志后抛出异常
                if self._logger is not None:
                    self._logger.log(
                        self._log_level, '[EX:%s]%s%s, service status:%s\n%s' %
                        (_check_result.error,
                         'SimpleGRpcConnection reconnect error: ',
                         _check_result.msg, _check_result.status,
                         _check_result.trace_str))
            return _check_result
        else:
            # 不检查的情况,直接返回成功,连接状态为SERVING
            _check_result = CResult('00000')
            _check_result.status = msg_pb2.HealthResponse.SERVING
            return _check_result