gRPC extensions for Google Cloud Platform

0.2.2 · maintenance · verified Sun Apr 05

grpcio-gcp provides gRPC extensions specifically designed for Google Cloud Platform. It allows developers to leverage GCP-specific features with gRPC client libraries, including channel pooling and configuration management. The latest release, 0.2.2, was published on September 10, 2018. While the library itself hasn't seen recent releases, its functionality is referenced and integrated into other Google client libraries, suggesting an ongoing maintenance posture for compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `ChannelPool` using `grpcio-gcp` and an `ApiConfig`. For actual usage, you would need compiled protobuf stubs for your specific Google Cloud service and a properly defined API configuration. Authentication typically relies on `GOOGLE_APPLICATION_CREDENTIALS`.

import os
from grpc_gcp import ChannelPool
from grpc_gcp.proto import api_config_pb2
import grpc

# NOTE: This is a conceptual example. For a real GCP service, you would need
# compiled protobufs for that service and proper API configuration.
# This example assumes a 'spanner.grpc.config' file exists for demonstration.
# In a real scenario, you'd replace 'spanner.grpc.config' with your actual
# service configuration and define the correct stub/client.

# Ensure GOOGLE_APPLICATION_CREDENTIALS is set for authentication
# For local development, set this environment variable:
# export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/key.json"
# Or ensure gcloud is authenticated: gcloud auth application-default login

# Example API configuration (replace with your service's actual config)
# Content of 'spanner.grpc.config' (as mentioned in PyPI usage):
# channel_pool: {
#   max_size: 10
#   max_concurrent_streams_low_watermark: 1
# }
# method: {
#   name: "/google.spanner.v1.Spanner/*"
#   per_rpc_timeout_millis: 10000
#   max_retries: 3
# }

if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS'):
    print("Warning: GOOGLE_APPLICATION_CREDENTIALS not set. Auth may fail.")

try:
    # Load configuration from a file (example placeholder)
    # In a real application, you would create or load a meaningful ApiConfig
    api_config = api_config_pb2.ApiConfig()
    # Assume a config file exists for a service, e.g., 'spanner.grpc.config'
    # This part is illustrative; actual loading depends on your config source
    # For this example, we'll manually set some values for demonstration
    api_config.channel_pool.max_size = 5

    # Create a ChannelPool instance
    # The target would be the address of your gRPC service, e.g., 'spanner.googleapis.com:443'
    target_service_address = 'localhost:50051' # Replace with actual service address
    with ChannelPool(target_service_address, api_config) as channel_pool:
        print(f"Created ChannelPool for {target_service_address}")
        # You can now get a channel from the pool and use it with a gRPC stub
        channel = channel_pool.get_channel()
        print(f"Obtained channel: {channel}")

        # Example of using the channel (requires a generated stub)
        # For a real service, you would import and use its generated stub:
        # import your_service_pb2_grpc
        # stub = your_service_pb2_grpc.YourServiceStub(channel)
        # response = stub.YourMethod(your_service_pb2.YourRequest(...))
        # print(response)

    print("ChannelPool demonstration complete.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have a gRPC service running at the target address and correct authentication.")

view raw JSON →