简体   繁体   English

谷歌云 function 在测试期间崩溃,即使部署很好

[英]Google cloud function crashes during testing even though the deployment is fine

I built a cloud function (a very simple function - not very efficient).我建立了一个云 function(一个非常简单的 function - 不是很有效)。 Below is the code and screenshot-以下是代码和屏幕截图-

import functions_framework
import requests
@functions_framework.http
def hello():
    URL='https://api.openweathermap.org/data/2.5/weather?lat=36&lon=139&appid=a70f3b4f60b2f4c64e7aad63df5758d7'
    r = requests.get(url = URL)
    data = r.json()
    b={}
    b['temp']=data['main']['temp']
    b['pressure']=data['main']['pressure']
    return b
print(hello())

Delopyment code开发代码

I am trying to get the temperature and pressure for a location.我正在尝试获取某个位置的温度和压力。 The log is attached below and values are being fetched - log file日志附在下面,正在获取值 -日志文件

However, there is an error called "hello() takes 0 positional arguments but 1 was given" which I am unable to fix.但是,有一个名为“hello() 采用 0 位置 arguments 但给出 1”的错误,我无法修复。

Could someone please help me?有人可以帮我吗?

Cloud Functions will be executed under events and in your case an HTTP event. Cloud Functions 将在事件下执行,在您的情况下是 HTTP 事件。 The registered function (in this case hello ) will always receive an argument which is the HTTP request done to the function.已注册的 function(在本例中为hello )将始终收到一个参数,该参数是对 function 完成的 HTTP 请求。

In fact this is very clear in the docs :事实上,这在文档中非常清楚:

In Python, you register an HTTP handler function with the Functions Framework for Python.在 Python 中,您注册了一个 HTTP 处理程序 function 与 ZA7F5F35426B9287271B 的函数框架Your HTTP handler function must accept a Flask request object as an argument and return a value that Flask can convert into an HTTP response object. Your HTTP handler function must accept a Flask request object as an argument and return a value that Flask can convert into an HTTP response object.

The code could be:代码可以是:

import functions_framework
import requests

@functions_framework.http
def hello(request):
    URL='https://api.openweathermap.org/data/2.5/weather?lat=36&lon=139&appid=a70f3b4f60b2f4c64e7aad63df5758d7'
    r = requests.get(url = URL)
    data = r.json()
    b={}
    b['temp']=data['main']['temp']
    b['pressure']=data['main']['pressure']
    return b

I'd highly recommend to first read the docs since it seems you've not understood Cloud Functions completely.我强烈建议您先阅读文档,因为您似乎还没有完全理解 Cloud Functions。 Basically Cloud Functions work as a very tiny web app.基本上,云功能作为一个非常小的 web 应用程序工作。

As well I don't recommend to print the result.我也不建议打印结果。 The point of using an HTTP Cloud Function is to get an HTTP response and not going to the logs every time to view that.使用 HTTP Cloud Function 的目的是获得 HTTP 响应,而不是每次都去日志查看。

Finally the fact that the build is ok does not mean the function at runtime will work as expected.最后,构建正常的事实并不意味着 function 在运行时会按预期工作。

Declare hello like this:像这样声明你好:

def hello(request):

This also means you cannot call the function like this:这也意味着您不能像这样调用 function:

print(hello())

The Python decorator @functions_framework.http defines the functions's signature. Python 装饰器@functions_framework.http定义了函数的签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Google Cloud Function 中出现 python ArrtibuteError,阻止部署但我的代码在本地运行良好? - Why am I getting a python ArrtibuteError in my Google Cloud Function, preventing deployment but my code runs fine locally? 在谷歌云中执行谷歌云 function 时出错,但在本地工作正常 - Error executing google cloud function in google cloud, but working fine in local Google Cloud Data Fusion 无法访问来自另一个项目的数据,即使已授予访问权限 - Google Cloud Data Fusion failing to access data from another project, even though the access is granted 在本地测试 (Python) Google Cloud Function 时出现应用程序上下文错误 - Application context errors when locally testing a (Python) Google Cloud Function Quarkus 部署到 Google Cloud App 引擎失败 - Quarkus deployment to Google Cloud App engine fails MongoDb 在谷歌云平台上的多个区域部署 - MongoDb deployment at multiple regions on Google Cloud Platform 谷歌云 Django 应用部署 - 权限问题 - Google Cloud Django App Deployment - Permission Issues 谷歌云 Function NPM - Google Cloud Function NPM PyCaret 和谷歌云 Function - PyCaret and Google Cloud Function 测试谷歌云 Function - Test Google Cloud Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM