Help Center/ FunctionGraph/ Developer Guide/ Python/ Function Development Overview
Updated on 2025-12-10 GMT+08:00

Function Development Overview

FunctionGraph supports the following Python runtimes:

  • Python 2.7
  • Python 3.6
  • Python 3.9
  • Python 3.10
  • Python 3.12

Function Syntax

Use the following syntax when creating a handler function in Python:

def handler (event, context)
  • handler: name of the function that FunctionGraph invokes to execute your code. The name must be consistent with that you define when creating a function.
  • event: event parameter defined for the function. The parameter is in JSON format.
  • Context: runtime information provided for executing the function. For details, see SDK APIs.

The Python function handler is in the format of [File name].[Function name]. You can configure the handler on the FunctionGraph console.

Python Initializer

For details about the initializer, see Initializer.

The initializer is in the format of [File name].[Initializer name].

For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.

To use Python to build initialization logic, define a Python function as the initializer. The following is a simple initializer (Python 3.12 is used as an example):

def my_initializer(context):
    print('hello world!')
  • Function name

    The function name my_initializer must be the initializer function name specified for a function. For example, if the initializer is named main.my_initializer, FunctionGraph loads the my_initializer function defined in the main.py file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.

Non-standard Libraries Integrated with the Python Runtime

Table 1 lists the non-standard libraries integrated with Python, which can be directly declared and used in Python function code.

Table 1 Non-standard libraries integrated with Python

Library

Usage

Version

dateutil

Date and time processing

2.6.0

requests

HTTP library

2.7.0

httplib2

httpclient

0.10.3

numpy

Mathematical computation

1.13.1

redis

Redis client

2.10.5

obsclient

OBS client

-

smnsdk

SMN access

1.0.1

SDK APIs

Table 2 describes the context methods provided by FunctionGraph.

Table 2 Context methods

Method

Description

getRequestID()

Obtains a request ID.

getRemainingTimeInMilliSeconds ()

Obtains the remaining running time of a function.

getAccessKey()

Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.

getSecretKey()

Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.

getSecurityAccessKey()

Obtains the SecurityAccessKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

getSecuritySecretKey()

Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

getSecurityToken()

Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

getUserData(string key)

Uses keys to obtain the values passed by environment variables.

getFunctionName()

Obtains the name of a function.

getRunningTimeInSeconds ()

Obtains the timeout of a function.

getVersion()

Obtains the version of a function.

getMemorySize()

Obtains the allocated memory.

getCPUNumber()

Obtains CPU usage of a function.

getPackage()

Obtains a function group.

getToken()

Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

getLogger()

Obtains the logger method provided by the context and returns a log output class. Logs are output in the format of Time-Request ID-Content by using the info method.

For example, use the info method to output logs:

log = context.getLogger()

log.info("test")

getAlias()

Obtains function alias.

As shown in Figure 1, you can use the context class in the code editor on the FunctionGraph console.

Figure 1 Using the context class

Helpful Links