Exposing a Python function

You can expose any Python function as a endpoint on the API node. Calling the endpoint will call your function with the parameters you specify and return the results of the function.

This might look similar to the Python prediction endpoint, but there are a few key differences:

  • A Python prediction endpoint has a strict concept of input records, and output prediction. It must output a prediction, and thus can only be used for prediction-like use cases. In contrast, a Python function can do any kind of action and return any form of result (or even no result). For example, you can use a Python function endpoint to store data in a database, a file, …
  • Since there is no concept of input records, you cannot use the dataset-based enrichment features in a Python function endpoint

Creating the Python function endpoint

To create a custom prediction endpoint, start by creating a service. (See Your first API service for more information). Then, create an endpoint of type “Custom function (Python)”.

DSS prefills the Code part with a sample

Structure of the code

The code of a Python function endpoint must include at least one function, whose name must be entered in the “Settings” tab.

The parameters of the function will be automatically filled from the parameters passed in the endpoint call.

The result of the function must be JSON-serializable.

Using managed folders

A Python function endpoint can optionally reference one or several DSS managed folders. When you package your service, the contents of the folders are bundled with the package, and your custom code receives the paths to the managed folder contents.

The paths to the managed folders (in the same order as defined in the UI) is available in a folders global variable which is transparently available in your code.

For example, the following code loads a pickle file from the 2nd managed folder at startup, and uses it in the my_api_function API function

import cPickle as pickle
import os.path

folder_path = folders[1]
file_path = os.path.join(folder_path, "mydata.pkl")

with open(file_path) as f:
        data = pickle.load(f)

def my_api_function(myparam):
        return data.do_something(myparam)

Testing your code

Developing a custom model implies testing often. To ease this process, a “Development server” is integrated in the DSS UI.

To test your code, click on the “Deploy to Dev Server” button. The dev server starts and load your model. You are redirected to the Test tab where you can see whether your model loads.

You can also define Test queries, i.e. JSON objects akin to the ones that you would pass to the API node user API. When you click on the “Play test queries” button, the test queries are sent to the dev server, and the result is printed.

R packages

We strongly recommend that you use code environments for deploying custom model packages if these packages use any external (not bundled with DSS) library

Server-side tuning

It is possible to tune the behavior of Python prediction endpoints on the API node side. You can tune how many concurrent requests your API node can handle. This depends mainly on your model (its speed and in-memory size) and the available resources on the server running the API node.

You can configure the parallelism parameters for an endpoint by creating a JSON file in the config/services folder in the API node’s data directory.

mkdir -p config/services/<SERVICE_ID>

Then create or edit the config/services/<SERVICE_ID>/<ENDPOINT_ID>.json file

This file must have the following structure and be valid JSON:

{
    "pool" : {
        "floor" : 1,
        "ceil" : 8,
        "cruise": 2,
        "queue" : 16,
        "timeout" : 10000
    }

}

This configuration allows you to control the number of allocated pipelines.

One allocated pipeline means one Python process running your code, preloaded with your initialization code, and ready to serve a prediction request. If you have 2 allocated pipelines (meaning 2 Python processes), 2 requests can be handled simultaneously, other requests will be queued until one of the pipelines is freed (or the request times out). When the queue is full, additional requests are rejected.

Those parameters are all positive integers:

  • floor (default: 1): Minimum number of pipelines. Those are allocated as soon as the endpoint is loaded.
  • ceil (default: 8): Maximum number of allocated pipelines at any given time. Additional requests will be queued. ceil floor
  • cruise (default: 2): The “nominal” number of allocated pipelines. When more requests come in, more pipelines may be allocated up to ceil. But when all pending requests have been completed, the number of pipeline may go down to cruise. floor cruise ceil
  • queue (default: 16): The number of requests that will be queued when ceil pipelines are already allocated and busy. The queue is fair: first received request will be handled first.
  • timeout (default: 10000): Time, in milliseconds, that a request may spend in queue wating for a free pipeline before being rejected.

Each Python process will only serve a single request at a time.

It is important to set “cruise”:

  • At a high-enough value to serve your expected reasonable peak traffic. If you set cruise too low, DSS will kill excedental Python processes, and will need to recreate a new one just afterwards.
  • But also at a not-too-high value, because each pipeline implies a running Python process consuming the memory required by the model.

You can also deploy your service on multiple servers, see High availability and scalability.