#! /bin/bashset -euo pipefail# 初始化 - 加载函数文件source ./"$(echo $_HANDLER | cut -d. -f1).sh"# 初始化完成,访问运行时API上报就绪状态curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"### 循环监听处理事件调用while truedoHEADERS="$(mktemp)"# 长轮询获取事件EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")# 调用函数处理事件RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")# 推送函数处理结果curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"done
# 初始化完成,访问运行时API上报就绪状态curl -d " " -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/init/ready"
SCF_RUNTIME_API:运行时 API 地址。SCF_RUNTIME_API_PORT:运行时 API 端口。# 长轮询获取事件EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/next")
# 调用函数处理事件RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# 推送函数处理结果curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/response" -d "$RESPONSE"
# 推送函数处理错误curl -X POST -s "http://$SCF_RUNTIME_API:$SCF_RUNTIME_API_PORT/runtime/invocation/error" -d "parse event error"
function main_handler () {EVENT_DATA=$1echo "$EVENT_DATA" 1>&2;RESPONSE="Echoing request: '$EVENT_DATA'"echo $RESPONSE}
├ bootstrap└ index.sh
chmod 755 命令,需要在 Linux 或 Mac OS 系统下执行。$ chmod 755 index.sh bootstrap
$ zip demo.zip index.sh bootstrapadding: index.sh (deflated 23%)adding: bootstrap (deflated 46%)
from tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.scf.v20180416 import scf_client, modelsfrom base64 import b64encodetry:cred = credential.Credential("SecretId", "secretKey")httpProfile = HttpProfile()httpProfile.endpoint = "scf.tencentcloudapi.com"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = scf_client.ScfClient(cred, "na-toronto", clientProfile)req = models.CreateFunctionRequest()f = open('demo.zip', 'r')code = f.read()f.close()params = '{\\"FunctionName\\":\\"CustomRuntime-Bash\\",\\"Code\\":{\\"ZipFile\\":\\"'+b64encode(code)+'\\"},\\"Timeout\\":3,\\"Runtime\\":\\"CustomRuntime\\",\\"InitTimeout\\":3}'req.from_json_string(params)resp = client.CreateFunction(req)print(resp.to_json_string())except TencentCloudSDKException as err:print(err)
参数类型 | 说明 |
"Runtime":"CustomRuntime" | Custom Runtime 对应的 runtime 类型。 |
"InitTimeout":3 | 初始化超时时间。Custom Runtime 针对初始化阶段新增超时控制配置,时间区间以 bootstrap 启动为始,以上报运行时 API 就绪状态为止。超出后将终止执行并返回初始化超时错误。 |
"Timeout":3 | 调用超时时间。事件调用的超时控制配置,时间区间以事件下发为始,以函数处理完成推送结果至运行时 API 为止。超出后将终止执行并返回调用超时错误。 |
from tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.scf.v20180416 import scf_client, modelstry:cred = credential.Credential("SecretId", "secretKey")httpProfile = HttpProfile()httpProfile.endpoint = "scf.tencentcloudapi.com"clientProfile = ClientProfile()clientProfile.httpProfile = httpProfileclient = scf_client.ScfClient(cred, "na-toronto", clientProfile)req = models.InvokeRequest()params = '{\\"FunctionName\\":\\"CustomRuntime-Bash\\",\\"ClientContext\\":\\"{ \\\\\\"key1\\\\\\": \\\\\\"test value 1\\\\\\", \\\\\\"key2\\\\\\": \\\\\\"test value 2\\\\\\" }\\"}'req.from_json_string(params)resp = client.Invoke(req)print(resp.to_json_string())except TencentCloudSDKException as err:print(err)
{"Result":{"MemUsage": 7417***,"Log": "", "RetMsg":"Echoing request: '{\\"key1\\": \\"test value 1\\",\\"key2\\": \\"test value 2\\"}'","BillDuration": 101,"FunctionRequestId": "3c32a636-****-****-****-d43214e161de","Duration": 101,"ErrMsg": "","InvokeResult": 0},"RequestId": "3c32a636-****-****-****-d43214e161de"}



文档反馈