通过函数计算的Custom Runtime,您可以在函数计算平台使用Python来编写函数。本文介绍如何快速部署和调用Python函数。您可以修改Python示例中的代码,实现您自己的需求。

背景信息

您已完成以下操作:
  • 安装8.6.0或以上版本的Node.js。更多信息,请参见官方下载地址
  • 安装Docker。更多信息,请参见Dockerhub

步骤一:准备环境

安装Funcraft,最简单的方式就是直接下载可执行的二进制文件。

  1. 安装Funcraft到本机。更多信息,请参见安装Funcraft
  2. 执行fun --version检查安装是否成功。
  3. 执行fun config配置Funcraft。然后按照提示依次配置Account IDAccess Key IDAccess Key SecretDefault region name
    fun config
    Aliyun Account ID 1234xxx
    Aliyun Access Key ID xxxx
    Aliyun Access Key Secret xxxx
    Default region name cn-xxxx
    The timeout in seconds for each SDK client invoking 300
    The maximum number of retries for each SDK client 5
    Allow to anonynously report usage statistics to improve the tool over time? (Y/n)

步骤二:部署并调用函数

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装Git,可以直接在浏览器地址栏输入https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载示例代码。
  2. 可选:修改python37-demo目录中的示例代码,实现您的业务逻辑。
  3. 执行以下命令进入克隆的示例项目中。
    1. 执行以下命令进入fc-custom-demo文件。
      cd fc-custom-demo
    2. 执行以下命令进入python37-demo文件。
      cd python37-demo    
  4. 执行以下命令安装依赖。
    fun install -v   
    返回结果如下。
    Digest: sha256:790b073005953d1b41eef2b7799e3d24381d9fe3e1e2bd48101530ce996b****
    Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.9
     ---> 2479a0c4aab6
    Step 2/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install flask
     ---> Running in 4aa275ae7e33
    Task => PipTask
         => PYTHONUSERBASE=/code/.fun/python pip install --user --upgrade flask -i https://pypi.tuna.tsinghua.edu.cn/simple
    Removing intermediate container 4aa275ae7e33
     ---> 7d100a3ab120
    Step 3/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install gunicorn
     ---> Running in d6a2de957cf3
    Task => PipTask
         => PYTHONUSERBASE=/code/.fun/python pip install --user --upgrade gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple
    Removing intermediate container d6a2de957cf3
     ---> f4f101691d37
    sha256:f4f101691d373598dbe02b93a1ce78dd643adf071483ad5645ef423de217****
    Successfully built f4f101691d37
    Successfully tagged fun-cache-a5ed3048-bf7f-48db-bd5d-371592c01059:latest
    copying function artifact to D:\fc-custom-demo\python37-demo
    
    Install Success 
  5. 执行以下命令将项目部署至云上。
    fun deploy -y      
    返回结果如下。
    Waiting for service python37-demo to be deployed...
            Waiting for function fc-python37 to be deployed...
                    Waiting for packaging function fc-python37 code...
                    The function fc-python37 has been packaged. A total of 2 files were compressed and the final size was 641 B
            function fc-python37 deploy success
    service python37-demo deploy success
  6. 执行以下命令调用部署的函数。
    fun invoke -e "Hello World"          
    说明 如果您使用的是Windows操作系统,您需要将bootstrap文件格式修改为UNIX格式。
    返回结果如下。
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: cd30369e-7dfa-439c-a68d-7fe16d5a7e05
    Hello World
    FC Invoke End RequestId: cd30369e-7dfa-439c-a68d-7fe16d5a7e05
    
    Duration: 54.13 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 133.70 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Hello World