PHP显示进度的飞行信息进度、信息、PHP

2023-09-10 17:44:58 作者:关于我们.

我的工作在处理大量的数据,并需要一段时间才能完成在PHP中的工具。我想保持用户更新是怎么回事,和当前任务的处理。

I am working in a tool in PHP that processes a lot of data and takes a while to finish. I would like to keep the user updated with what is going on and the current task processed.

什么是您认为做到这一点的最好方法是什么?我有一些想法,但不能决定一个最有效的:

What is in your opinion the best way to do it? I've got some ideas but can't decide for the most effective one:

老办法:执行脚本的一小部分,并显示一个页面,用一个元重定向或JavaScript的定时器,对用户发送的请求,继续脚本(如 /脚本的.php?步= 2 )。

发送Ajax请求不断地读取PHP不断通过fwrite的更新服务器文件()。

Sending AJAX requests constantly to read a server file that PHP keeps updating through fwrite().

同上,但PHP更新保存文件在数据库中的字段来代替。

Same as above but PHP updates a field in the database instead of saving a file.

有没有任何这些声音好?任何想法?

Does any of those sound good? Any ideas?

谢谢!

推荐答案

而不是写一个静态文件,您将获取与AJAX或额外的数据库字段,为什么不能有另外一个PHP脚本,只是返回一个完成百分比指定任务。你的页面就可以通过更新一个非常轻量级的Ajax请求的进展表示PHP脚本。

Rather than writing to a static file you fetch with AJAX or to an extra database field, why not have another PHP script that simply returns a completion percentage for the specified task. Your page can then update the progress via a very lightweight AJAX request to said PHP script.

对于实施这一进步的剧本,我可以提供更多的建议,如果我有更多的了解,以你所说的处理大量的数据的意思。如果你正在写一个文件,你的进步的脚本可以简单地检查文件的大小,并返回完成百分比。对于更复杂的任务,你可以分配基准,以特定的过程和在此基础上的过程已经完成最后的或正在运行的回归估计完成百分比。

As for implementing this "progress" script, I could offer more advice if I had more insight as to what you mean by "processes a lot of data". If you are writing to a file, your "progress" script could simply check the file size and return the percentage complete. For more complex tasks, you might assign benchmarks to particular processes and return an estimated percentage complete based on which process has completed last or is currently running.

更新

这是建议的方法,以检查的进展的积极脚本,只是等待一个请求的响应。我有我使用了类似的方法的数据挖掘应用程序。

This is one suggested method to "check the progress" of an active script which is simply waiting for a response from a request. I have a data mining application that I use a similar method for.

在你的脚本,让你在等待,你可以存储(要检查进度的脚本)(在一个文件或数据库中,我使用一个数据库,因为我有数以百计的进程中运行的要求在任何时候,所有需要跟踪他们的进步,我还有一个脚本,让我监视这些进程的进展情况)的进程中取得进展变量。当这个过程开始时,将其设置为1。您可以方便地选择检查站的剧本任意数量会通过,并计算考虑到当前的检查点的百分比。对于一个大的请求,但是,你可能更想知道的大概%的请求已完成。一个可能的解决办法是知道返回内容的大小,并根据在任何时刻接收到的百分比设置状态变量。即如果收到请求的数据在一个循环中,每次迭代可以更新状态。或者,如果你下载到的平面文件,你可以查询该文件的大小。这可能会随着时间的推移进行精确度较低(而不是文件大小),如果你知道的大致时间请求应才能完成,只是比较的脚本当前执行的时间。显然,这些都不是完美的解决方案,但我希望他们会给你一些洞察到你的选择。

In your script that makes the request you're waiting for (the script you want to check the progress of), you can store (either in a file or a database, I use a database as I have hundreds of processes running at any time which all need to track their progress, and I have another script that allows me to monitor progress of these processes) a progress variable for the process. When the process begins, set this to 1. You can easily select an arbitrary number of 'checkpoints' the script will pass and calculate the percentage given the current checkpoint. For a large request, however, you might be more interested in knowing the approximate percent the request has completed. One possible solution would be to know the size of the returned content and set your status variable according to the percentage received at any moment. I.e. if you receive the request data in a loop, each iteration you could update the status. Or if you are downloading to a flat file you could poll the size of the file. This could be done less accurately with time (rather than file size) if you know the approximate time the request should take to complete and simply compare against the script's current execution time. Obviously neither of these are perfect solutions, but I hope they'll give you some insight into your options.