简体   繁体   English

为什么当无服务器 function 在 Firebase 云 Z2764085932F8B 中成功完成 Promise.all() 时退出时性能会下降?

[英]Why does performance degrade when a serverless function exits without completing Promise.all() successfully in Firebase Cloud Function?

Why does performance degrade when a serverless function exits without completing Promise.all() successfully in Firebase Cloud Function?为什么当无服务器 function 在 Firebase 云 Z2764085932F8B 中成功完成 Promise.all() 时退出时性能会下降?

In the past, I used to run Promise.all() without await in Cloud Function.过去,我曾经在 Cloud Function 中运行Promise.all()而无需await Then, this would cause the Cloud Function to exit without waiting for Promise.all() to complete.然后,这将导致云 Function 退出而不等待Promise.all()完成。 As a result, my Cloud Function sometimes worked correctly and sometimes did not.结果,我的 Cloud Function 有时可以正常工作,有时不能正常工作。

This is problem, so we fixed it to await Promise.all() to wait for all the processing to finish.这是问题,所以我们修复它await Promise.all()等待所有处理完成。

Then, the Cloud Function, which used to take several minutes to complete, now completes in a few seconds.然后,以前需要几分钟才能完成的 Cloud Function 现在只需几秒钟即可完成。

I am curious about this issue and would like to understand it.我对这个问题很好奇,想了解一下。 Why is it that when I fix all async process to wait, the function completes immediately?为什么当我修复所有异步进程等待时,function 立即完成?

Thanks.谢谢。

It's simply the serverless model of Google Cloud.它只是 Google Cloud 的无服务器 model。 You pay when you process traffic, I mean, when your request is handled.您在处理流量时付费,我的意思是,在处理您的请求时付费。

When you have sent a response, you stop to pay, and so, Google limit the CPU available to your Cloud Functions (about 5% of the CPU available).当您发送响应后,您将停止付款,因此,Google 会限制您的 Cloud Functions 可用的 CPU(大约 5% 的可用 CPU)。 Indeed, if you don't pay, Google threshold the CPU and propose the processing power to other Cloud Function that handle traffic, and which pays for it.实际上,如果您不付费,Google 会设置 CPU 阈值,并向其他处理流量的 Cloud Function 提供处理能力,而后者会为此付费。

To free the memory reserved by your cloud functions, after a while (about 15 minutes) Google stop the instance.要释放您的云功能保留的 memory,请稍后(约 15 分钟)Google 停止实例。


So, in your case, if you send the response before the end of the processing, the processing will continue in background with a low percentage of CPU.因此,在您的情况下,如果您在处理结束之前发送响应,则处理将在后台以较低百分比的 CPU 继续进行。 Either it finishes or it doesn't have the time to finish before the instance stop;它要么完成,要么在实例停止之前没有时间完成;

Now, because you await all the promises before sending the response, you have 100% of the CPU power for you (because you pay for it) and you finish quickly the processing.现在,因为您在发送响应之前等待所有承诺,所以您拥有 100% 的 CPU 能力(因为您为此付费)并且您可以快速完成处理。

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

相关问题 如何传递Promise.all返回另一个函数Cloud Functions for Firebase - How to pass a Promise.all return to another function Cloud Functions for Firebase Firebase 云 Function - 承诺:可以添加到 Promise.all() 的“承诺”数量是否有任何限制? - Firebase Cloud Function - Promises : Is there any limitation to the number of 'promises' that can be added to Promise.all()? 类型错误:未定义不是函数 - Cloud Functions 中的 Promise.all() 错误 - Type error: undefined is not a function - Promise.all() error in Cloud Functions Firebase云功能promise.all - Firebase cloud functions promise.all 为什么一个 function 不等待 Promise.all() 在另一个 function 中解析? - Why does one function not wait for Promise.all() to resolve in another function? Promise.所有返回 function - Promise.all returns function 返回嵌套的Promise.all结果在Firebase函数onCall中 - Return nested Promise.all result in firebase function onCall 为什么 Firebase 云 Function 执行定义的 Promise - Why does Firebase Cloud Function execute defined Promise 处理等待 Promise.all() 拒绝而没有 then() function - Handle await Promise.all() rejections without then() function 为什么这个 Promise.all() function 只返回最后解析的值? - Why does this Promise.all() function only return the last resolved value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM