You can use the custom runtime of Function Compute to write Python functions in Function Compute. This topic describes how to deploy and invoke a Python function. You can modify the sample Python code.

Prerequisites

Before you begin, make sure that you have completed the following operations:
  • Install Node.js 8.6.0 or later. For more information, visit Downloads.
  • Install Docker. For more information, visit Docker Hub.

Procedure

  1. Run the following command to clone the sample project to the on-premises machine:
    git clone https://github.com/awesome-fc/fc-custom-demo          
    Note If you do not install Git, you can download the sample project from https://github.com/awesome-fc/fc-custom-demo.
  2. Optional:Modify the sample code in the python37-demo directory to implement your business logic.
  3. Run the following command to go to the cloned sample project:
    1. Run the following command to go to the python37-demo directory:
      cd python37-demo
  4. Run the following command to install dependencies:
    fun install -v
    The following output is returned:
    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. Run the following command to deploy the project to Function Compute:
    fun deploy -y 
    The following output is returned:
    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. Run the following command to invoke the deployed function:
    fun invoke -e "Hello World"
    The following output is returned:
    ========= 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