Dapr Python SDK integration with FastAPI

How to create Dapr Python virtual actors with the FastAPI extension

The Dapr Python SDK provides integration with FastAPI using the dapr-ext-fastapi module

Installation

You can download and install the Dapr FastAPI extension module with:

  1. pip install dapr-ext-fastapi

Note

  1. The development package will contain features and behavior that will be compatible with the pre-release version of the Dapr runtime. Make sure to uninstall any stable versions of the Python SDK extension before installing the dapr-dev package.
  1. pip install dapr-ext-fastapi-dev

Example

  1. from fastapi import FastAPI
  2. from dapr.ext.fastapi import DaprActor
  3. from demo_actor import DemoActor
  4. app = FastAPI(title=f'{DemoActor.__name__}Service')
  5. # Add Dapr Actor Extension
  6. actor = DaprActor(app)
  7. @app.on_event("startup")
  8. async def startup_event():
  9. # Register DemoActor
  10. await actor.register_actor(DemoActor)
  11. @app.get("/GetMyData")
  12. def get_my_data():
  13. return "{'message': 'myData'}"