长时间运行时定期输出到浏览器

发布于 2024-12-17 16:54:40 字数 154 浏览 5 评论 0原文

我使用 LOAD DATA INFILE 导入 11M 记录,然后脚本继续执行其他操作。导入需要 10 分钟,此时浏览器已经放弃(3 分钟后),并且我看不到后续操作的进度。有没有办法在运行这个耗时的查询时定期向浏览器输出一些内容。 phpmyadmin 似乎做了类似的事情,并且浏览器不会超时。

I'm using LOAD DATA INFILE to import 11M records and then the script continues with other operations. The import takes 10 minutes by which time the browser has given up (after 3 minutes) and I don't see the progress of the subsequent operations. Is there a way to output something to the browser periodically while this time consuming query is running. Phpmyadmin seems to do something like this and the browser doesn't time out.

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

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

发布评论

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

评论(1

云胡 2024-12-24 16:54:40

查看flush() (http://php.net/manual/en/function.flush .php) 或 ob_flush() (http://www.php.net/manual/en/function.ob-flush.php)。

基本上,您通常会生成输出(在本例中为当前状态),然后强制服务器输出缓冲区中当前的内容。

如果您的操作需要 10 分钟,您可能需要使用 set_time_limit($seconds) (http://php.net/manual/en/function.set-time-limit.php),请注意不要将其设置为某个较大的值,以防页面挂起。

如果您想每 10%(1 分钟)输出一次状态,您可以在每次更新刷新后将超时重置为 2 分钟。这样,如果脚本运行超出了预期时间,它就会超时。

Check out flush() (http://php.net/manual/en/function.flush.php) or ob_flush() (http://www.php.net/manual/en/function.ob-flush.php).

Basically you generate your output (current status in this case) normally then force the server to output what is currently in the buffer.

If your operation is taking 10 minutes you will likely need to use set_time_limit($seconds) (http://php.net/manual/en/function.set-time-limit.php), be careful you don't just set it to some big amount in case the page does hang.

If you want to output status every 10% (1 minute) you could reset the timeout for 2 minutes after each update has been flushed. This way your script will timeout if it goes beyond expected time running.

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