Skip to content

Instantly share code, notes, and snippets.

@domm

domm/w2.pl

Created Dec 7, 2012
Embed
What would you like to do?
AnyEvent based worker
#!/usr/bin/perl
use 5.016;
use AnyEvent;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw(ZMQ_SUB ZMQ_SUBSCRIBE ZMQ_RCVMORE ZMQ_FD);
my $context = zmq_init();
my $subscriber = zmq_socket($context, ZMQ_SUB);
zmq_connect($subscriber, 'tcp://localhost:5556');
zmq_setsockopt($subscriber, ZMQ_SUBSCRIBE, 'request');
my %stat;
my $cnt=0;
my $quit_program = AnyEvent->condvar;
my $fh = zmq_getsockopt( $subscriber, ZMQ_FD );
my $w = AnyEvent->io(
fh => $fh,
poll => "r",
cb => sub {
while ( my $recvmsg = zmq_recvmsg( $subscriber, ZMQ_RCVMORE ) ) {
my $msg = zmq_msg_data($recvmsg);
$msg=~s/^request //;
say "someone accessed $msg";
$cnt++;
$stat{ $msg }++;
if ($cnt % 10 == 0 ) {
foreach (sort keys %stat) {
say "$_\t$stat{$_}";
}
}
}
});
$quit_program->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment