Created
March 17, 2018 07:53
-
-
Save Abhinickz/f1e3621bc91cc958e7a13661735dd56c to your computer and use it in GitHub Desktop.
Perl Async Example: Use AnyEvent to run two separate code simultaneously in while (1) condition.
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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use AnyEvent; | |
my $count = { | |
cache => 0, | |
main_loop => 0, | |
}; | |
my $condvar = AnyEvent->condvar; | |
my $cache = AnyEvent->timer( # Runs as cache build subroutine in parallel. | |
after => 0, | |
interval => 1, | |
cb => sub { | |
print 'cache: ' . $count->{cache}++ . " \n"; | |
} | |
); | |
my $main_loop = AnyEvent->timer( # Runs as main while loop in parallel. | |
after => 0, | |
interval => 5, | |
cb => sub { | |
print_test(); | |
print 'main_loop : ' . $count->{main_loop}++ . " \n"; | |
} | |
); | |
# Runs both loop here. | |
$condvar->recv; | |
sub print_test{ | |
print "test\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment