在本地测试Python Google Cloud Functions [英] Test Python Google Cloud Functions locally

查看:86
本文介绍了在本地测试Python Google Cloud Functions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google Cloud Functions上试用Python3.7运行时.我能够部署功能并使它们一旦部署就可以工作,但是,在部署之前,我似乎无法在本地运行模拟器来对其进行测试.

I'm trying out the Python3.7 runtime on Google Cloud Functions. I am able to deploy the functions and make them work once deployed, however, I can't seem to run the emulator to test them locally before I deploy.

Google的文档在告诉您在此处安装google函数模拟器的地方有点不一致: https: //cloud.google.com/functions/docs/emulator

Google's documentation is a little inconsistent where they tell you to install the google functions emulator here: https://cloud.google.com/functions/docs/emulator

但是在Firebase上,他们告诉您npm install firebase-admin,firebase-tools和firebase-functions.

But over on Firebase they tell you to npm install firebase-admin, firebase-tools and firebase-functions.

所有仿真器文档都引用了用JS编写的示例,而没有使用Python编写的示例,因此我想知道这些仿真器是否甚至在本地运行Python函数?

All of the emulator documentation references examples written in JS, none in Python so I'm wondering if these emulator even run Python functions locally?

谢谢

推荐答案

您可以使用功能框架让Python 在本地运行该功能.

You can use the Functions Framework for Python to run the function locally.

在名为main.py的文件中提供一个函数,如下所示:

Given a function in a file named main.py like so:

def my_function(request):
    return 'Hello World'

您可以这样做:

$ pip install functions-framework
$ functions-framework --target my_function

将在 http://localhost:8080 上启动本地开发服务器.

Which will start a local development server at http://localhost:8080.

要在本地为HTTP函数调用它:

To invoke it locally for an HTTP function:

$ curl http://localhost:8080

对于具有非二进制数据的后台功能:

For a background function with non-binary data:

$ curl -d '{"data": {"hi": "there"}}' -X POST \
-H "Content-Type: application/json" \
http://localhost:8080

对于具有二进制数据的后台功能:

For a background function with binary data:

$ curl -d "@binary_file.file" -X POST \
-H "Ce-Type: true" \
-H "Ce-Specversion: true" \
-H "Ce-Source: true" \
-H "Ce-Id: true" \
-H "Content-Type: application/json" \
http://localhost:8080

这篇关于在本地测试Python Google Cloud Functions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆