调用云功能而无需等待响应 [英] Call cloud functions without waiting for response

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

问题描述

在这里尝试使用云功能进行一些修改,但似乎无法弄清楚问题出在哪里.

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

我当前正在使用now.sh托管无服务器功能,并且想从另一个调用1个功能.可以说我有两个声明为 fetchData &的函数. setData .调用 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?

推荐答案

问题的摘要似乎是,当您调用Cloud Function时,您希望它能够在执行后台操作的同时向其调用者返回一个值诸如调用另一项服务并等待响应之类的工作.

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中除了回报以外的任何 life .

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.

问题的另一部分是,如果我们想让我们的Cloud Function进行异步REST调用,而又不关心等待响应,该怎么办.我们可以从Cloud Function返回,而不必等待嵌套的REST调用完成吗?

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调用之前发生了传输.此外,如果我们确实结束了顶层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天全站免登陆