Ejemplo n.º 1
0
def main():
    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        process(stub)
Ejemplo n.º 2
0
def run_client(channel_compression, call_compression, target):
    with grpc.insecure_channel(target,
                               compression=channel_compression) as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'),
                                 compression=call_compression,
                                 wait_for_ready=True)
        print("Response: {}".format(response))
Ejemplo n.º 3
0
def main():
    # For more channel options, please see https://grpc.io/grpc/core/group__grpc__arg__keys.html
    with grpc.insecure_channel(target='localhost:50051',
                               options=[('grpc.lb_policy_name', 'pick_first'),
                                        ('grpc.enable_retries', 0),
                                        ('grpc.keepalive_timeout_ms', 10000)
                                        ]) as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
    print("Greeter client received: " + response.message)
def send_rpc(channel):
    stub = helloworld_pb2_grpc.GreeterStub(channel)
    request = helloworld_pb2.HelloRequest(name='you')
    try:
        response = stub.SayHello(request)
    except grpc.RpcError as rpc_error:
        _LOGGER.error('Received error: %s', rpc_error)
        return rpc_error
    else:
        _LOGGER.info('Received message: %s', response)
        return response
Ejemplo n.º 5
0
def run():
    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response, call = stub.SayHello.with_call(
            helloworld_pb2.HelloRequest(name='you'),
            metadata=(
                ('initial-metadata-1', 'The value should be str'),
                ('binary-metadata-bin',
                 b'With -bin surffix, the value can be bytes'),
                ('accesstoken', 'gRPC Python is great'),
            ))

    print("Greeter client received: " + response.message)
    for key, value in call.trailing_metadata():
        print('Greeter client received trailing metadata: key=%s value=%s' %
              (key, value))
def main():
    # Pick a random free port
    with get_free_loopback_tcp_port() as server_address:
        # Register connectivity event to notify main thread
        transient_failure_event = threading.Event()

        def wait_for_transient_failure(channel_connectivity):
            if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE:
                transient_failure_event.set()

        # Create gRPC channel
        channel = grpc.insecure_channel(server_address)
        channel.subscribe(wait_for_transient_failure)
        stub = helloworld_pb2_grpc.GreeterStub(channel)

        # Fire an RPC without wait_for_ready
        thread_disabled_wait_for_ready = threading.Thread(target=process,
                                                          args=(stub, False))
        thread_disabled_wait_for_ready.start()
        # Fire an RPC with wait_for_ready
        thread_enabled_wait_for_ready = threading.Thread(target=process,
                                                         args=(stub, True))
        thread_enabled_wait_for_ready.start()

    # Wait for the channel entering TRANSIENT FAILURE state.
    transient_failure_event.wait()
    server = create_server(server_address)
    server.start()

    # Expected to fail with StatusCode.UNAVAILABLE.
    thread_disabled_wait_for_ready.join()
    # Expected to success.
    thread_enabled_wait_for_ready.join()

    # server.wait_for_termination()

    server.stop(None)
    channel.close()
Ejemplo n.º 7
0
def main():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
    print("Greeter client received: " + response.message)
Ejemplo n.º 8
0
def run(addr, n):
    with grpc.insecure_channel(addr) as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        request = helloworld_pb2.HelloRequest(name='you')
        for _ in range(n):
            process(stub, request)
Ejemplo n.º 9
0
def run(server_address):
    with grpc.insecure_channel(server_address) as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
    print("Greeter client received: " + response.message)