| Dan - Cpanel Support 2004-04-25, 8:32 pm |
| 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?
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
Anything even remotely like what I'm describing or am I just nuts
(probably a bit of both ;p). Mostly asking for academic purposes...
TIA
Lee.M - JupiterHost.Net
|