The documentation you are viewing is for Dapr v1.6 which is an older version of Dapr. For up-to-date documentation, see the latest version.

开始使用 Dapr actor Python SDK

如何使用 Dapr Python SDK 启动和运行

Dapr actor 包允许您从 Python 应用程序中与 Dapr virtual actor 进行交互。

前提

Actor 接口

该接口定义了 actor 契约,该契约在 actor 实现和调用 actor 的客户端之间共享。 因为客户端可能依赖于它,所以通常在一个与 actor 实现分开的程序集中定义它是有意义的。

from dapr.actor import ActorInterface, actormethod

class DemoActorInterface(ActorInterface):
    @actormethod(name="GetMyData")
    async def get_my_data(self) -> object:
        ...

Actor 服务

Actor 服务承载着虚拟 Actor。 它实现了一个派生自基类型 Actor 的类,并实现了 actor 接口中定义的接口。

可以使用以下 Dapr Actor 扩展之一创建 Actor:

Actor 客户端

Actor 客户端包含调用 Actor 接口中定义的 Actor 方法的 Actor 客户端实现。

import asyncio

from dapr.actor import ActorProxy, ActorId
from demo_actor_interface import DemoActorInterface

async def main():
    # Create proxy client
    proxy = ActorProxy.create('DemoActor', ActorId('1'), DemoActorInterface)

    # Call method on client
    resp = await proxy.GetMyData()

示例

请访问 本页 以获得可运行的 Actor 示例。