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

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/python37/event-demo/
  3. 执行以下命令,安装依赖。
    s build -d
    输出示例:
    [2021-09-08T14:58:24.993] [INFO ] [S-CLI] - Start ...
      devsapp_fc.zip file decompression completed
    [2021-09-08T14:58:26.918] [INFO ] [FC-BUILD] - Build artifact start...
    [2021-09-08T14:58:26.938] [INFO ] [FC-BUILD] - Use docker for building.
    [2021-09-08T14:58:27.155] [INFO ] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.19
    [2021-09-08T14:58:27.217] [INFO ] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.19...
    [2021-09-08T14:59:45.319] [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
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-07T11:35:00.900] [INFO ] [S-CLI] - Start ...
    (node:31560) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    [2021-09-07T11:35:02.487] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-07T11:35:02.488] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-07T11:35:02.488] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-07T11:35:02.489] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-07T11:35:03.205] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-07T11:35:03.466] [INFO ] [FC-DEPLOY] - Checking Function pyEventFunc exists
    [2021-09-07T11:35:03.584] [INFO ] [FC-DEPLOY] - Fc detects that you have run build command for function: pyEventFunc.
    [2021-09-07T11:35:03.584] [INFO ] [FC-DEPLOY] - Using codeUri: /test/start-fc-custom-samples/python37/event-demo/.s/build/artifacts/custom-demo/pyEventFunc
    [2021-09-07T11:35:03.585] [INFO ] [FC-DEPLOY] - Fc add/append some content to your origin environment variables for finding dependencies generated by build command.
    {
      "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"
    }
      Make service custom-demo success.
      Make function custom-demo/pyEventFunc success.
    [2021-09-07T11:35:23.139] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-07T11:35:23.199] [INFO ] [FC-DEPLOY] - Checking Function pyEventFunc exists
    
    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
    
    fc-python37-demo:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: pyEventFunc
        runtime: custom
        handler: index.handler
        memorySize: 1536
        timeout: 30
  5. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    [2021-09-07T15:01:23.259] [INFO ] [S-CLI] - Start ...
    (node:32576) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    ========= FC invoke Logs begin =========
    
    Duration: 4.75 ms, Billed Duration: 5 ms, Memory Size: 1536 MB, Max Memory Used: 78.26 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    
    End of method: invoke