Tencent Cloud SDK
Tencent CloudGitHub
简体中文
简体中文
  • 欢迎
  • 常见问题
  • Python
  • 概述
  • 安装
  • 入门
  • 最佳实践
    • 无服务器云函数
      • 无服务器云函数间调用
      • 定时调用无服务器云函数
      • 原生调用无服务器云函数
        • 绑定到无服务器云函数
        • 使用例程组织无服务器云函数
    • 无服务器数据库
      • 创建和管理无服务器数据库
    • 其他 Tencent Cloud 产品
      • 选择最佳的访问凭据类型
  • API 文档
    • 无服务器云函数
      • 类和方法
        • Client 类
          • easy_invoke 方法
          • routine_invoke 方法
          • select_function 方法
          • select_routine 方法
          • bind_function 方法
          • bind_routine 方法
          • schedule_invoke 方法
          • run_schedule 方法
          • invoke 方法
          • create_function 方法
          • delete_function 方法
          • publish_function_version 方法
          • copy_function 方法
          • update_function_code 方法
          • update_function_configure 方法
          • get_function_result_by_request_id 方法
          • get_function_results 方法
          • list_functions 方法
          • list_function_versions 方法
          • get_function_info 方法
          • create_trigger 方法
          • delete_trigger 方法
          • create_namespace 方法
          • delete_namespace 方法
          • list_namespaces 方法
          • update_namespace 方法
          • get_function_code_download_url 方法
          • download_function_code 方法
          • create_layer 方法
          • delete_layer 方法
          • get_layer_info 方法
          • list_layers 方法
          • list_layer_versions 方法
          • submit_monitor_indicator 方法
        • FunctionSchedule 类
          • cancel 方法
        • FunctionResultFuture 类
          • get_request_id 方法
        • FunctionTrigger 类
          • get_trigger_kind 方法
          • get_trigger_context 方法
          • use_timer 方法
          • use_object_storage_bucket 方法
          • use_message_queue_topic 方法
        • FunctionCode 类
          • get_code_source 方法
          • get_code_context 方法
          • use_object_storage_bucket 方法
          • use_local_zip_archive 方法
          • use_git_repository 方法
        • LayerContent 类
        • fetch_client 函数
        • use_routine_dispatcher 函数
      • 数据类型
        • FunctionResult 类型
        • FunctionInfo 类型
        • FunctionInfoEx 类型
        • FunctionVersionInfo 类型
        • NamespaceInfo 类型
        • LayerInfo 类型
        • FunctionConfigure 类型
        • FunctionResultFilter 类型
        • FunctionFilter 类型
        • ObjectStorageBucketTriggerFilter 类型
        • MessageQueueTopicTriggerFilter 类型
        • LayerFilter 类型
        • FunctionRuntime 枚举器
        • FunctionType 枚举器
        • FunctionResultType 枚举器
        • FunctionTriggerKind 枚举器
        • FunctionCodeSource 枚举器
        • LayerContentSource 枚举器
        • LayerStatus 枚举器
      • 异常
        • InvokeError 异常
        • StatusError 异常
    • 无服务器数据库
      • 类和方法
        • Client 类
          • create_instance 方法
          • delete_instance 方法
          • list_instances 方法
          • set_instance_extranet 方法
        • fetch_client 函数
      • 数据类型
        • InstanceInfo 类型
        • InstanceConfigure 类型
        • DatabaseCharset 枚举器
    • 其他 Tencent Cloud 产品
      • 类和方法
        • Waitable 类
          • wait_for_done 方法
          • has_done 方法
          • set_result 方法
        • ErrorManager 类
          • add_handler 方法
          • remove_handler 方法
          • has_handler 方法
          • clear_all_handler 方法
        • UniversalClient 类
          • action 方法
          • action_for_product 方法
          • select_action 方法
          • get_product_id 方法
          • set_product_id 方法
          • set_access_endpoint 方法
          • set_access_proxies 方法
          • get_last_response_metadata 方法
        • Proxies 类
          • use_proxy_server 方法
          • add_proxy_server 方法
          • remove_proxy_server 方法
      • 数据类型
        • ResponseMetadata 类型
        • ProxyAuth 类型
        • ProxyType 枚举器
        • ErrorHandlerResult 枚举器
        • WaitableStatus 枚举器
      • 异常
        • Error 异常
        • NotFoundError 异常
        • ExistedError 异常
        • OccupiedError 异常
        • ClientError 异常
        • RequestError 异常
        • ResponseError 异常
        • ActionError 异常
        • ActionResultError 异常
  • GitHub 存储库
  • Issues 跟踪器
  • PyPI 项目主页
由 GitBook 提供支持
在本页
  • 概述
  • 指导
  • 绑定 Python 函数
  • 绑定 Python 类方法
  • 探究

这有帮助吗?

导出为 PDF
  1. 最佳实践
  2. 无服务器云函数
  3. 原生调用无服务器云函数

绑定到无服务器云函数

本文将向您进一步介绍并演示借助 Tencent Cloud SDK for Python 如何将无服务器云函数绑定到类方法、类实例方法或函数,这是获得关联无服务器云函数的 Python 原生函数的进阶。

上一页原生调用无服务器云函数下一页使用例程组织无服务器云函数

最后更新于5年前

这有帮助吗?

概述

这是由 Tencent Cloud SDK for Python 的无服务器云函数产品客户端所提供的高级特性,如果您未曾阅读如何选择一个无服务器云函数并获得与之关联的 Python 原生函数的实践文档,请先阅读:

在上篇实践文档中我们得知 Tencent Cloud SDK for Python 的无服务器云函数产品客户端的实例方法 select_function 可以选择一个无服务器云函数并获得与之相关联的 Python 原生函数,通过对 Python 原生函数的调用即为对关联无服务器云函数的调用,从而屏蔽 RPC 感知。

遗憾的是,我们无法控制所获得与无服务器云函数关联的 Python 原生函数的签名和形态,且不能将其集成为类方法或类实例方法。与此同时,我们或许还需要在全局范围内维护一个 Python 原生函数实例。

不过无需担心,Tencent Cloud SDK for Python 的无服务器云函数产品客户端提供了另一种高级特性用于解决上述问题,我们将其称之为绑定到无服务器云函数。

与关联到无服务器云函数的高级特性不同,绑定到无服务器云函数特性使用 Python 高级特性装饰器实现,这意味着开发者可以以更简洁、美观的代码快速集成该特性。

没有理解?没关系,在下文中我们将通过几个简单的应用场景向您进一步说明如何使用绑定到无服务器云函数特性组成您的应用程序代码逻辑。

指导

在下文中我们将列举几段连续的示例代码并向您介绍 Tencent Cloud SDK for Python 的无服务器云函数产品客户端所提供的绑定到无服务器云函数特性的工作流程,我们仍然以无服务器云函数 addend 为例:

导入模块

首先我们需要在应用程序中导入所依赖的 Tencent Cloud SDK for Python 相关命名空间和模块:

from tencent.cloud.auth import credentials
from tencent.cloud.serverless import functions

产品客户端

然后我们需要在应用程序源代码的适合层级实例化一个或多个无服务器云函数产品客户端和访问凭据,在下文中我们将选择 Python 模块全局变量作为适合层级:

function_client: functions.Client = functions.Client(
    access_credentials = credentials.Credentials(
        secret_id = 'AKIDF3sMOAU1pOgkmrKHchX6TZQ1Mo1C5qa7',
        secret_key = 'b4JL8fwxkIgsKMXGi39yYt0ECxZw4wZf'
    )
)

在上文示例代码中,我们所实例化的无服务器云函数产品客户端由 Python 模块全局变量 function_client 存储。

绑定 Python 函数

Python 原生函数可能是我们将应用程序与无服务器云函数相集成的多数选择。

投射函数

我们首先为无服务器云函数 addend 编写在 Python 原生函数的投射:

def addend(
    value1: int,
    value2: int
) -> int:
    raise NotImplementedError

在上文示例代码中,Python 原生函数 addend 将引发 NotImplementedError 异常,该异常指示当前函数逻辑未实现。

绑定函数

接下来我们需要做的是,为无服务器云函数 addend 的 Python 原生投射函数套用一层装饰器:

@function_client.bind_function(
    region_id = 'ap-shanghai',
    namespace_name = 'default',
    function_name = 'addend'
)
def addend(
    value1: int,
    value2: int
) -> int:
    raise NotImplementedError

上文示例代码中是 Python 原生投射函数 addend 套用 Python 装饰器后的形态。

运行函数

最后,我们尝试调用在上文示例代码中定义的 Python 原生函数 addend 并获得运行结果:

def main():
    addition_result: int = addend(10, 20)
    print('info: addition result: ' + str(addition_result))

如果一切正常,我们将得到打印输出:

info: addition result: 30

绑定的无服务器云函数 addend 运行事件为:

{
    "value1": 10,
    "value2": 20
}

绑定 Python 类方法

有时或许我们希望使用 Python 类方法或类实例方法与无服务器云函数相集成。

定义实例类

首先我们需要编写一个可实例化的类以演示如何将类方法或类实例方法绑定到无服务器云函数:

class Addition:

    def __init__(self,
        value1: int,
        value2: int
    ):
        self.value1: int = value1
        self.value2: int = value2
    
    def get_result(self) -> int:
        raise NotImplementedError

在上文示例代码中,我们定义了符号名为 Addition 的实例类。该类拥有一个实例方法 get_result 用于计算类属性 value1 和 value2 的相加值。

绑定方法

接着,我们需要将实例类 Addition 的实例方法 get_result 绑定到无服务器云函数 addend 以实现加法运算功能。套用装饰器后的实例类形态如下:

class Addition:

    def __init__(self,
        value1: int,
        value2: int
    ):
        self.value1: int = value1
        self.value2: int = value2
    
    @function_client.bind_function(
        region_id = 'ap-shanghai',
        namespace_name = 'default',
        function_name = 'addend',
        include_attributes = [
            'value1',
            'value2'
        ]
    )
    def get_result(self) -> int:
        raise NotImplementedError

在上文示例代码中我们额外使用了一个在绑定 Python 函数时未使用也不受支持的装饰器参数 include_attributes。该参数允许提供一个列表实例,列表实例的成员为需要从当前所绑定类方法或类实例方法的类实例或静态类中所额外包含的公开属性名称。

被装饰器参数 include_attributes 包含的类公开属性名和值将作为绑定的无服务器云函数运行时事件的字段名称和值,被绑定的类方法或类实例方法的参数也将包括在其中,这与是否使用并设置装饰器参数 include_attributes 的值无关。

请注意,被绑定的类方法的首个参数名称必须为 cls;被绑定的类实例方法的首个参数名称必须为 self,否则行为是未定义的。

运行方法

最后,我们尝试实例化一个 Addition 类并调用其实例方法 get_result 获得加法运算结果:

def main():
    addition_result: int = Addition(10, 20).get_result()
    print('info: addition result: ' + str(addition_result))

如果一切正常,我们将得到打印输出:

info: addition result: 30

绑定的无服务器云函数 addend 运行事件为:

{
    "value1": 10,
    "value2": 20
}

探究

或许您已经注意到,使用 Tencent Cloud SDK for Python 的无服务器云函数产品客户端所提供的绑定到无服务器云函数高级特性时,调用被绑定的函数、类方法或类实例方法支持顺序参数和关键字参数,与 Python 原生函数、类方法或类实例方法的参数传递规则一致。

这是因为 Tencent Cloud SDK for Python 在实现过程中使用 Python 反射(自省)高级特性实现对被绑定可调用对象的参数名称的检查。

原生调用无服务器云函数
什么是 Python 装饰器?
bind_function 方法