Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created April 23, 2010 05:16
Show Gist options
  • Save kazeburo/376214 to your computer and use it in GitHub Desktop.
Save kazeburo/376214 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use AE;
use AnyEvent::MPRPC::Server;
use AnyEvent::Curl;
use Parallel::Prefork;
my $ua = AnyEvent::Curl->new;
my $server = AnyEvent::MPRPC::Server->new( port => 4423 );
$server->reg_cb(
req => sub {
my ($res_cv, @params) = @_;
$server->{exit_guard}->begin;
my $cv = $ua->add( $params[0] );
$cv->cb( sub {
my $res = shift->recv;
$server->{exit_guard}->end;
$res_cv->result(${$res->{header}});
} );
},
);
my $pm = Parallel::Prefork->new({
max_workers => 2,
trap_signals => {
TERM => 'TERM',
HUP => 'TERM',
USR1 => undef,
}
});
while ( $pm->signal_received ne 'TERM' ) {
$pm->start and next;
$ua->start;
my $exit = $server->{exit_guard} = AE::cv;
$exit->begin;
my $w; $w = AE::signal TERM => sub { $exit->end; undef $w };
$exit->recv;
$pm->finish;
}
$pm->wait_all_children();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment