如何读取不断更新的文件? [英] How do I read a file which is constantly updating?
问题描述
我从外部服务器获取数据流(文本格式),并希望将其逐行传递到脚本.该文件将以连续方式添加.这是执行此操作的理想方法.使用Perl的IO :: Socket方法可以吗?最终,这些数据必须通过PHP程序(可重复使用),并最终进入MySQL数据库.
I am getting a stream of data (text format) from an external server and like to pass it on to a script line-by-line. The file is getting appended in a continuous manner. Which is the ideal method to perform this operation. Is IO::Socket method using Perl will do? Eventually this data has to pass through a PHP program (reusable) and eventually land onto a MySQL database.
问题是如何打开不断更新的文件?
The question is how to open the file, which is continuously getting updated?
推荐答案
在Perl中,您可以使用 tell
从不断增长的内容中读取文件.可能看起来像这样(从 perldoc -f seek
大量借用)
In Perl, you can make use of seek
and tell
to read from a continuously growing file. It might look something like this (borrowed liberally from perldoc -f seek
)
open(FH,'<',$the_file) || handle_error(); # typical open call
for (;;) {
while (<FH>) {
# ... process $_ and do something with it ...
}
# eof reached on FH, but wait a second and maybe there will be more output
sleep 1;
seek FH, 0, 1; # this clears the eof flag on FH
}
这篇关于如何读取不断更新的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!