Python 3 language support for Dispatch
Latest image on Docker Hub
You need a recent version of Dispatch installed in your Kubernetes cluster, Dispatch CLI configured to use it.
To add the base-image to Dispatch:
$ dispatch create base-image python3-base dispatchframework/python3-base:<tag>
Make sure the base-image status is READY
(it normally goes from INITIALIZED
to READY
):
$ dispatch get base-image python3-base
Library dependencies listed in requirements.txt
(pip dependency manifest) need to be wrapped into a Dispatch image. For example, suppose we need a HTTP library:
$ cat ./requirements.txt
requests
$ dispatch create image python3-mylibs python3-base --runtime-deps ./requirements.txt
Make sure the image status is READY
(it normally goes from INITIALIZED
to READY
):
$ dispatch get image python3-mylibs
Using the python3 base-image, you can create Dispatch functions from python source files. The file can require any libraries from the image (see above).
The only requirement is: a function called handle
must be defined that accepts 2 arguments (context
and payload
), for example:
$ cat ./http_func.py
import requests
def handle(context, payload):
url = payload.get("url", "http://example.com")
resp = requests.get(url)
return {"status": resp.status_code}
$ dispatch create function http ./http_func.py --image=python3-mylibs --handler=http_func.handle
Make sure the function status is READY
(it normally goes from INITIALIZED
to READY
):
$ dispatch get function http
As usual:
$ dispatch exec --json --input '{"url": "http://dispatchframework.io"}' --wait http
{
"blocking": true,
"executedTime": 1524768025,
"faasId": "09e3c92a-a9e5-438c-b627-84a100c041d5",
"finishedTime": 1524768025,
"functionId": "6f5d49a6-3a5a-43d2-ab60-7bef6b3d0b71",
"functionName": "http",
"input": {
"url": "http://dispatchframework.io"
},
"logs": {
"stderr": null,
"stdout": null
},
"name": "e7ef9a88-ff3e-48b6-a331-b350dbde293e",
"output": {
"status": 200
},
"reason": null,
"secrets": [],
"services": null,
"status": "READY",
"tags": []
}
There are three types of errors that can be thrown when invoking a function:
InputError
FunctionError
SystemError
SystemError
represents an error in the Dispatch infrastructure. InputError
represents an error in the input detected either early in the function itself or through input schema validation. FunctionError
represents an error in the function logic or an output schema validation error.
Functions themselves can either throw InputError
or FunctionError
For Python, the following exceptions thrown from the function are considered InputError
:
ValueError
TypeError
All other exceptions thrown from the function are considered FunctionError
.
To validate input in the function body:
def lower(ctx, payload):
if type(payload) is not str:
raise TypeError("payload is not of type str")
return payload.lower()
Since ValueError
and TypeError
are considered InputError
, functions should not throw them unless explicitly thrown due to an input validation error. Functions should catch and handle them accordingly if they should not be classified as InputError
.
To run tests, ensure you have all the required dependencies:
pip install -r ./function-server/requirements.txt
Then run:
python3 -m unittest discover