This can be done by offloading the work that takes a long time to a separate script, and calling that one from a primary script.
The first file (call.pl) calls via a background system() call to the script that does the actual work that takes a long time (busy.pl). While busy.pl is doing stuff, the call.pl loops over to check for a lock file, and will inform the user that work is going on in the background.
In the busy.pl file, replace sleep() with the code that takes a long time, and edit call.pl to your liking so it notifies however often you want it to.call.pl
#!/usr/bin/perl # call.pl use warnings; use strict; my $lock_file = 'lock.lck'; print "$0: Calling subtask script...\n"; system("/usr/bin/perl busy.pl &"); my $start = 0; while(1){ # on first loop, sleep two seconds before checking # for the lock file sleep(2) if ! $start; $start++; if (-e $lock_file){ print "$0: Subtask still running...\n"; sleep(1); next; } print "$0: Done!\n"; exit; }
busy.pl
#!/usr/bin/perl # busy.pl use warnings; use strict; my $lock_file = 'lock.lck'; open my $lock, '>', $lock_file or die "Can't grab a lock file: $!"; print "$0: Starting subtask...\n"; sleep(5); print "$0: Ending subtask...\n"; close $lock; unlink $lock_file;
Output:
$ perl call.pl call.pl: Calling subtask script... busy.pl: Starting subtask... call.pl: Subtask still running... call.pl: Subtask still running... call.pl: Subtask still running... call.pl: Subtask still running... busy.pl: Ending subtask... call.pl: Done!
-stevieb
In reply to Re: marking time during function call
by stevieb
in thread marking time during function call
by lycanhunter
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |