For Python functions, veFaaS uses the handler entry function defined in the index.py file that you provide to start the veFaaS service process and process request events. Python functions support both online editing in the console and uploading code packages. When submitting a code package via a local ZIP file or TOS, ensure that the root directory of the ZIP package contains a Python source file named index.py, and that this file contains an entry function named handler. This can avoid execution failures caused by the absence of the entry file or entry function.
The sample Python function code is as follows.
Entry function handler
The handler is the entry point for the request processing logic of the function. It contains your core business logic, which is used to process event requests, and finally returns an optional value. The signature of the handler function is as follows.
When veFaaS triggers your handler function, the Python runtime passes two parameters, event and context, to your handler function.
event parameter
The event parameter is used to pass trigger event data to your handler function. The type of the event parameter is dict.
context parameter
The context parameter is used to pass runtime information to your handler function. The type of the context parameter is object.
- The context parameter contains the following fields:
Return value
The handler function can return an optional value of the dict type. Among the return values, only the three key-value pairs statusCode, headers, and body will be returned to the requester.
If no value is returned, veFaaS will return a 200 status code by default.
A sample return value of the handler function is as follows.
Parameter description
Use environment variables
For veFaaS functions, you can configure the environment variables required by the runtime of your function in the console and retrieve them in your code to implement function processing logic. veFaaS injects configured environment variables into the container where the function runs, and the program can read them using a built-in library for the programming language.
For example, if the key of an environment variable is ENV_KEY, you can use the following code sample to read the environment variable.