谷歌应用引擎 + jquery ajax 延迟 +光标

发布于 2024-12-29 11:13:21 字数 991 浏览 6 评论 0原文

我使用谷歌应用程序引擎来运行我的某些项目。由于它对请求施加 30 秒的时间限制,我使用游标和内存缓存来限制数据获取过程。

问题陈述:

  1. 我有一个 ajax 调用日志,它基本上是我需要从服务器获取的所有详细信息的列表,由某些用户交互创建,
  2. 调用日志中的每个 ajax 调用都可能包含数据,这可能会导致达到 30 秒的限制。所以我需要将其分解并部分返回。
  3. 日志中的所有调用完成后,我需要根据返回数据执行一些自定义逻辑。

现在,我已经有了一个以这种方式工作的解决方案:

  1. 使用一些随机标识符启动第一个 ajax 调用
  2. 返回时,检查一些参数以确定是否已获取所有必需的数据。
  3. 如果没有,则使用与步骤 1 中相同的随机标识符再次执行调用 + 保存到目前为止已获取的所有数据
  4. 如果是,则从日志中删除该项目并为下一个项目重复步骤

但所有这些代码当前正在处理中在 ajax 调用成功的情况下。我读到了 jQuery Deferreds ,它似乎是修复糟糕代码的一个非常好的候选者目前正在雇用的人。

我已经弄清楚了以下内容:

  1. 使用 $.when.apply(null, arrayOfDeferreds) 创建各种动态调用列表 - 阅读 jQuery.when
  2. 我现在可以使用 deferred.done 函数为每个调用保存数据转储
    问题是,我如何告诉 deferred 如果需要再次调用该请求,则不要解析 ajax 请求 - 只需保存我的数据并使用与初始调用相同的随机标识符再次执行调用?也就是说,基本上重复调用我的完成函数,直到保存所有数据?如果有更好的方法,请提供解释。

I use the google app engine to run certain projects of mine. Because of the 30 second time limit that it imposes on requests, I use cursors and memcache to throttle the data fetch process.

Problem statement:

  1. I have an ajax call log which is basically a list of all the details I need from the server, created by some user interaction
  2. Each ajax call in the call log may have data, that may cause the 30 second limit to hit. So I need to break it down and return it in parts.
  3. Once all the calls in the log have been completed, I need to perform some custom logic, based on the return data.

Now, I already have a solution that works in this way:

  1. Initiate the first ajax call with some random identifier
  2. On return, check some parameter to identify whether all the required data has been acquired.
  3. If not, then perform the call again, with the same random identifier as in step 1 + save whatever data has been fetched so far
  4. If yes, remove the item from log and repeat steps for next item

But all of this code is currently being handled in the success event of the ajax call. I read about jQuery Deferreds and it seems like a really good candidate to fix the atrocious code that is currently being employed.

I've already figured out the following:

  1. Use $.when.apply(null, arrayOfDeferreds) to create a dynamic call list of sorts - read jQuery.when
  2. I can now use the the deferred.done function to save a data dump for each call
    The problem being, how do I tell the deferred that if the request needs to be called again, then don't resolve the ajax request - just save my data and perform the call again with the same random identifier as its initial call? That is, basically calling my done function repeatedly until all the data is saved ? If there is a better way to do this, please provide an explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2025-01-05 11:13:21

您可以按如下方式递归使用管道:

function myFetch( id ) {
    return $.ajax({
        url: serviceURL,
        data: {
            id: id,
            someOthers: data
        }
    }).pipe(function( data ) {
        saveData( data );
        if ( !isOK( data ) ) {
            return myFetch( id );
        }
    });
}

ajax 请求仅定义一次,并且您可以在管道处理程序中有条件地重新发出它。

You can use pipe recursively as follows:

function myFetch( id ) {
    return $.ajax({
        url: serviceURL,
        data: {
            id: id,
            someOthers: data
        }
    }).pipe(function( data ) {
        saveData( data );
        if ( !isOK( data ) ) {
            return myFetch( id );
        }
    });
}

The ajax request is only defined once and you conditionaly re-issue it in the pipe handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文