Skip to content

Instantly share code, notes, and snippets.

@Abhinickz
Created March 17, 2018 07:53
Show Gist options
  • Save Abhinickz/f1e3621bc91cc958e7a13661735dd56c to your computer and use it in GitHub Desktop.
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.
#!/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