Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Progress Bar Between Multiple System Calls

by monkfan (Curate)
on May 05, 2007 at 08:26 UTC ( #613697=perlquestion: print w/replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,
AFAIK, progress bar can only be applied under loops. For example we can use Term::ProgressBar or Smart::Comments. But is there away to do it for system call like this:
#!/usr/bin/perl system("code1.out param1 > output.txt"); system("code2.out param2 output.txt > output_final.txt");
How can I show Progress Bar between first (code1) system call and the second (code2) system call? Especially we don't know how long each system call will take.

Preferrably not using external CPAN module (except pre-installed module).


Replies are listed 'Best First'.
Re: Progress Bar Between Multiple System Calls
by BrowserUk (Pope) on May 05, 2007 at 11:46 UTC

    If you just want to show that progress is being made, rather how far things have got, monitoring the size of the output file is easy using a thread:

    Added a sleep to the display loop to prevent it from running away with the cpu. Using Win32::Sleep( 500 ); or selectundef, undef, undef, 0.5 might be better?

    #! perl -slw use strict; use threads; use threads::shared; my $outfile = 'output.txt'; unlink $outfile ; my $done:shared = 0; async{ system("dir /s > $outfile"); $done =1; }; sleep 1 until -e $outfile; printf STDERR "\r%d\t", -s( $outfile ) and sleep 1 until $done; print STDERR 'Output complete';

    If you can estimate the size of the output file--for example in your second command, there may be some rough correspondance between the size of the output from the first and that from the second--then you can easily convert the raw numbers into an estimated percentage. If your percentage runs over by a few percent and finishes at 105%, you'll be forgiven provided that you a) indicate that it is an estimate; b) it proves reasonably accurate.

    Alternatively, add 10% to your final size estimate. When the command finishes 'early' at say ~90%, no one will complain :)

    Wrapping that up into a function that takes the command, the path of the output file and an estimate of the final size is left as an exercise.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Progress Bar Between Multiple System Calls
by GrandFather (Sage) on May 05, 2007 at 08:42 UTC

    If you mean "How do I show progress of the system call" the general answer pretty much is "You can't". To show a sensible progress bar you have to have some way to provide a progress update that indicates how far through the process is. In the general case for a system call you can't do that. At best you could estimate the time that may be required and show a progress bar based on time (or similarly for an external measure of the progress - target file size for example) in a forked process. That's a pretty hairy and unsatisfactory solution though.

    DWIM is Perl's answer to Gödel
Re: Progress Bar Between Multiple System Calls
by ambrus (Abbot) on May 05, 2007 at 16:07 UTC
    print "[ ] 0%\r"; system("code1.out param1 > output.txt"); print "[############ ] 50%\r"; system("code2.out param2 output.txt > output_final.txt"); print "[########################] 100%\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://613697]
Approved by GrandFather
Front-paged by neversaint
[ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
[ambrus]: ething that's both cleanly organized and mostly automated.
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.
[Corion]: ambrus: Most of our "automation" is tied to process exit codes and a shell pipeline :-\
[LanX]: ... a week later they realized that one of the databases - which recorded how much the other banks due to this bank - was not correctly plugged
[ambrus]: Corion: I have no problem with exit codes and shell pipeline. My problem is that the current process requires a lot of manual intervention from me, including editing the source codes.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2017-03-29 11:49 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.