AJAX:如何在网络应用程序中获取进度反馈,并避免长请求超时?
这是一个一般性设计问题,涉及如何创建一个 Web 应用程序,该应用程序将接收大量上传的数据、处理数据并返回结果,而所有这些都不会出现 5 分钟可怕的旋转沙滩球或可能的 HTTP 超时。
要求如下:
- 制作一个 Web 表单,
- 当用户单击“提交”时,您可以在其中上传包含 URL 列表的 CSV 文件,服务器获取该文件,并检查每个 URL 是否存在,以及其标题标签是什么该页面是。
- 结果是包含 URL 的可下载 CSV 文件,并且
- 输入 CSV 的结果 HTTP 代码可能非常大(> 100000 行),因此获取过程可能需要 5-30 分钟。
到目前为止,我的解决方案是在客户端站点上有一个旋转的 JavaScript 循环,它每秒查询服务器以确定作业的总体进度。这对我来说似乎很笨拙,我犹豫是否接受这是最好的解决方案。
我正在使用 Perl、模板工具包和 jquery,但使用任何 Web 技术的任何解决方案都是可以接受的。
编辑: 可能的解决方案的一个例子是这个问题:如何实现基本的“长轮询”?< /a>
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.
Here's the requirements:
- make a web form where you can upload a CSV file containing a list of URLs
- when the user clicks "submit", the server fetches the file, and checks each URL to see if its alive, and what the title tag of the page is.
- the result is a downloadable CSV file containing the URL, and the result HTTP code
- the input CSV can be very large ( > 100000 rows), so the fetch process might take 5-30 minutes.
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.
I'm using perl, template toolkit, and jquery, but any solution using any web technology would be acceptable.
edit:
An example of a possible solution is in this question: How do I implement basic "Long Polling"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 AJAX 来完成此操作,但使用类似 COMET 的实现可能会获得更好的实时结果。我相信 COMET 实现是专门为解决一些超时限制而设计的,但我没有使用过任何超时限制,所以我无法提供直接指南。
无论哪种方式,我的建议都是在工作到达服务器后将工作移交给另一个进程。
对于这种性质的批处理任务,我已经采用了多种不同的解决方案,而我最喜欢的解决方案是将批处理工作移交给另一个进程。在这样的系统中,上传页面将工作交给单独的处理器,并立即返回,并提供用户监控流程的指令。
批处理器可以通过多种方式实现:
然后,您可以为用户提供多种监控流程的方法:
批处理器可以通过多种方法传达其状态:
将代码交给另一个进程有很多好处:
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:
You can then offer the user multiple ways to monitor the process:
The batch processor can communicate it's status via a number of methods:
There are a number of benefits to handing the code off to another process:
最简单的是批处理甚至流式处理作业。如果您将其视为页面上的数据表。如果表中有> 100000 条记录您是否一次请求所有记录。我会这样做:
发送下载文件的请求。
发送请求以处理 100 条(任意数量)记录。
a.处理记录。
b.保存到临时 csv 文件。
c.回复完成/未完成流程状态。
d.如果状态为未完成,请重复第二步。
The simplest would be to batch process or even to stream the job. If you treat it like a data table you have on your page. If the table has > 100000 records would you just request all records at once. I would do this:
Send a request to download file.
Send a request to process 100 (arbitrary number) records.
a. Process records.
b. Save to temporary csv file.
c. Response back with status of complete / not complete process.
d. If status is not complete repeat step two.
您提到客户端不可信任,因此我建议(在客户端)预先解析每 X 条记录的文件,将校验和附加到每个记录子集,然后允许客户端通过固定数量的连接进行上传代理,以便您可以更准确地监控进度。
You mentioned that the client cannot be trusted, so I recommend (on the client side) pre-parsing the file per X number of records, appending a checksum to each subset of records, then allowing the client a fixed number of connections to upload through the proxy so you can monitor the progress more accurately.