【发布时间】:2019-09-14 10:44:52
【问题描述】:
我有一个云函数在 python 中调用另一个云函数。我的问题是,当我调用下一个函数时,第一个函数等待其执行或超时。
关键是这是关于 Google Cloud Functions 的。特别是函数超时和最大 API 调用率之间的不匹配。我的问题是该函数的最大超时时间(540 秒)比我需要进行所需 API 调用的时间短,而且我不想创建更多触发器。
在调用第二个函数之后,我怎样才能让第一个(“调用者”)完成它的工作? 一些示例代码:
# main.py
# url: this-particular-cloud-function
# function initiated with a post request containing {"previous_tin_index": 0}
import requests
import time
import logging
final_tin_index = 100
def eat_spam(request):
started_eating_spam = time.time()
spam_json = request.get_json()
spam_to_eat = spam_json["previous_tin_index"]
for spam in range(spam_to_eat):
time.sleep(5)
previous_tin_index += 1
logging.info("I hate spam....")
finished_previous_spam_time = time.time() - started_eating_spam
if finished_previous_spam_time >= 10:
logging.info("Make it stop!")
requests.post("this-particular-cloud-function", json={"previous_tin_index": previous_tin_index})
return "200"
编辑:我知道固有的问题是函数永远不会达到返回值。我想知道这是否可以修复,例如,将代码重写为 Javascript 承诺。
附:我查看了Cloud Documentation,但在特定示例中似乎缺少python。
【问题讨论】:
标签: python google-cloud-functions