Need AnyEvent help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need AnyEvent help
# 1  
Old 01-26-2011
Need AnyEvent help

I'm trying to learn AnyEvent but have very little experience with event handlers in general and have a hard time grasping the concept.

What I need is a script that runs as a daemon, watches some files, and every 60 seconds executes a function to print out some summaries.

So far I have this script which does not do what I want:

Code:
use AnyEvent;

sub do_report() {
  print "Inside do_report\n";
}

my $loop = AnyEvent->condvar;


my $report = AnyEvent->timer(
        after => 3,
        interval => 3,
        cb => sub {
                do_report();
        }
);

$loop->recv;

while ( 1 ) {
  print "Hello\n";
  sleep 1;
}


All this does is execute the do_report function every 3 seconds. What I was hoping for what a script which runs the while ( 1 ) loop and prints hello once per second AND also executes the do_report function every 3. Is this doable ? Or am I going about it the wrong way ? I suppose I need to use threads or fork here to accomplish what I'm hoping for ?

---------- Post updated at 04:44 PM ---------- Previous update was at 12:16 AM ----------

Since I try to help others around here I'll permit myself to do a shameless *bump* on this thread :-)
# 2  
Old 01-26-2011
It'd be helpful to tell people this is perl.

Most forum regulars probably haven't even heard of this module either, let alone used it or had it installed. I'll see if I can find it in my distro.

---------- Post updated at 07:06 PM ---------- Previous update was at 06:56 PM ----------

How about
Code:
#!/usr/bin/perl

use AnyEvent;

sub do_report() {
  print "Inside do_report\n";
}

my $loop = AnyEvent->condvar;


my $report = AnyEvent->timer(
        after => 3,
        interval => 3,
        cb => sub {
                do_report();
        }
);

$w = AnyEvent->idle (cb => sub { print "idle\n"; sleep 1; } );

$loop->recv;

exit 0;

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-27-2011
Perfect ! That gave me exactly what I needed :-)
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question