tencent cloud

Serverless Cloud Function

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Related Concepts
How It Works
Strengths
Scenarios
Related Products
Purchase Guide
Billing Overview
Billing Mode
Billable Items and Billing Modes
Function Computing Power Support
Free Tier
SCF Pricing
Billing Example
Payment Overdue
Getting Started
Creating Event Function in Console
User Guide
Quota Management
Managing Functions
Web Function Management
Log Management
Concurrence Management
Trigger Management
Function URL
A Custom Domain Name
Version Management
Alias Management
Permission Management
Running Instance Management
Plugin Management
Managing Monitors and Alarms
Network Configuration
Layer Management
Execution Configuration
Extended Storage Management
DNS Caching Configuration
Resource Managed Mode Management
Near-Offline Resource Hosting Model
Workflow
Triggers
Trigger Overview
Trigger Event Message Structure Summary
API Gateway Trigger
COS Trigger
CLS Trigger
Timer Trigger
CKafka Trigger
Apache Kafka Trigger
MQTT Trigger
Trigger Configuration Description
MPS Trigger
CLB Trigger Description
TencentCloud API Trigger
Development Guide
Basic Concepts
Testing a Function
Environment Variables
Dependency Installation
Using Container Image
Error Types and Retry Policies
Dead Letter Queue
Connecting SCF to Database
Automated Deployment
Cloud Function Status Code
Common Errors and Solutions
Developer Tools
Serverless Web IDE
Calling SDK Across Functions
Third-Party Tools
Code Development
Python
Node.js
Golang
PHP
Java
Custom Runtime
Deploying Image as Function
Web Framework Development
Deploying Framework on Command Line
Quickly Deploying Egg Framework
Quickly Deploying Express Framework
Quickly Deploying Flask Framework
Quickly Deploying Koa Framework
Quickly Deploying Laravel Framework
Quickly Deploying Nest.js Framework
Quickly Deploying Next.js Framework
Quickly Deploying Nuxt.js Framework
Quickly Deploying Django Framework
Use Cases
Overview
Solutions with Tencent Cloud Services
Business Development
TRTC Practices
COS Practices
CKafka Practice
CLS
CLB Practice
MPS
CDN
CDWPG
VOD
SMS
ES
Scheduled Task
Video Processing
Success Stories
Tencent Online Education
Online Video Industry
Tencent Online Education
Best Practice of Tencent IEG Going Global
API Documentation
History
Introduction
API Category
Making API Requests
Other APIs
Namespace APIs
Layer Management APIs
Async Event Management APIs
Trigger APIs
Function APIs
Function and Layer Status Description
Data Types
Error Codes
SDK Documentation
FAQs
General
Web Function
Billing FAQs
Network FAQs
Log FAQs
SCF utility class
Event Handling FAQs
API Gateway Trigger FAQs
Related Agreement
Service Level Agreement
Contact Us
Glossary

Deployment Methods

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-12-02 18:12:22

Function Form

The Python function form is generally as follows:
import json

def main_handler(event, context):
print("Received event: " + json.dumps(event, indent = 2))
print("Received context: " + str(context))
return("Hello World")

Execution Method

An execution method should be specified when each SCF function is created. The execution method of the Python programming language is similar to index.main_handler, where index refers to the index.py entry file, and main_handler refers to the main_handler entry function. When submitting the zip code package by uploading a local zip package or uploading it through COS, the root directory of the package should contain the specified entry file and the file should contain the specified entry function. The filename and function name should match those entered in the execution method to ensure successful execution.

Input Parameters

The input parameters in the Python environment include event and context, both of which are of the Python dict type.
event: this parameter is used to pass the trigger event data.
context: this parameter is used to pass runtime information to your handler.
The event parameter varies with trigger or event source. For more information on its data structure, see Trigger Overview.

Response

Your handler can use return to return a value. The return value will be handled differently depending on the function invocation type.
In the Python environment, a serializable object such as dict object can be directly returned:
def main_handler(event, context):
resp = {
"isBase64Encoded": false,
"statusCode": 200,
"headers": {"Content-Type":"text/html","Key":["value1","value2","value3"]},
"body": "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
}
return(resp)
Two methods are available to return values:
Sync invocation: the return value of a sync invocation will be serialized in JSON and returned to the caller for subsequent processing. The function testing in the console is sync invocation, which can capture the function’s return value and display it after the invocation is completed.
Async invocation: the return value of an async invocation will be discarded, since the invocation method responds right after the function is triggered, without waiting for function execution to complete.
Note:
The return value of both sync and async invocations will be recorded in the function logs.

Exception Handling

You can throw an exception using raise Exception inside the function.
If the exception is captured and handled before returning without being thrown outside, the function is considered to have been executed. In this case, information specified in the entry function’s return will be returned. The following sample codes show that the function is successfully executed and will return Hello World.
# -*- coding: utf8 -*-
def main_handler(event, context):
try:
print("try exception")
raise Exception("err msg")
except Exception as e:
print(e)
return("Hello World")
If an exception is not captured before returning, it will be thrown outside the entry function and captured by SCF. In this case, the function is considered to have failed, and an error message will be returned. The following sample codes show that the function execution fails.
# -*- coding: utf8 -*-
def main_handler(event, context):
print("try exception")
raise Exception("err msg")
return("Hello World")
The function returns information similar to the following:
{
"errorCode":-1,
"errorMessage":"user code exception caught",
"requestId":"a325b967-ef5b-4aa3-a329-c6bb0df72948",
"stackTrace":"Traceback (most recent call last):\\n File \\"/var/user/index.py\\", line 4, in main_handler\\n raise Exception(\\"err msg\\")\\nException: err msg",
"statusCode":430
}
The errorCode field indicates a code error, and errorMessage provides error details. The stackTrace field indicates an error stack, and statusCode provides error details. For more information about statusCode, see Function Status Code.

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백