Developing with Python

The Akka Serverless Python SDK offers an idiomatic Python language SDK for writing APIs and Services that will run in the Akka Serverless Platform-as-a-Service product. This page describes prerequisites for Python development and basic requirements for a development project. It is recommended that you check out the Akka Serverless documentation site for introduction to the product itself, including the concepts.

Lightbend provides Tier 3 support for the Python SDK. See an explanation of support tiers for more information. Further development will need to happen to take the Python support to Tier 2. Use of this SDK in a production environment is not recommended. Please visit https://github.com/jpollock/akkaserverless-python-sdk/discussions to add ideas or ask questions specific to the Python SDK.

Your development project needs to include the Akka Serverless Python SDK. You define API components using protobuf and use protoc to compile them. Finally, you implement business logic for service components.

Prerequisites

The following are required to develop Akka Serverless services in Python:

Python

Akka Serverless requires at least Python 3. Download and install Python here new tab.

Docker

Akka Serverless requires Docker new tab 19.03 for building your service images. Most popular build tools have plugins that assist in building Docker images.

Development project requirements

The following examples show how to install the SDK for use in your Python code.

If you are starting from scratch, you will need to add configuration and code to:

Add SDK libraries

Once you have the prerequisites, you need to add the @lightbend/akkaserverless-python-sdk package to your service development project, which can be done by running:

pip install akkaserverless

You can also use the requirements.txt file found in the samples and starter projects.

pip install -r requirements.txt

To create a basic service, you need to include the akkaserverless package dependencies in your project, define gRPC descriptors in .proto files and reference them in your project, compile the .proto files, and have logic to start the gRPC server in your source code.

Define gRPC descriptors and compile

Descriptors for gRPC are defined in protobuf files. You can place protobuf files in your project wherever you like, for example, in the root directory, or in a directory named protos. For the Python SDK, right now, it is recommended to keep in the root directory. See Writing gRPC descriptors for more details.

Precompile the protobuf descriptor set

The gRPC descriptor is serialized to binary using the Protobuf FileDescriptorSet message type. In order to use protobuf in the Python SDK you need to generate the Python stubs. This can be done by using the compile.sh script, which is located in the bin directory of the SDK repository itself; when installing the SDK, if you do so within a virtual environtment (using https://docs.python.org/3/library/venv.html or https://github.com/pyenv/pyenv-virtualenv), the script will be located in the path of your terminal and you should be able to type compile.sh directly, from the root of the project.

Create and start the gRPC server

To create a server, you can use the AkkaServerlessnew tab class, add one or more entities to it, and then invoke startnew tab, like so:

# create service and add components
service = AkkaServerlessService()
service.add_component(customer_value_entity)
service.add_component(customer_value_entity_view)
service.add_component(customer_eventsourced_entity)
service.add_component(customer_eventsourced_entity_view)
service.start()