关于分段 AJAX 响应的进一步问题

发布于 2024-09-12 20:24:03 字数 1181 浏览 1 评论 0原文

这个问题与我之前问过的问题相关,请参阅此处

作为实现分段 ajax 响应的一种方法,我创建了一个代码来执行以下操作:

客户端首先调用初始化进程的脚本。在服务器端,startScript.cgi 代码开始生成数据,在执行此操作时,它将响应分组为块,并将它们写入按顺序索引的单个文件中(chunk1.txt chunk2.txt 等)。 startScript.cgi 启动此进程后,客户端立即开始第二个 ajax 请求,发送到 GatherOutput.cgi,参数为 ?index=0。

GatherOutput.cgi 看到请求,然后查找 'chunk'.$index.'.txt',然后返回数据。客户端将其输出到 html,然后使用参数 ?index=1 等开始第二个 ajax 请求到 GatherOutput.cgi。此过程将持续到报告来自 startScript.cgi 的所有数据为止。

如果gatherOutput.cgi无法找到“chunk$index.txt”,它会进入这个循环:

until(-e "$directory/chunk$index.txt")
{
    #nothing
}
open $fh, "<$directory/chunk$index.txt" || warn "File not found. blah blah";
#Read file and print, etc...

注意,startScript.cgi运行的代码可能需要很长时间才能完成,所以重点是同时广播来自startScript.cgi的旧输出:它正在产生新的产出。

这样做的问题是性能会受到影响,尽管输出是很久以前创建的,但仍需要一段时间才能出来。我假设这是由于与 startScript.cgi 中的 CPU 操作相比,硬盘访问非常慢,因此 GatherOutput.cgi 经常等待写入新块,或者客户端经常等待 GatherOutput.cgi 读取文件等。尽管可能还有其他问题。

有人有任何想法或建议来解决这个问题吗?或者,如果有人对这个问题有不同的方法,那就太好了。

顺便说一句,startScript.cgi 可能只被调用一次,它启动一个持续运行的大型任务系统任务(带有系统转义符,例如 exec、system 或反引号),并且无法进行分段。

This question is related to one I asked previously, see here.

As a way to implement segmented ajax responses, I created a code which does this:

The client first calls the script which initializes the process. On the server side, the startScript.cgi code starts generating data, and as it does this, it groups the responses into chunks, and writes them into individual files indexed sequentially (chunk1.txt chunk2.txt etc). Immediately after startScript.cgi starts this process, the client side begins a second ajax request, sent to gatherOutput.cgi, with parameter ?index=0.

gatherOutput.cgi sees the request, and then looks in 'chunk'.$index.'.txt' and then returns the data. The client outputs this to html, and then begins a second ajax request to gatherOutput.cgi with parameter ?index=1, etc. This continues until all of the data from startScript.cgi is reported.

If gatherOutput.cgi cannot locate "chunk$index.txt", it goes into this loop:

until(-e "$directory/chunk$index.txt")
{
    #nothing
}
open $fh, "<$directory/chunk$index.txt" || warn "File not found. blah blah";
#Read file and print, etc...

Note, startScript.cgi runs code which may take a long time to complete, so the point is to simultaneously broadcast older output from startScript.cgi as it is generating new output.

The problem with this is that the performance suffers, and output would take a while to come out despite being long ago created. I'm assuming this is due to harddrive access being very slow compared to the CPU operations in startScript.cgi, so gatherOutput.cgi is frequently waiting on the new chunk to be written, or the client is frequently waiting for gatherOutput.cgi to read the files, etc. Though there could be other issues.

Does any one have any ideas or suggestions to fix this problem? Or if anyone has a different approach to this problem that'd be great as well.

By the way, startScript.cgi may only be called once, it starts a large task system task (with system escapes such as exec, system, or backticks) that keeps running, and can't fathomably be segmented.

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

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

发布评论

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

评论(1

拒绝两难 2024-09-19 20:24:04

当文件不存在时,您的 GatherOutput.cgi 不应陷入循环。相反,向您的 AJAX 请求返回一个状态,表明该文件尚不存在,然后让它等待(使用 setInterval 或 setTimeout),并在几秒钟后重试。

这在您的服务器上会容易得多。对于用户,您仍然可以显示加载图形或其他内容,让他们知道该过程仍在后台发生。

Your gatherOutput.cgi shouldn't drop into a loop when the file doesn't exist. Instead return a status to your AJAX request that the file doesn't exist yet and then have it wait (using setInterval or setTimeout) and try again after so many seconds.

That will be MUCH easier on your server. For the user you can still show a loading graphic or something else that let's them know the process is still happening in the background.

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