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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.