长时间运行时定期输出到浏览器 [英] Periodically output to browser on long operation

查看:67
本文介绍了长时间运行时定期输出到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LOAD DATA INFILE导入1100万条记录,然后脚本继续执行其他操作.导入过程需要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.

推荐答案

签出flush()(

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.

如果您的操作耗时10分钟,则可能需要使用set_time_limit($ seconds)( http://php.net/manual/zh-CN/function.set-time-limit.php ),请注意,不要只是将其设置为较大值,以防页面出现错误挂.

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.

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

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.

这篇关于长时间运行时定期输出到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆