Test Python Google Cloud Functions locally

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-16 16:49:42

问题


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's documentation is a little inconsistent where they tell you to install the google functions emulator here: https://cloud.google.com/functions/docs/emulator

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

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?

Thanks


回答1:


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

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

def my_function(request):
    return 'Hello World'

You can do:

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

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

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



回答2:


Update

Please, use the official emulator and serving framework from GCP https://github.com/GoogleCloudPlatform/functions-framework-python

You can install it with

pip install functions-framework

Deprecated

Based on Dustin's answer I've developed a package to serve as emulator:

pip install gcp-functions-emulator

Given you want to serve the following function

# mycloudfunction.py
def api(request):
  return 'important data'

To emulate we have to call it like so:

gcpfemu <path/to/file.py> <function_name>

For example, with the code above we will call it:

gcpfemu mycloudfunction.py api

And to access the data we can use for example curl:

curl localhost:5000/api
> important data



回答3:


To run it in IntelliJ with Target Type = Script Path and the default options it should look like this:

from flask import Flask, request
app = Flask(__name__)


@app.route('/')
def hello():
    return hello_get(request)

if __name__ == '__main__':
    app.run('127.0.0.1', debug=True)



回答4:


See this project on GitHub: GoogleCloudPlatform/functions-framework

Currently there are only implementations in Node.js, Go, and PHP, but see Issue #5 about the Python implementation.

I suggest, whatever implementation you use, to follow the Functions Framework Contract

UPDATE: As Dustin mentioned, there is also a Python implementation now.



来源:https://stackoverflow.com/questions/53693987/test-python-google-cloud-functions-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!