无需等待响应即可调用云函数 [英] Call cloud functions without waiting for response

查看:26
本文介绍了无需等待响应即可调用云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里尝试使用云功能进行一些小技巧,但似乎无法弄清楚问题所在.

Trying a little hack here with cloud functions but can't seem to figure out what the issue is.

我目前正在使用 now.sh 来托管无服务器函数,并希望从另一个函数调用 1 个函数.假设我声明了 2 个函数 fetchData &设置数据.当调用 setData 函数时,它会处理一些数据,然后调用 fetchData 函数.

I'm currently using now.sh to host serverless functions and would like to call 1 function from another. Lets say I have 2 functions declared fetchData & setData. When the setData function is called it processes some data and then calls the fetchData function.

export const setData = async (req: Request, res: Response) => {
  await axios.post(
    fetchDataEndpointUrl,
    {
      params: {
        key,
      },
    },
  );

  return res.json({
    payload: true,
  });

}

上面的代码工作正常,但是完成整个操作所用的时间是 setData 函数调用 + fetchData 函数调用完成所用的时间.我想要做的是调用 fetchData 而不必等待它完成基本上删除 axios 调用中的 await .我试过删除 await 但是当 setData 调用结束时调用突然结束.有没有办法解耦这个动作而不必等待 setData 函数完成?

The above code works fine but the time taken for the entire operation to complete would be setData function call + the time taken for the fetchData function call to complete. What I'm trying to do is make a call to fetchData without having to wait for it to complete essentially removing the await in the axios call. I've tried removing the await but the call just ends abruptly when the setData call ends. Is there a way to decouple this action and not have to wait for the setData function to complete?

推荐答案

你的问题的总结似乎是当你调用一个云函数时,你希望它能够在执行后台的同时向调用者返回一个值例如调用另一个服务并等待响应.

The summary of your question appears to be that when you call a Cloud Function, you want it to be able to return a value to its caller while simultaneously performing background work such as calling another service and waiting for a response.

当您将值返回给 Cloud Function 的调用者时,即是 Cloud Function 生命周期的结束.除了回报之外,您无法确定 Cloud Function 中的任何生命.

When you return a value to the caller for a Cloud Function, that is the end of the life span of the Cloud Function. You can not be assured of any kind of life in the Cloud Function beyond the return.

此处记录了这一点.

正文(部分)如下:

函数只能访问请求的资源(CPU 和内存)在函数执行期间.代码在外部运行执行周期不保证执行,可以停止随时.因此,您应该始终表示您的结束函数执行正确,避免运行超出它的任何代码.

A function has access to the resources requested (CPU and memory) only for the duration of function execution. Code run outside of the execution period is not guaranteed to execute, and it can be stopped at any time. Therefore, you should always signal the end of your function execution correctly and avoid running any code beyond it.

问题的另一部分是,如果我们想让我们的云函数在我们不关心等待响应的情况下进行异步 REST 调用,该怎么办.我们能否在不等待嵌套 REST 调用完成的情况下从 Cloud Function 返回?

Another part of the question is what if we want to have our Cloud Function make an asynchronous REST call where we don't care about waiting for the response. Can we return from the Cloud Function without waiting for the nested REST call to complete?

答案是也许,但这取决于被调用服务的性质以及我们如何调用它.要了解其中的细微差别,请记住 JavaScript 是一种单线程语言.没有多个执行线程.在我们的 JavaScript 应用程序中,一次只会发生一件事.如果我们进行异步 REST 调用并且不关心回复但(显然)确实关心请求已发送,那么我们需要在终止 Cloud Function 之前同步发送请求.如果我们开始使用库包而不深入研究它们的性质,这可能会变得很棘手.例如,一个 REST 调用可能包括:

The answer is maybe but it will depend on the nature of service being called and how we are calling it. To appreciate the nuances in this remember that JavaScript is a single threaded language. There aren't multiple threads of execution. In our JavaScript app, only one thing will ever happen at a time. If we make an asynchronous REST call and don't care about a reply but (obviously) do care that the request has been sent then we need to synchronously send the request before we terminate the Cloud Function. This can get tricky if we start using library packages without delving into their natures. For example, a REST call might include:

  • 与合作伙伴的套接字连接
  • 出站请求的传输
  • 收到响应时的回调

在结束顶层云函数调用之前,我们需要绝对确定传输已经发生.此外,如果我们确实结束了顶级 Cloud Function 调用,那么很可能会降低用于响应的套接字.这可能会导致调用的 REST 服务出现异常,现在无法返回其 200 响应.

We need to be absolutely sure that the transmission has happened before we end the top level Cloud Function call. In addition, if we do end the top level Cloud Function call, that may very well tare down the socket used for the response. This could result in an exception in the called REST service that now is unable to return its 200 response.

为了在我们不需要也不想等待响应的地方运行工作,GCP 为此提供了一个结构化的解决方案.它被称为云任务"(请参阅​​ 云任务).在您的 Cloud Function 中,您将定义一个异步调用嵌套服务的请求,并将该请求交给 Cloud Tasks 执行.Cloud Tasks 会确认收到执行请求,您现在可以放心,它会并且可以在最高级别返回.

To run work where we don't need to nor want to wait for a response, GCP provides an architected solution for this. It is called "Cloud Tasks" (see Cloud Tasks). Within your Cloud Function, you would define a request to call your nested service asynchronously and hand that request off to Cloud Tasks to execute. Cloud Tasks would acknowledge receipt of the request to execute and you can now be assured that it will and can return at the highest level.

这篇关于无需等待响应即可调用云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆