Perl Anyevent non blocking push

I have the code below to make non-blocking rpush on the redis server. When I run it in just 1 rpush, the code works fine. But when I run this while loop, the script freezes after the first run. What for?

#!/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();
+4
source share
2 answers

You block script execution when you call $ cv-> recv (), while the loop and script wait for $ cv-> send or $ cv-> croak, but you do not call $ cv β†’ Send () on the callback.

$ cv-> recv

Wait (if necessary, block) until the methods β†’ send or β†’ croak are called in $ cv, usually serving other observers.

, 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();
0

, 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');

From:

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

0

All Articles