Syntax for creating a handler function in Python:
def handler (event, context)
FunctionGraph supports the following Python runtimes:
Initializer syntax:
[File name].[Initializer name]
For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.
To use Python to build initialization logic, define a Python function as the initializer. The following is a simple initializer:
def my_initializer(context):print("hello world!")
The function name my_initializer must be the initializer function name specified for a function. For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.
The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.
Table 1 describes the context methods provided by FunctionGraph.
Method | Description |
|---|---|
getRequestID() | Obtains a request ID. |
getRemainingTimeInMilliSeconds () | Obtains the remaining running time of a function. |
getAccessKey() | Obtains the AK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function. NOTE: FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK. |
getSecretKey() | Obtains the SK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function. NOTE: FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK. |
getUserData(string key) | Uses keys to obtain the values passed by environment variables. |
getSecurityAccessKey() | Obtains the SecurityAccessKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. |
getSecuritySecretKey() | Obtains the SecuritySecretKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. |
getSecurityToken() | Obtains the SecurityToken (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. |
getFunctionName() | Obtains the name of a function. |
getRunningTimeInSeconds () | Obtains the timeout of a function. |
getVersion() | Obtains the version of a function. |
getMemorySize() | Obtains the allocated memory. |
getCPUNumber() | Obtains CPU usage of a function. |
getProjectID() | Obtains a project ID. |
getPackage() | Obtains a function group, that is, an app. |
getToken() | Obtains the token (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function. |
getLogger() | Obtains the logger method provided by the context and returns a log output class. Logs are output in the format of Time-Request ID-Content by using the info method. For example, use the info method to output logs: logg = context.getLogger() logg.info("hello") |
Results returned by using the getToken(), getAccessKey(), and getSecretKey() methods contain sensitive information. Exercise caution when using these methods.
The execution result consists of the function output, summary, and log output.
Parameter | Successful Execution | Failed Execution |
|---|---|---|
Function Output | The defined function output information is returned. | A JSON file that contains errorMessage, errorType, and stackTrace is returned. The format is as follows:
errorMessage: Error message returned by the runtime. errorType: Error type. stackTrace: Stack error information returned by the runtime. |
Summary | Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed. | Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed. |
Log Output | Function logs are printed. A maximum of 4 KB logs can be displayed. | Error information is printed. A maximum of 4 KB logs can be displayed. |