Getting started with the Dapr client Python SDK
How to get up and running with the Dapr Python SDK
The Dapr client package allows you to interact with other Dapr applications from a Python application.
前提
- 安装 Dapr CLI
- 初始化的 Dapr 环境
- Python 3.7+ installed
- Dapr Python module installed
Import the client package
The dapr package contains the DaprClient
which will be used to create and use a client.
from dapr.clients import DaprClient
构建块
The Python SDK allows you to interface with all of the Dapr building blocks.
调用服务
from dapr.clients import DaprClient
with DaprClient() as d:
resp = d.invoke_service(id='service-to-invoke', method='method-to-invoke', data='{"message":"Hello World"}')
- 有关服务调用的完整指南,请访问 如何:调用服务。
- Visit Python SDK examples for code samples and instructions to try out service invocation
保存 & 获取 应用程序状态
from dapr.clients import DaprClient
with DaprClient() as d:
# Save state
d.save_state(store_name="statestore", key="key1", value="value1")
# Get state
data = d.get_state(store_name="statestore", key="key1").data
# Delete state
d.delete_state(store_name="statestore", key="key1")
- 有关状态操作的完整列表,请访问 如何:获取 & 保存 状态。。
- Visit Python SDK examples for code samples and instructions to try out state management
发布消息
from dapr.clients import DaprClient
with DaprClient() as d:
resp = d.publish_event(pubsub_name='pubsub', topic='TOPIC_A', data='{"message":"Hello World"}')
- 有关状态操作的完整列表,请访问 如何: 发布 & 订阅。
- Visit Python SDK examples for code samples and instructions to try out pub/sub
与输出绑定交互
from dapr.clients import DaprClient
with DaprClient() as d:
resp = d.invoke_binding(name='kafkaBinding', operation='create', data='{"message":"Hello World"}')
- 有关输出绑定的完整指南,请访问 如何:使用绑定。
- Visit Python SDK examples for code samples and instructions to try out output bindings
检索密钥
from dapr.clients import DaprClient
with DaprClient() as d:
resp = d.get_secret(store_name='localsecretstore', key='secretKey')
- 有关密钥的完整指南,请访问如何:检索密钥。
- Visit Python SDK examples for code samples and instructions to try out retrieving secrets