Perl Anyevent,非阻塞redis push

Perl Anyevent , non blocking redis push

我有下面的代码来执行非阻塞 rpush 到 redis 服务器 当我 运行 这只是 1 rpush 时,代码工作正常 但是当我在 while 循环中 运行 this 时,脚本在第一次执行后挂起。 为什么 ?

#!/usr/bin/perl                                                                                                                                                          
use AnyEvent;
use AnyEvent::Redis::RipeRedis;
use strict;
#my $cv = AE::cv();                                                                                                                                                      

my $redis = AnyEvent::Redis::RipeRedis->new(
  host     => 'localhost',
  port     => '6379',
    );

my $i=0;

my $cv;
while($i++ < 5) {
    $cv = AnyEvent->condvar;
    $redis->rpush( 'list', "1","2","3",
                   { on_done => sub {
                   my $data = shift;
                   print "$data\n";
                     },
                   }
        );
    $cv->recv();
}
$redis->quit(
    sub {$cv->send();}
    );
$cv->recv();

请注意你还没有定义,connection_timeout:

my $redis = AnyEvent::Redis::RipeRedis->new(
  host     => 'localhost',
  port     => '6379',
    );

应该看起来像:

  my $redis = AnyEvent::Redis::RipeRedis->new(
    host => 'localhost',
    port => '6379',
    password => 'your_password',
    connection_timeout => 5,
    reconnect => 1,
    encoding => 'utf8');

发件人:

ftp://ftp.uni-siegen.de/pub/CPAN/authors/id/I/IP/IPH/AnyEvent-Redis-RipeRedis-1.002.readme

当您在 while 循环中调用 $cv->recv() 并且脚本正在等待 $cv->send 或 $cv->croak 时,您阻止脚本执行,但在回调中您不调用 $cv->发送().

$cv->recv

Wait (blocking if necessary) until the ->send or ->croak methods have been called on $cv, while servicing other watchers normally.

如果您想发送不同的非阻塞请求,请尝试使用 AnyEvents 开始和结束方法。

#!/usr/bin/perl
use AnyEvent;
use AnyEvent::Redis::RipeRedis;
use strict;

my $redis = AnyEvent::Redis::RipeRedis->new(
  host     => 'localhost',
  port     => '6379',
);

my $i=0;

my  $cv = AnyEvent->condvar;
while($i++ < 5) {
   $cv->begin;
   $redis->rpush( 'list', "1","2","3",
               { 
                  on_done => sub {
                    my $data = shift;
                    print "$data\n";
                    $cv->end();
                  },
               }
    );
}

$cv->recv();