This topic describes how to implement and use instance lifecycle hook methods in Python.
Background information
After you implement and configure an instance lifecycle hook, Function Compute calls the hook when the corresponding lifecycle event occurs. The instance lifecycle includes Initializer and PreStop hooks. For more information, see Configure instance lifecycles.
The billing rules for instance lifecycle hooks are the same as for standard function invocations. However, their execution logs can be viewed only in Real-time Logs, Function Logs, or Advanced Logs. Lifecycle hook logs are not displayed in the Invocation Request List. For more information, see View the logs of instance lifecycle hooks.
Initializer hook
The Initializer hook runs after a function instance starts and before the handler runs. Function Compute ensures that the Initializer hook runs successfully only once per instance lifecycle. If the Initializer hook fails on its first execution, the function invocation returns an error. The next time you invoke the function, a new instance is created to run the Initializer hook again.
If an Initializer hook times out or fails, the server returns an HTTP 200 status code. To check for an initialization failure, inspect the X-Fc-Error-Type:InitializationError
response header or the errorMessage field in the response body.
An Initializer hook has only one input parameter, context, and is used in the same way as a handler.
The following code shows a simple Initializer method.
def initialize(context):
print("initialize invoked")
initialize
is the name of the Initializer hook method. It must match the Initializer hook that you configure in the Function Compute console. For example, if you set the Initializer hook for a function to index.initialize
, Function Compute loads the index.py
file's initialize
method.
Method signature
The only input parameter is
context
. This parameter provides the runtime context for your FC function invocation.No value is returned.
PreStop hook
The PreStop hook runs before a function instance is destroyed. The method signature is the same as that of an Initializer hook.
The following code shows a simple PreStop method.
def preStop(context):
print("preStop invoked")
Configure lifecycle hooks
Configure hooks in the console
Log on to the Function Compute console to configure the Initializer Hook and PreStop Hook for a function. For more information, see Configure instance lifecycles.
The format for a hook is [File name.Method name]
. For example, if you set Initializer Hook to index.initialize
, the index.py
file's initialize
method is used.
Configure hooks using Serverless Devs
If you use Serverless Devs, you can add the Initializer and PreStop hook configurations to the s.yaml
file.
Initializer hook configuration
Under the function configuration, add the instanceLifecycleConfig.initializer field. This field includes the handler and timeout fields.
PreStop hook configuration
Under the function configuration, add the instanceLifecycleConfig.preStop field. This field includes the handler and timeout fields.
The following code provides an example.
edition: 3.0.0
name: fcDeployApp
access: "default"
vars: # Global variables
region: "cn-hangzhou"
resources:
hello_world:
component: fc3 # Component name
props:
region: ${vars.region} # For information about how to use variables, see: 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 # Timeout for the initialization method
# initializer: index.my_initializer # Initialization method
instanceLifecycleConfig: # Extension function
preStop: # PreStop function
handler: index.preStop # Function handler
timeout: 60 # Timeout
initializer: # Initializer function
handler: index.initialize
timeout: 60
For more information about the YAML configuration specifications for Serverless Devs, see Common Serverless Devs commands.
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 obtains MySQL database configurations from environment variables, creates MySQL connections, and tests the connectivity. A PreStop hook closes the MySQL connections.
For more information, see python3-mysql.