In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Dan - Cpanel Support) writes:
>Hello list!
>
>On a sleepless Friday night/Saturday morning a question cam to mind and 
>I had to ask...
>
>Assume I have a script that will run for 10 seconds.
>
>Is it possible to have a function executed every 2 seconds?
>
>I know it sounds weird but how would one go about something like that?

Based on your apparent requirement (below) for the functions to be
executed as child processes, I suggest you look into POE.
Interprocess communication is not trivial.  If you can guarantee
that no job will need to run past the time the next one needs to
start then it gets a whole lot easier.

Another possible module to build on is Parallel::ForkManager.

>Something like:
>
>#!/usr/bin/perl
>
>use strict;
>use warnings;
>
>#here's where it gets fake :)
>  use InternalTimer;
>
>  my $icron = InternalTimer new;
>
>  my $job = $icron->job('Name',sub { print 'This gets done every 2 
>seconds' }, 2);
>  $job->start;  # 1 or 0
>  sleep(10);    # would print 'This gets done every 2 seconds' 4 or 5 times
>  $job->run;    # execute it now
>  $job->error;  # error since last call to error
>  $job->status; # 1 = still running 0 = error, undef = not started or 
>stopped
>  $job->verbose;# verbose status form start or last call to verbose
>  $job->amt;    # how many times its run in this script
>  $job->stop;   # 1 or 0
># end fake example code ##
>
>any such beast?
>
>One halfway semi practical application I could see is, say you have a 
>long running app that prints the date out every so often ( I realize 
>they could use localtime each time but hear me out...
>
>
>my $now = localtime;
>my $job = $icron->job('Now',sub { $now = localtime; }, 1);
>
>print "$now\n"; # Sat Apr 24 00:57:03 2004
>sleep(3);       # or your long running code here ...
>print "$now\n"; # Sat Apr 24 00:57:06 2004
>print $job->error if !$job->status;
>$job->stop;
>sleep(3);
>print "$now\n"; # still Sat Apr 24 00:57:06 2004 because we stopped it

-- 
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to