更新时间:2023-04-26 GMT+08:00
函数间可以相互调用吗?
相同Region间函数可通过API/SDK相互调用。不同Region间函数调用,调用者须具有公网访问权限。
使用API互调函数
具体请参考函数互调。
使用SDK互调函数(以Python为例)
- 创建被调函数FuncB
创建空白函数,函数名称设置为FuncB,运行时选择Python3.6,函数代码修改如下:
# -*- coding:utf-8 -*- import json def handler (event, context): # 增加输入参数打印,用于验证互调成功 logger = context.getLogger() logger.info(event) return { "statusCode": 200, "isBase64Encoded": False, "body": json.dumps(event), "headers": { "Content-Type": "application/json" } }
- 创建主调函数FuncA
1. 创建空白函数,函数名称设置为FuncA,运行时选择Python3.6,函数代码修改如下所示,您需要替换<Your Function Region>和<Your Function URN>为FuncB函数所在Region和URN:
# -*- coding:utf-8 -*- from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * def handler (event, context): logger = context.getLogger() ak = context.getAccessKey() sk = context.getSecretKey() credentials = BasicCredentials(ak, sk) client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<Your Function Region>")) \ .build() try: request = InvokeFunctionRequest() request.function_urn = "<Your Function URN>" # 函数入参 request.body = "{'input_key':'input_value'}" # 返回体格式,取值v0,v1。 v0:默认返回文本格式 v1:默认返回json格式 request.x_cff_request_version = "v1" response = client.invoke_function(request) logger.info(response) except exceptions.ClientRequestException as e: logger.error(e.status_code) logger.error(e.request_id) logger.error(e.error_code) logger.error(e.error_msg) return { "statusCode": 200, "isBase64Encoded": False, "body": "", "headers": { "Content-Type": "application/json" } }
- 测试验证
为FuncA函数配置“空白模板”测试事件,点击“测试”触发函数,执行结果如下:
图1 查看执行结果
查看FuncB函数相同时间段的执行日志:
父主题: 函数执行
函数执行 所有常见问题
- FunctionGraph函数的执行需要多长时间?
- FunctionGraph函数的执行包含了哪些过程?
- FunctiongGraph函数的并发处理过程是什么?
- FunctiongGraph函数如何处理长时间不执行的实例?
- 首次访问函数慢,如何优化?
- 怎样获取在函数运行过程中实际使用了多少内存?
- 为什么第一次请求会比较慢?
- 调用API时,报错怎么办?
- 如何读取函数的请求头?
- 函数间可以相互调用吗?
- API同步执行函数接口,是否支持内网调用?
- 为什么函数实际使用内存大于预估内存,甚至触发OOM?
- 函数内存超限返回“runtime memory limit exceeded”,如何查看内存占用大小?
- 如何定位自定义镜像执行失败“CrashLoopBackOff”的原因?
- 用户使用相同的镜像名更新镜像,预留实例无法自动更新,会一直使用老镜像,应如何处理?
more