0
我想觀看一組文件以進行更改,並且這樣做沒有CPU和電池的巨大損失。理想情況下,我的perl代碼可以在macos和linux上運行,但前者更重要。我試過使用perl監視文件的變化(macos和linux)
我試過Mac::FSEvents
,它在macos上工作,似乎很適合用於目錄,但對於文件來說,我無法看到。
my $fs = Mac::FSEvents->new('try.txt');
my $fh= $fs->watch;
my $sel = IO::Select->new($fh);
while ($sel->can_read) {
my @events = $fs->read_events;
for my $event (@events) {
printf "File %s changed\n", $event->path;
}
}
它根本不響應;並且很有希望更OS無關
use File::Monitor;
my $monitor = File::Monitor->new();
my @files= qw(try.txt);
foreach (@files) { $monitor->watch($_); }
消耗100%的CPU。單獨的$monitor-watch()
不會阻止。我也試過
use File::Monitor;
my $monitor = File::Monitor->new();
$monitor->watch('try.txt', sub {
my ($name, $event, $change) = @_;
print "file has changed\n";
});
但這立即返回。
我發現了另一個,
use File::ChangeNotify;
my $watcher =
File::ChangeNotify->instantiate_watcher
(directories => [ './' ],
filter => qr/try\.txt/,
);
# blocking
while (my @events = $watcher->wait_for_events()) {
print "file has changed\n";
}
但是CPU利用率再高(70%)。
也許這些都是錯誤的cpan模塊。有人可以給我建議,我應該如何做到這一點?
問候,
/IAW