AJAX:如何获得进展反馈网络应用程序,并避免长时间请求超时?长时间、如何获得、应用程序、进展

2023-09-10 13:35:11 作者:八月柒秋叶初凉

这是关于如何使Web应用程序将接收上载的数据,过程中,它的大量的,并返回结果,都没有可怕的纺纱沙滩球5分钟或可能的HTTP超时总体设计问题

This is a general design question about how to make a web application that will receive a large amount of uploaded data, process it, and return a result, all without the dreaded spinning beach-ball for 5 minutes or a possible HTTP timeout.

下面的要求:

请在Web表单,您可以上传包含URL列表的CSV文件 当用户点击提交,服务器获取的文件,并检查每个URL如果活着看到,什么网页的标题标签。 的结果是包含URL下载的CSV文件,结果HTTP code 输入CSV可以非常大(> 100000行),这样的获取过程可能需要5-30分钟。

我的解决办法,到目前为止是有一个旋转的JavaScript循环在客户站点,查询服务器每秒钟以确定工作的整体进度。这似乎缺憾给我,我很犹豫接受这是最好的解决方案。

My solution so far is to have a spinning javascript loop on the client site, which queries the server every second to determine the overall progress of the job. This seems kludgy to me, and I'm hesitant to accept this as the best solution.

我用perl,模板工具包,和jQuery,但使用任何网络技术的任何解决方案,是可以接受的。

I'm using perl, template toolkit, and jquery, but any solution using any web technology would be acceptable.

编辑: 的一个可能的解决方案的一个例子是在这样一个问题:http://stackoverflow.com/questions/333664/simple-long-polling-example-$c$c

edit: An example of a possible solution is in this question: http://stackoverflow.com/questions/333664/simple-long-polling-example-code

推荐答案

您可以做到这一点使用AJAX,但你可能会得到与像实施彗星更好的实时结果。我相信,彗星的实现是专门设计来解决一些超时限制,但我没有使用任何,所以我不能提供一个直接的指导。

You can do this with AJAX but you may get better real-time results with a COMET like implementation. I believe that COMET implementations are specifically designed to get around some timeout limitations but I haven't used any so I can't offer a direct guide.

无论哪种方式,我的建议是手头宽裕的工作,另一个进程一旦获得到服务器。

Either way my recommendation is to hand off the work to another process once it gets to the server.

我已经工作了许多不同的解决方案,这种性质和一个批次的任务我最喜欢的是手头宽裕的一批工作到另一个进程。在这样的系统中上传页面手拿开的工作,以一个单独的处理器并立即与指示用于返回用户监控过程

I've worked a number of different solutions for batch tasks of this nature and the one I like the best is to hand off the batch work to another process. In such a system the upload page hands off the work to a separate processor and returns immediately with instructions for the user to monitor the process.

的批处理器可以以几种方式来实现:

The batch processor can be implemented in a couple of ways:

叉和分离的孩子IO,完成批量处理。家长完成Web请求。 保存上传内容到的处理队列(例如:文件系统上的文件,在数据库中的记录),并具有Web服务器通知一个外部处理器 - 或者定制守护程序,或者一个现成的调度,如在对于* nix系统。

然后,您可以为用户提供多种方式来监视过程:

You can then offer the user multiple ways to monitor the process:

上传确认页包含所述批处理过程的一同步实时监视器(通过彗星或Flash)。当完成该确认页可指示用户将其下载。 和上面一样,但显示器不活,而是通过AJAX或页面元刷新使用定期轮询 在队列监视页面,显示他们,他们正在运行的批处理的状态。

的批处理器可以沟通其状态通过多种方法

The batch processor can communicate it's status via a number of methods:

更新数据库系统记录 生成处理日志 使用命名管道

有许多好处,以递的code切换到另一个进程:

There are a number of benefits to handing the code off to another process:

在这个过程将会继续,当用户不小心将停止浏览器。 在使用外部程序迫使你的方式,可以让你脱离你的显示器并重新连接随时沟通批次状态。例如:当用户不小心导航远离页面之前的过程完成 这是更容易,如果你决定你需要发$ P $垫出你的批量处理发生在低的网络流量小时,实现批量限制和推迟。 您不必担心网络超时(无论是客户端还是服务器端)。 您可以重新启动Web服务器,而无需担心是否你打断一个批处理过程。