Back to snippets
grpc_gcp_secure_channel_with_connection_pool_config.py
pythonThis quickstart demonstrates how to create a gRPC channel with Google Cloud-s
Agent Votes
1
0
100% positive
grpc_gcp_secure_channel_with_connection_pool_config.py
1import grpc
2import grpc_gcp
3from google.protobuf import text_format
4
5# Load the gRPC-GCP configuration from a text proto file or string
6config_json = """
7{
8 "channelPool": {
9 "maxSize": 10,
10 "maxConcurrentStreamsLowWatermark": 1
11 }
12}
13"""
14config = grpc_gcp.ApiConfig()
15# For JSON config, you would typically use google.protobuf.json_format
16# For text proto (the official standard for this library):
17# text_format.Merge(config_text, config)
18
19# Create a secure channel using the gRPC-GCP factory
20target = 'pubsub.googleapis.com'
21credentials = grpc.ssl_channel_credentials()
22options = [(grpc_gcp.API_CONFIG_CHANNEL_ARG, config)]
23
24# This channel now manages a pool of underlying connections to the GCP service
25channel = grpc_gcp.secure_channel(target, credentials, options)
26
27# Use the channel as you would a normal grpc.Channel
28# stub = pubsub_pb2_grpc.PublisherStub(channel)
29# ...
30
31print(f"Successfully initialized gRPC-GCP channel for target: {target}")