Skip to content

Instantly share code, notes, and snippets.

@riywo
Created November 14, 2012 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riywo/4070597 to your computer and use it in GitHub Desktop.
Save riywo/4070597 to your computer and use it in GitHub Desktop.
ZMQ pub/sub example with perl
use strict;
use warnings;
use ZMQ;
use ZMQ::Constants qw/ZMQ_PUB/;
use Time::HiRes qw/usleep/;
my $cxt = ZMQ::Context->new();
my $pub = $cxt->socket( ZMQ_PUB );
$pub->bind( "tcp://*:9999" );
my $msg_id = 0;
while (1) {
my $zip = rand(10) + 10001;
my $temp = rand(215) - 80;
my $rel = rand(50) + 10;
my $update = sprintf("%05d %d %d %d", $zip, $temp, $rel, $msg_id);
$msg_id += 1;
print $update."\n";
usleep 100000;
$pub->send($update);
}
use strict;
use warnings;
use ZMQ;
use ZMQ::Constants qw/ZMQ_SUB ZMQ_SUBSCRIBE/;
my $cxt = ZMQ::Context->new();
my $sub = $cxt->socket( ZMQ_SUB );
$sub->connect( "tcp://localhost:9999" );
my $zipfilter = scalar @ARGV > 1 ? $ARGV[0] : "10001 ";
$sub->setsockopt(ZMQ_SUBSCRIBE, $zipfilter);
my $samples = 10;
my $total_temp = 0.0;
for my $updates (1..$samples) {
my $msg = $sub->recv();
my ($zip, $temp, $relh, $msg_id) = split / /, $msg->data();
printf("zip:%s, temp:%s. relh:%s, id:%s\n", $zip, $temp, $relh, $msg_id);
$total_temp += $temp;
}
printf("average temprature for zipcode '%s' was '%f'\n", $zipfilter, $total_temp / $samples);
@riywo
Copy link
Author

riywo commented Nov 14, 2012

こちらのエントリを単純にperlにしてみた。
http://ymotongpoo.hatenablog.com/entry/20120328/1332915057

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