SCF provides the features of getting the list and status of and terminating asynchronously executed function events for easier event management.
Note:Features involved in this document are supported only for asynchronously executed functions.
An asynchronously executed function event has the following status:
API | Description | Documentation |
---|---|---|
ListAsyncEvents | This API lists the information of an asynchronously executed event. It can query the information by conditions such as `RequestId`, function name, function version, event status, and event invocation/end time.
|
ListAsyncEvents |
GetAsyncEventStatus | This API is used to get the async event execution status based on the `RequestId`. The event status will be retained for 72 hours (after the end of the event). | GetAsyncEventStatus |
Note:When using an API, pay attention to the API call rate limit. We recommend you not call the
ListAsyncEvents
API frequently. To query the execution result of an async event, call theGetAsyncEventStatus
API instead.
SCF provides two async event termination methods: terminating invocation and sending termination signal, whose differences and usage are as detailed below:
API | Description | Documentation |
---|---|---|
TerminateAsyncEvent | This API is used to terminate an asynchronously executed event in progress according to the returned `RequestId`. The default behavior of this API is to terminate invocation. If the `GraceShutdown` parameter is set to `True`, the `SIGTERM` termination signal will be sent to the request. You can listen on the signal and customize the signal processing logic inside the function. | TerminateAsyncEvent |
When a function is invoked, SCF will assign an instance to process the function request or event. After the function code is executed and its response is returned, the instance will process other requests. If all instances are running when a request arrives, SCF will assign a new concurrent instance. For more information on instances, see Concurrency Overview.
After the asynchronously executed function event receives the invocation termination instruction, SCF will forcibly stop instance operations and repossess the instance. When the next request arrives, if there are no idle instances, SCF will assign a new instance to process the request.
Use cases
This method is suitable for scenarios where function execution needs to be stopped in advance, such as infinite loop and execution exception of an asynchronously executed function.
Notes
Invocation termination will forcibly terminate an instance and trigger instance repossession, which means that cached information in the instance cannot be obtained normally (such as files in the /tmp
folder). If you want to use this feature, promptly write the files in the instance cache to other persistent storage media to avoid file loss after instance repossession.
When you call the TerminateAsyncEvent
API and set the GraceShutdown
parameter to True
, SCF will send the termination signal SIGTERM
to the event specified in the input API parameter. You can listen on the signal and customize the processing logic after receiving the signal, including but not limited to function execution termination.
After an asynchronously executed function event receives the SIGTERM
signal:
User process exit when running
, indicating that the user process exits).This method is suitable for scenarios where function execution needs to be stopped for business requirements and custom processing logic needs to be run before the stop.
The following sample code describes how to use a custom signal processing function to stop function execution after the SIGTERM
signal is listened on:
# -*- coding: utf8 -*-
import time
import signal
class GracefulKiller:
kill_now = False
def __init__(self):
# Register signal processing function
signal.signal(signal.SIGTERM, self.graceshutdown)
def graceshutdown(self, *agrg):
print("do something before shutdown.")
self.kill_now = True
def main_handler(event, context):
killer = GracefulKiller()
while not killer.kill_now:
time.sleep(1)
print(killer.kill_now)
print("Graceful shutdown.")
return("END")
Python
# -*- coding: utf8 -*-
from flask import Flask, request
import time
import signal
app = Flask(__name__)
class GracefulKiller:
kill_now = False
def __init__(self):
# Register signal processing function
signal.signal(signal.SIGTERM, self.graceshutdown)
def graceshutdown(self, *agrg):
print("do something before shutdown.")
self.kill_now = True
@app.route('/event-invoke', methods = ['POST'])
def invoke():
while not killer.kill_now:
time.sleep(1)
print(killer.kill_now)
print("Graceful shutdown.")
return("END")
if __name__ == '__main__':
killer = GracefulKiller()
app.run(host='0.0.0.0', port=9000)
Was this page helpful?