Skip to content

Instantly share code, notes, and snippets.

@jzawodn
Created October 29, 2010 17:06
Show Gist options
  • Save jzawodn/653912 to your computer and use it in GitHub Desktop.
Save jzawodn/653912 to your computer and use it in GitHub Desktop.
AnyEvent Perl BLPOP loop. Has memory leak.
#!/usr/bin/perl -w
$|=1;
use strict;
use lib '/home/jzawodn/code/AnyEvent-Redis/lib';
use AnyEvent::Redis;
my $host = 'localhost';
my $port = 6379;
my @chan = 'a'..'z'; # which queues/channels to check
my $command_timeout = 1; # how long to wait for a response
my $done_cv = AnyEvent->condvar;
$done_cv->begin;
my $redis = AnyEvent::Redis->new(host => $host, port => $port,
on_error => sub { warn @_; $done_cv->end; }
);
sub handler;
sub handler {
my ($stuff) = @_;
if ($stuff) {
my ($key, $msg) = @$stuff;
print "got $msg via $key\n";
} else {
print "blpop timeout\n";
}
$redis->blpop(@chan, $command_timeout, sub { handler(@_); });
};
handler();
$done_cv->recv;
exit;
@jzawodn
Copy link
Author

jzawodn commented Oct 29, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment