Perl 使用不同的进程在同一文件中读写

Perl 使用不同的进程在同一文件中读写,perl,Perl,我已经写了两个程序。一个程序同时将内容写入文本文件。另一个程序同时读取该内容 但是这两个程序应该同时运行。对我来说,程序是正确的,文件是正确的。但另一个程序不读取该文件 我知道,一旦写入过程完成,只有数据才会存储在硬盘中。然后另一个进程可以读取数据 但我希望在同一个文件中使用不同的进程同时读取和写入。我该怎么做 请帮帮我。 下面的代码将内容写入文件中 sub generate_random_string { my $length_of_randomstring=shift;# the l

我已经写了两个程序。一个程序同时将内容写入文本文件。另一个程序同时读取该内容

但是这两个程序应该同时运行。对我来说,程序是正确的,文件是正确的。但另一个程序不读取该文件

我知道,一旦写入过程完成,只有数据才会存储在硬盘中。然后另一个进程可以读取数据

但我希望在同一个文件中使用不同的进程同时读取和写入。我该怎么做

请帮帮我。 下面的代码将内容写入文件中

sub generate_random_string
{
    my $length_of_randomstring=shift;# the length of 
       # the random string to generate

    my @chars=('a'..'z','A'..'Z','0'..'9','_');
    my $random_string;
    foreach (1..$length_of_randomstring)
    {
        # rand @chars will generate a random 
        # number between 0 and scalar @chars
        $random_string.=$chars[rand @chars];
    }
    return $random_string;
}
#Generate the random string
open (FH,">>file.txt")or die "Can't Open";
while(1)
{
my $random_string=&generate_random_string(20);
sleep(1);
#print $random_string."\n";
print FH $random_string."\n";
}
下面的代码是读取的内容。这是另一个过程

 open (FH,"<file.txt") or die "Can't Open";
              print "Open the file Successfully\n\n";
              while(<FH>)
              {
                  print "$_\n";
              }

open(FH),您是在windows还是*nix上?您可以在*nix上通过使用tail来获取写入文件的输出,从而将类似的内容串在一起。在windows上,您可以调用CreateFile()使用FILE_SHARE_READ和/或FILE_SHARE_WRITE,以便在您打开该文件进行读/写时允许其他人访问该文件。您可能需要定期检查文件大小是否已更改,以便知道何时读取(我在这里不是100%确定)

另一个选项是内存映射文件。

这是可行的

作者:

use IO::File ();

sub generate_random_string {...}; # same as above

my $file_name = 'file.txt';
my $handle = IO::File->new($file_name, 'a');
die "Could not append to $file_name: $!" unless $handle;
$handle->autoflush(1);

while (1) {
    $handle->say(generate_random_string(20));
}
读者:

use IO::File qw();

my $file_name = 'file.txt';
my $handle = IO::File->new($file_name, 'r');
die "Could not read $file_name: $!" unless $handle;

STDOUT->autoflush(1);
while (defined (my $line = $handle->getline)) {
    STDOUT->print($line);
}

您可以使用一个复杂的合作协议,如下面所示。两端,
读卡器
写卡器
,在
轮流
模块中使用公共代码,处理诸如锁定和锁定文件所在位置等复杂细节。客户端只需指定当它们以独占方式访问文件

阅读器

#! /usr/bin/perl

use warnings;
use strict;

use TakeTurns;

my $runs = 0;
reader "file.txt" =>
       sub {
         my($fh) = @_;
         my @lines = <$fh>;
         print map "got: $_", @lines;
         ++$runs <= 10;
       };
#! /usr/bin/perl

use warnings;
use strict;

use TakeTurns;

my $n = 10;
my @chars = ('a'..'z','A'..'Z','0'..'9','_');

writer "file.txt" =>
       sub { my($fh) = @_;
             print $fh join("" => map $chars[rand @chars], 1..$n), "\n"
               or warn "$0: print: $!";
           };
轮流
模块正在工作:

pwrite

#! /usr/bin/perl

use warnings;
use strict;

my $pipe = "/tmp/mypipe";
system "mknod $pipe p 2>/dev/null";

open my $fh, ">", $pipe or die "$0: open $pipe: $!";

my $n = 10;
my @chars = ('a'..'z','A'..'Z','0'..'9','_');

while (1) {
  print $fh join("" => map $chars[rand @chars], 1..$n), "\n"
    or warn "$0: print: $!";
}
两端都试图使用创建管道,因为它们没有其他同步方法。至少有一个会失败,但只要管道存在,我们就不在乎了


正如您所看到的,所有等待的机器都由系统处理,因此您可以做您关心的事情:读写消息。

您已经尝试过了。您可以共享您尝试过的代码吗?尝试以非块模式读取文件您可能希望在写入程序中关闭该文件,然后再在读取程序中打开它。您是如何操作的您是否确保只有在写入之后才进行读取?这可能与您正在执行的操作有关: got: 1Upem0iSfY got: qAALqegWS5 got: 88RayL3XZw got: NRB7POLdu6 got: IfqC8XeWN6 got: mgeA6sNEpY got: 2TeiF5sDqy got: S2ksYEkXsJ got: zToPYkGPJ5 got: 6VXu6ut1Tq got: ex0wYvp9Y8
#! /usr/bin/perl

use warnings;
use strict;

my $pipe = "/tmp/mypipe";
system "mknod $pipe p 2>/dev/null";

open my $fh, "<", $pipe or die "$0: open $pipe: $!";

while (<$fh>) {
  print "got: $_";
  sleep 0;
}
#! /usr/bin/perl

use warnings;
use strict;

my $pipe = "/tmp/mypipe";
system "mknod $pipe p 2>/dev/null";

open my $fh, ">", $pipe or die "$0: open $pipe: $!";

my $n = 10;
my @chars = ('a'..'z','A'..'Z','0'..'9','_');

while (1) {
  print $fh join("" => map $chars[rand @chars], 1..$n), "\n"
    or warn "$0: print: $!";
}