Created
November 14, 2012 06:07
-
-
Save riywo/4070597 to your computer and use it in GitHub Desktop.
ZMQ pub/sub example with perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こちらのエントリを単純にperlにしてみた。
http://ymotongpoo.hatenablog.com/entry/20120328/1332915057