部署Python版第二代Firebase云函数时如何配置参数?
Python版Firebase Cloud Function配置region与memory参数
一、全局配置(所有函数默认生效)
使用set_global_options方法可统一设置所有云函数的默认参数,比如区域、内存规格:
from firebase_functions import https_fn, options from firebase_admin import initialize_app import logging # 全局配置:设置默认区域为asia-northeast1,内存为512MB options.set_global_options( region="asia-northeast1", memory=options.MemoryOption.MB_512 ) initialize_app() logging.getLogger("Functions").setLevel(logging.INFO)
二、单个函数独立配置(覆盖全局设置)
如果需要给特定函数单独指定参数,直接在on_request或on_call装饰器中传入options参数,实例化HttpsOptions即可,支持配置region、memory、concurrency等属性:
针对HTTP触发函数(on_request)
@https_fn.on_request( options=https_fn.HttpsOptions( region="us-central1", # 单独设置该函数的区域 memory=options.MemoryOption.MB_1024, # 单独设置内存为1GB concurrency=500 # 设置该函数的最大并发数 ) ) def example(req: https_fn.Request) -> https_fn.Response: logging.info(f"Request example called : {req.auth}") return https_fn.Response("Hello world!")
针对Callable触发函数(on_call)
@https_fn.on_call( options=https_fn.HttpsOptions( region="asia-northeast1", memory=options.MemoryOption.MB_256, concurrency=300 ) ) def callable_example(req: https_fn.CallableRequest) -> dict: return {"message": "Hello from callable function!"}
参数说明
region:支持Firebase云函数的所有可用区域,比如asia-northeast1、us-central1等;memory:通过options.MemoryOption枚举选择,可选值包括MB_128、MB_256、GB_1、GB_2等;concurrency:设置函数的最大并发执行数量。
内容的提问来源于stack exchange,提问作者Tom3652
相关产品推荐
相关产品推荐

