如何逐行读取文件10秒?(How can I read a file line by line for 10 seconds?)

编程入门 行业动态 更新时间:2024-10-24 23:22:20
如何逐行读取文件10秒?(How can I read a file line by line for 10 seconds?)

我正在开展一个项目,我需要阅读日志并检查错误。 我正在使用while循环:

while($count < 10) { $count++; # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = $sdata[count($sdata)-1]; if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } sleep(1); }

这是每秒检查一次日志,但是,我需要在它进入时读取每一行。如何在保持while循环的超时值的同时读取日志的最后一行?

I'm working on a project where I need to read a log and check for errors. I'm using a while loop like so:

while($count < 10) { $count++; # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = $sdata[count($sdata)-1]; if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } sleep(1); }

This is checking the log every second, however, I need to read EVERY line as it comes in. How do I read the last line of the log as it is written while maintaining a timeout value for the while loop?

最满意答案

这样做你需要的吗?

$time = time(); $stop = $time+10; while($time < $stop) { $time = time(); # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = end($sdata); if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } }

Does this do what you need?

$time = time(); $stop = $time+10; while($time < $stop) { $time = time(); # Stop waiting if an exception is thrown $sdata = file($logfile); $lastline = end($sdata); if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; } }

更多推荐

本文发布于:2023-07-31 02:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340455.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   read   seconds   line   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!