Skip to content

Instantly share code, notes, and snippets.

@hercynium
Created December 20, 2010 19:12
Show Gist options
  • Save hercynium/748834 to your computer and use it in GitHub Desktop.
Save hercynium/748834 to your computer and use it in GitHub Desktop.
test destroying and re-initializing an EV loop in a forked child
#!/usr/bin/env perl
use strict;
use warnings;
use English qw( -no_match_vars );
use Data::Dumper;
use EV;
use AnyEvent;
use AnyEvent::Strict;
# only timer1 should fire in the parent process
# and only timer2 should fire in the child.
my $timer1 = AnyEvent->timer(
after => .5,
interval => 1,
cb => sub { warn "\ttimer1 fired in pid [$PID]" },
);
my $cpid = fork();
unless ( defined $cpid ) {
die "fork failed: $!\n";
}
elsif ( $cpid == 0 ) {
warn "now in child process pid [$PID]\n";
EV::default_destroy();
EV::default_loop(); # you can call ->verify_loop on this, too but I dunno if it's useful
my $timer2 = AnyEvent->timer(
after => .5,
interval => 1,
cb => sub { warn "\ttimer2 fired in pid [$PID]" },
);
AnyEvent->condvar->wait;
}
warn "starting loop in parent pid [$PID]\n";
AnyEvent->condvar->wait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment