Functions Framework 上手教程
关联项目:Functions Framework 前置要求:Python 3.9+(具体支持版本以官方文档为准);pip;了解 Flask 的 request / response 基本概念;Google Cloud 账号与
gcloudCLI(仅部署时需要)。预计耗时:本地安装与跑通 HTTP 函数约 15 分钟;部署到 Cloud Functions / Cloud Run 视账号权限约 20–30 分钟。
官方仓库:github.com/GoogleCloudPlatform/functions-framework-python
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
创建虚拟环境并安装(版本以官方文档为准):
python -m venv .venv
source .venv/bin/activate # Windows 用 .venv\Scripts\activate
pip install functions-framework
python -c "import functions_framework, flask; print('ok')"
预期输出 ok;若需 CloudEvents 支持,安装带额外依赖的版本,以 PyPI 页面为准。
二、安装部署
验证 CLI:
functions-framework --help
预期:打印 --target、--port、--debug 等选项;若命令不存在,确认虚拟环境已激活,或改用 python -m functions_framework。
三、快速上手:跑通第一个示例
最小 HTTP 函数(保存为 main.py):
import functions_framework
@functions_framework.http
def hello_http(request):
name = request.args.get('name', 'World')
return f'Hello, {name}!'
本地启动与调用:
functions-framework --target=hello_http --debug
# 另一个终端
curl 'http://localhost:8080/?name=Ada'
预期输出:Hello, Ada!。--target 指定函数入口名;默认端口 8080,可用 --port 修改;--debug 开启 Flask 调试模式。返回 str 时框架自动包装为 200 响应,也可返回 flask.Response 或 (body, status, headers) 元组。
四、核心工作流
任务:写一个读取查询参数的函数并部署到 Cloud Run
- 写好
main.py(上面的hello_http),本地执行functions-framework --target=hello_http --debug并用 curl 验证返回Hello, Ada!。 - 准备
requirements.txt,例如functions-framework==3.*(版本以官方为准),可加flask。 - 登录并设置项目后部署到 Cloud Run:
gcloud auth login
gcloud config set project <PROJECT_ID>
gcloud run deploy hello-fn --source . --region <REGION> --allow-unauthenticated
预期:输出服务 URL;<PROJECT_ID>、<REGION> 按实际填写,具体参数以官方文档为准。
4. 调用部署后的服务:curl "<SERVICE_URL>/?name=Cloud",预期 Hello, Cloud!。
5. 查看日志:gcloud run services logs read hello-fn --region <REGION>,预期看到请求日志。
6. 清理:gcloud run services delete hello-fn --region <REGION>,避免持续产生费用。
五、进阶实践
- 事件函数:
import functions_framework
@functions_framework.cloud_event
def process_event(cloud_event):
print(cloud_event['type'], cloud_event.data)
触发事件类型与属性以官方文档为准。
- 返回自定义响应:from flask import Response; return Response('ok', status=201, mimetype='text/plain')。
- 函数工厂:返回可调用对象的工厂函数也可作为入口,细节以官方文档为准。
- 容器内运行:监听 PORT 环境变量指定端口,CMD ["functions-framework", "--target=hello_http"],基础镜像以官方文档为准。
- Cloud Functions gen2 部署:gcloud functions deploy hello_http --gen2 --runtime python312 --trigger-http --allow-unauthenticated --entry-point hello_http --source . --region <REGION>,运行时名称以官方支持列表为准。
六、常见问题
Q:functions-framework 命令找不到?
A:确认虚拟环境已激活,或改用 python -m functions_framework;检查 PATH 是否包含虚拟环境的 bin/Scripts 目录。
Q:--target 报找不到函数?
A:函数名需与模块中定义一致(装饰器不改变函数名);确认 --source 指向正确文件。
Q:事件函数在本地怎么触发? A:可按官方示例发送带 CloudEvents 相关头的 POST 请求,或使用 Cloudevents 工具;具体头名以官方文档为准。
Q:可以直接上生产吗? A:本地是 Flask 开发服务器;生产应部署到 Cloud Run / Cloud Functions,或换用正式 WSGI 服务器。
Q:与 Flask 有什么区别? A:框架只包装函数入口,适合短请求与事件处理;复杂路由与中间件建议直接用 Flask / FastAPI。
七、学习资源
- 快速上手:https://cloud.google.com/functions/docs/functions-framework
- Python 仓库与 README:https://github.com/GoogleCloudPlatform/functions-framework-python
- HTTP 函数文档:https://cloud.google.com/functions/docs/writing/write-http-functions
- 事件函数文档:https://cloud.google.com/functions/docs/writing/write-event-driven-functions
- Cloud Run 部署:https://cloud.google.com/run/docs/deploying