This topic describes how to implement lifecycle hooks for function instances in a Python runtime.
Background
After you implement and configure a lifecycle hook for function instances, Function Compute calls the hook when the corresponding instance lifecycle event occurs. The following lifecycle hooks can be configured for a Python runtime: Initializer hooks and PreStop hooks. For more information, see Configure instance lifecycles.
The billing rules for lifecycle hooks of function instances are the same as the billing rules for common invocation requests. However, the execution logs can be queried only in Function Logs, Real-time Logs, and Advanced Logs. The logs for lifecycle hooks are not displayed in the invocation request list. For more information, see View the logs of instance lifecycle hooks.
Initializer hooks
An Initializer hook is invoked after a function instance is started and before a handler runs. Function Compute ensures that the Initializer hook is successfully invoked only once in the lifecycle of a function instance. If your Initializer hook fails to be executed, an error is returned for the function invocation. When you invoke the function next time, the system creates another function instance to execute the Initializer hook.
An Initializer hook contains only the context input parameter and can be invoked in the same manner as a handler.
The following sample code provides an example of a simple Initializer hook:
def initialize(context):
print("initialize invoked")
In the preceding sample code, initialize
is the name of the Initializer hook. The name must be the same as the Initializer hook that you configured in the Function Compute console. For example, if you set Initializer Hook
to index.initialize for a function, Function Compute loads the initialize
method defined in the index.py
file after the Initializer property is configured.
Method signature
The input parameter is only
context
, which provides your FC function with the running context during invocations.No value is returned.
PreStop hooks
A PreStop hook is executed before a function instance is destroyed. The method signature of a PreStop hook is the same as the method signature of an Initializer hook.
The following sample code provides an example of a simple PreStop hook:
def preStop(context):
print("preStop invoked")
Configure lifecycle hooks
Use the Function Compute console
In the Function Compute console, you can configure the Initializer Hook and PreStop Hook parameters of a function. For more information, see Configure the instance lifecycle.
The format of a hook is [File name.Method name]
. For example, if you set Initializer Hook to index.initialize
, the initialize
method in the index.py
file is loaded.
Use Serverless Devs
If you use Serverless Devs, you must add Initializer and PreStop hooks to the s.yaml
file.
Initializer hook
Add instanceLifecycleConfig.initializer, including the handler and timeout fields, to function.
PreStop hooks
Add instanceLifecycleConfig.preStop to function, including the handler and timeout fields.
Sample code:
edition: 3.0.0
name: fcDeployApp
access: "default"
vars: # The global variables
region: "cn-hangzhou"
resources:
hello_world:
component: fc3 # The name of the component
props:
region: ${vars.region} # For information about how to use variables, visit https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.
functionName: "emojipy"
description: 'this is emoji'
runtime: "python3"
code: ./
handler: index.handler
memorySize: 128
timeout: 30
environmentVariables:
PYTHONPATH: /code:/code/python:/opt/python
initializationTimeout: 20 # The timeout period for the execution of the initialization method.
# initializer: index.my_initializer # The initialization method
instanceLifecycleConfig: # The extension function
preStop: # The PreStop hook
handler: index.preStop # The handler
timeout: 60 # The timeout period
initializer: # The Initializer hook
handler: index.initialize
timeout: 60
For more information about the YAML syntax of Serverless Devs, see Common commands of Serverless Devs.
View the logs of instance lifecycle hooks
You can view the logs for lifecycle hook in Logs.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Test Function tab, click Test Function, and then choose .
On the Logs tab, you can view function invocation logs and Initializer logs. Example:
2024-03-04 17:57:28FC Initialize Start RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:282024-03-04 09:57:28.192 1-65e59b07-1520da26-bf73bbb91b69 [info] initializer 2024-03-04 17:57:28FC Initialize End RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:28FC Invoke Start RequestId: 1-65e59b07-1520da26-bf73bbb91b69 2024-03-04 17:57:28FC Invoke End RequestId: 1-65e59b07-1520da26-bf73bbb91b69
Each function instance is cached for a period of time and not immediately destroyed, you cannot view logs for PreStop hooks right away. To quickly trigger a PreStop hook, you can update the function configurations or function code. After the update is complete, you can view the logs for PreStop hooks in Function Logs. The following sample code shows an example:
2024-03-04 18:33:26FC PreStop Start RequestId: 93c93603-9fbe-4576-9458-193c8b213031 2024-03-04 18:33:262024-03-04 10:33:26.077 93c93603-9fbe-4576-9458-193c8b213031 [info] preStop 2024-03-04 18:33:26FC PreStop End RequestId: 93c93603-9fbe-4576-9458-193c8b213031
Sample program
Function Compute provides a sample MySQL program that uses Initializer and PreStop hooks. In this example, an Initializer hook is used to obtain MySQL database configurations from environment variables, create MySQL database connections, and test the connectivity. A PreStop hook is used to close MySQL connections.
For more information, see python3-mysql.