Creating a python service

How to create a python service and leverage dependencies

Creating services

Configuring your service

If you haven’t already done so, please see the getting started page for details on how to configure your service.

Configuring python as the primary initialization language for your service is as simple as updating your architect.json file with the proper language and main file:

{
  "language": "python",
  "main": "main.py"
}

The language specification tells Architect which service launcher to use for the service, and the main file indicates which file should be enriched with environment-specific dependency stubs.

The main file

The main file cited in your service configuration is where Architect will inject references to dependencies. For javascript services, the main file must be structured as follows in order to receive dependencies:

class MyService():
  dependencies = ['dependency-1', 'dependency-2', /* ... */]

  def __init__(self, dependency_1, dependency_2, /* ... */):
    self.dependency_1 = dependency_1
    self.dependency_2 = dependency_2
    # ...

  # ...

Populating GRPC service methods

Architect uses GRPC and Protocol Buffers to define and generate services. By defining a .proto file with your service inside, you can create methods in your service’s main file that will be automatically available to other services.

Proto file

syntax = "proto3";

message RecommendationRequest {
  string user_id = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message RecommendationResponse {
  // ...
}

service MovieService {
  rpc RecommendationsByUserId (RecommendationRequest) returns (RecommendationResponse) {}
}

Main file

# Architect will setup an instance of MyService and inject dependency stubs
# via the constructor.
class MyService():
  # ...
  
  def RecommendationsByUserId(self, request, context):
    user_id = request.user_id
    page_number = request.page_number
    results_per_page = request.results_per_page
    
    # Make calls to dependencies as needed....
    dependency_request = self.dependency_service['messages'].DependencyRequest()
    dependency_response = self.dependency_service['client'].CallDependency(dependency_request)
    
    recommendation_response = self.recommendation_service['messages'].RecommendationResponse()
    return recommendation_response