[英]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())
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.