本文介绍 Redis Python SDK 的代码示例供您参考。
使用 Python SDK 调用 Redis 接口时,默认无超时时间限制。
您可以在发起接口调用请求时,在代码的最后加上 _request_timeout
参数来指定请求的超时时间(单位:秒),若未在该时间内完成,请求即会终止并报超时错误。
例如,您可以在调用 DescribeDBInstanceDetail 接口时,通过如下代码将超时时间设置为 10 秒。
resp = api_instance.describe_db_instance_detail(volcenginesdkredis.DescribeDBInstanceDetailRequest( instance_id="redis-cn04giei8c91*****", ), _request_timeout=10)
说明
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。
import volcenginesdkredis import volcenginesdkcore from volcenginesdkcore.rest import ApiException from pprint import pprint if __name__ == "__main__": configuration = volcenginesdkcore.Configuration() configuration.ak = "Your AK" configuration.sk = "Your SK" configuration.region = "Your Region" try: api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration)) resp = api_instance.create_db_instance(volcenginesdkredis.CreateDBInstanceRequest( region_id="cn-beijing", zone_ids=["cn-beijing-a"], instance_name="python_sdk_shardedcluster_test", sharded_cluster=1, password="Pwd@12****", node_number=2, shard_number=2, shard_capacity=8192, engine_version="5.0", vpc_id="vpc-1g0ckv3hu4s1s8ibuxwqe****", subnet_id="subnet-1g0ckvr67hfr48ibuxxgr****", )) pprint(resp) except ApiException as e: print("Exception when calling REDISApi->create_db_instance: %s\n" % e)
import volcenginesdkredis import volcenginesdkcore from volcenginesdkcore.rest import ApiException from pprint import pprint if __name__ == "__main__": configuration = volcenginesdkcore.Configuration() configuration.ak = "Your AK" configuration.sk = "Your SK" configuration.region = "Your Region" try: api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration)) resp = api_instance.create_db_instance(volcenginesdkredis.CreateDBInstanceRequest( region_id="cn-beijing", zone_ids=["cn-beijing-a"], instance_name="python_sdk_test", sharded_cluster=0, password="Pwd@12****", node_number=2, shard_capacity=8192, engine_version="5.0", vpc_id="vpc-1g0ckv3hu4s1s8ibuxwqe****", subnet_id="subnet-1g0ckvr67hfr48ibuxxgr****", )) pprint(resp) except ApiException as e: print("Exception when calling REDISApi->create_db_instance: %s\n" % e)
说明
关于下述 SDK 请求中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail。
import volcenginesdkredis import volcenginesdkcore from volcenginesdkcore.rest import ApiException from pprint import pprint if __name__ == "__main__": configuration = volcenginesdkcore.Configuration() configuration.ak = "Your AK" configuration.sk = "Your SK" configuration.region = "Your Region" try: api_instance = volcenginesdkredis.REDISApi(volcenginesdkcore.ApiClient(configuration)) # 查询指定 Redis 实例信息 resp = api_instance.describe_db_instance_detail(volcenginesdkredis.DescribeDBInstanceDetailRequest( instance_id="redis-cn04giei8c91*****", )) pprint(resp) except ApiException as e: print("Exception when calling REDISApi->describe_db_instance_detail: %s\n" % e)