简体   繁体   English

perl:多线程写和尾文件

[英]perl: multithread write and tail file

I'm working on a simple use-case of perl multi-thread: one thread writes to a file and another thread tails the file. 我正在研究perl多线程的简单用例:一个线程写入文件,另一个线程写入文件。 here is the code: 这是代码:

use strict;
use warnings;
use threads;
use File::Tail;

my $file = 'data.txt';
sub tail_file{
    my $file=File::Tail->new($file);
    while (defined(my $line=$file->read)) {
        print "$line";
    }
}

sub write_file{
    open (MYFILE, ">> $file");
    print MYFILE scalar localtime .  "  A data.\n";
    close (MYFILE); 
    print 'write done!';
}

my $t_tail = threads->new(\&tail_file);
$t_tail->join();

my $t_write = threads->new(\&write_file);
$t_write->join();

When running, this program is stuck on the console. 运行时,该程序卡在控制台上。

If you get rid of the call to $t_tail->join() , your program actually works fine. 如果你摆脱了对$t_tail->join()的调用,你的程序实际上工作正常。 You need to get rid of this call because your $t_tail thread will run forever (and thus will never join ), and your $t_write thread will never get kicked off. 你需要摆脱这个调用,因为你的$t_tail线程将永远运行(因此永远不会join ),你的$t_write线程将永远不会被启动。

You should know, however, that even if you get rid of that line, if the $t_write thread executes before the $t_tail thread (since the order of execution with threads is never guaranteed), then it may be the case you finish writing to the file before your File::Tail object is even initialized. 然而,你应该知道的是,即使你摆脱这一行的,如果$t_write线程之前执行$t_tail线程(因为使用线程是永远不会保证执行顺序),那么它可能是你写完的情况下, File::Tail对象之前的File::Tail甚至被初始化。 And since File::Tail only captures changes to the file that happen after it is initialized, it may look like nothing is happening at all. 而且由于File::Tail只捕获初始化发生的文件更改,因此看起来根本没有任何事情发生。

Lastly, you may think that File::Tail works similarly to the Linux/Unix tail , but the documentation shows that the default wait times used by the module are quite generous in comparison to the Linux/Unix counterpart: 最后,你可能会认为File::Tail的工作方式类似于在Linux / Unix tail ,但文件表明,该模块使用的缺省等待时间相比,在Linux / Unix对口相当慷慨:

maxinterval maxinterval

The maximum number of seconds (real number) that will be spent sleeping. 睡眠所花费的最大秒数(实数)。 Default is 60, meaning File::Tail will never spend more than sixty seconds without checking the file. 默认值为60,这意味着File :: Tail在不检查文件的情况下永远不会花费超过60秒。

interval 间隔

The initial number of seconds (real number) that will be spent sleeping, before the file is first checked. 首次检查文件之前将花费的初始秒数(实数)。 Default is ten seconds, meaning File::Tail will sleep for 10 seconds and then determine, how many new lines have appeared in the file. 默认值为10秒,这意味着File :: Tail将休眠10秒钟,然后确定文件中出现了多少新行。

If you run your program, and your File::Tail object does happen to get initialized before you start writing to the file, then you may have to wait a while (10 seconds) before you see anything on your console. 如果您运行程序,并且在开始写入文件之前确实初始化了File::Tail对象,那么在控制台上看到任何内容之前,您可能需要等待一段时间(10秒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM