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

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init fc-custom-python37-event
  2. 执行以下命令,进入项目目录。
    cd fc-custom-python37-event
  3. 可选:您可以修改示例代码,实现二次开发。
  4. 执行以下命令,安装依赖。
    s build -d
    输出示例:
    [2021-12-24 09:42:25] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-24 09:42:26] [INFO] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:42:27] [INFO] [FC-BUILD] - begin pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21, you can also use docker pull registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21 to pull image by yourself.
    build-1.9.21: Pulling from aliyunfc/runtime-custom
    ......
    Digest: sha256:29057f611f84f43a38fae4e69ea3f8ef6acbc077fb08b603f1626621bf79****
    Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:44:26] [INFO] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
  5. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start the pre-action
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Action: s build --use-docker
    [2021-12-24 09:49:31] [INFO] [S-CLI] - Start ...
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2021-12-24 09:49:32] [INFO] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21...
    [2021-12-24 09:50:02] [INFO] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    [2021-12-24 09:50:02] [INFO] [S-CLI] - End the pre-action
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
     devsapp_fc-plan.zip file decompression completed
     Checking Function pyEventFunc exists
    Detail:
    
    updated:
      LD_LIBRARY_PATH: /code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib
      PATH:            /code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
      NODE_PATH:       /code/node_modules:/usr/local/lib/node_modules
      PYTHONUSERBASE:  /code/.s/python
    {
      "LD_LIBRARY_PATH": "/code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib",
      "PATH": "/code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin",
      "NODE_PATH": "/code/node_modules:/usr/local/lib/node_modules",
      "PYTHONUSERBASE": "/code/.s/python"
    }
     Checking Service, Function (3.53s)
     Creating Service, Function (18.03s)
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       pyEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1536
        timeout:    30
  6. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    [2021-12-24 10:00:12] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    
    Duration: 780.81 ms, Billed Duration: 781 ms, Memory Size: 1536 MB, Max Memory Used: 80.67 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    End of method: invoke