Ajax / PHP - 我应该使用一个长时间运行的脚本还是轮询?(Ajax/PHP - should I use one long running script or polling?)

我有一个PHP脚本,通过ajax启动。 这个PHP脚本使用exec()通过shell运行单独的PHP脚本。

通过exec()调用的脚本可能需要30秒左右才能完成。 我需要在完成后更新UI。

哪个选项更受青睐?

a)保持HTTP连接打开30秒并等待它完成。 b)让exec()在后台运行PHP脚本,然后使用ajax轮询检查完成(每5秒左右)。 c)我还没有想过的其他东西。

谢谢你,Brian

I have a PHP script that is kicked off via ajax. This PHP script uses exec() to run a separate PHP script via the shell.

The script that is called via exec() may take 30 seconds or so to complete. I need to update the UI once it is finished.

Which of these options is preferred?

a) Leave the HTTP connection open for the 30 seconds and wait for it to finish. b) Have exec() run the PHP script in the background and then use ajax polling to check for completion (every 5 seconds or so). c) Something else that I haven't thought of.

Thank you, Brian

最满意答案

每隔几秒轮询一次服务器以获取更新。 当您将连接打开很长一段时间时,服务器或其浏览器可能会丢弃它们(如果HTTP请求花费的时间太长,则浏览器会超时)。

Poll the server for updates every few seconds. When you leave connections open for that long a period of time there's always the possibility that they may be dropped by the server or their browser (browsers timeout if an HTTP request takes too long).

更多推荐