You need to enable JavaScript to run this app.
Volcano Engine Function as a Service

Volcano Engine Function as a Service

Copy page
Download PDF
Native Python runtime
Development guidelines
Copy page
Download PDF
Development guidelines
A veFaaS web application is an HTTP server running in the Native Python Runtime. This topic outlines the specifications and guidelines you need to follow when developing an HTTP server.​
Service specifications​
When developing an HTTP server locally, follow the specifications below:​
  • The service itself must be stateless​
  • If state persistence is required, store state remotely, for example, in Redis or MySQL. Service startup must not depend on local memory caches or on-disk data.​
  • Startup command​
  • The service must have a startup command, which can be configured when you create the web application in the console.​
  • For a simple startup command, you can enter it directly in the console.​
  • If the startup process is more complex, you can include a startup script in the project and specify this script when configuring the startup command in the console.​
  • ​
    chmod a+x run.sh​
    ​
  • Listening port​
  • The service must listen on a port.​
  • ‌Ensure the port your service code listens on matches the port configured when you create the web application in the console. A mismatch will cause a cold start failure during function deployment with the following error.​
  • ​
    function_start_timeout​
    ​
    function process start timeout: function start timed out (120s) with command `./main --port 3000`, function may not listen on address *:8000, logs: current working directory: /opt/bytefaas Server listen on address: :3000. Please check your log(runtime_log)/code and then retry, or contact oncall​
    ​
  • In the service, you can obtain the specified listening port through the environment variable _FAAS_RUNTIME_PORT.​
  • Avoiding background processes​
  • We recommend against starting background processes or threads (where the request has returned a response, but tasks continue executing asynchronously in the background). veFaaS dynamically scales backend instances based on request volume and cannot guarantee the survival of background processes or threads.​
Development example​
In the Native Python runtime, veFaaS forwards the request Method, Path, Body, Query, and Headers to your HTTP Server. You can directly use the incoming request headers and body to implement your business logic.​
The following is a complete code example of developing an application using the FastAPI framework.​
  • app.py:​
​
from fastapi import FastAPI​
import time​
​
# Create a FastAPI application instance.​
app = FastAPI()​
​
# Define the GET route for the root path '/'.​
@app.get('/')​
def index_handler():​
return 'Hello FastAPI From FaaS nativepython3 (and bye python2)!'​
​
# Define the POST route for the root path '/'.​
@app.post('/')​
async def root_post_handler(payload: dict):​
return {"message": "POST request received", "payload": payload}​
​
  • run.sh (startup script):​
​
#!/bin/bash​
set -ex​
cd `dirname $0`​
​
if [ -d "output" ]; then​
cd ./output/​
fi​
​
# Configure the listening address and port.​
HOST="0.0.0.0"​
PORT=${_FAAS_RUNTIME_PORT}​
TIMEOUT=${_FAAS_FUNC_TIMEOUT}​
​
# Parse parameters.​
while [[ $# -gt 0 ]]; do​
case $1 in​
--port)​
PORT="$2"​
shift 2​
;;​
--host)​
HOST="$2"​
shift 2​
;;​
*)​
shift​
;;​
esac​
done​
​
exec python3 -m uvicorn app:app --host $HOST --port $PORT --timeout-graceful-shutdown $TIMEOUT​
​
Service listening port and startup​
You can pass the port via a command-line parameter or obtain the configured listening port (default: 8000) via the environment variable _FAAS_RUNTIME_PORT. The following run.sh code demonstrates the logic for obtaining the listening address (host) and port number (port) from the command-line parameters and the environment variables.​
warning​
The listening port of the service must match the one configured in the console.​
​
# Configure the listening address and port.​
# The listening address must be 0.0.0.0. Otherwise, the deployment will fail.​
HOST="0.0.0.0"​
PORT=${_FAAS_RUNTIME_PORT}​
TIMEOUT=${_FAAS_FUNC_TIMEOUT}​
​
# Parse parameters.​
while [[ $# -gt 0 ]]; do​
case $1 in​
--port)​
PORT="$2"​
shift 2​
;;​
--host)​
HOST="$2"​
shift 2​
;;​
*)​
shift​
;;​
esac​
done​
​
Request processing​
Since you implement your own HTTP server in the Native Python runtime, you can define custom request processing logic in the HTTP server code. The following code demonstrates a simple request handler interface.​
​
# Process requests.​
@app.get('/')​
def index_handler():​
return 'Hello FastAPI From FaaS nativepython3 (and bye python2)!'​
​
Configure a startup command​
After code development and compilation, you need to configure a startup command and a listening port when you create the web application in the console. In this example, the startup command is ./run.sh and the listening port is 8000.​
Use environment variables​
You can configure the environment variables required by the runtime of your Native Python 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 envKey, you can use the following code sample to read the environment variable.​
​
import "os"​
envValue = os.getenv("envKey")​
​
​
Last updated: 2026.08.19 14:06:05