【发布时间】:2021-02-05 18:00:31
【问题描述】:
我正在尝试使用云功能通过每天调用一次外部 API 来更新数据。
到目前为止,我有:
-
云计划设置为调用函数 1
-
函数 1 - 遍历项目并为每个项目创建一个任务
-
任务 - 使用函数 1 提供的数据调用函数 2
-
函数 2 - 调用外部 API 来获取数据并更新我们的数据库
问题是每天有大约 2k 项要更新,并且云功能在它可以执行此操作之前会超时,因此我将它们放在队列中。但是,即使将项目放入队列中,云功能也需要很长时间,因此在将它们全部添加之前会超时。
有没有一种简单的方法可以一次将多个任务批量添加到队列中?
如果做不到这一点,是否有更好的解决方案?
全部用python编写
函数 1 的代码:
def refresh(request):
for i in items:
# Create a client.
client = tasks_v2.CloudTasksClient()
# TODO(developer): Uncomment these lines and replace with your values.
project = 'my-project'
queue = 'refresh-queue'
location = 'europe-west2'
name = i['name'].replace(' ','')
url = f"https://europe-west2-my-project.cloudfunctions.net/endpoint?name={name}"
# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)
# Construct the request body.
task = {
"http_request": { # Specify the type of request.
"http_method": tasks_v2.HttpMethod.GET,
"url": url, # The full url path that the task will be sent to.
}
}
# Use the client to build and send the task.
response = client.create_task(request={"parent": parent, "task": task})
【问题讨论】:
-
我了解到您已经提高了 Cloud Functions 的超时时间,这需要超过 9 分钟,您是否尝试过为 Cloud Functions 使用 higher tier?我的意思是使用一个 512Mb 的函数,它使用 800 MHz 的 CPU 或 1024 Mb,也许这有助于处理时间。
-
@Chris32 我认为问题不在于等待外部 API 的处理时间
标签: python google-cloud-platform google-cloud-functions google-cloud-scheduler google-cloud-tasks