Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have been working on a command line script that makes several system calls, some of which take a few seconds (minutes?) to return. The long pauses give the impression that the script has hung, which isn't great. I figured I could address this by adding some kind of visual "still processing" feedback, like an hourglass or pinwheel. Because it's just a command line script, I do it with dots.
My code is below. I would appreciate any feedback or suggestions for improvement. Thank you.
use strict; use threads; use threads::shared; sub SystemCall { my $call = join ' ', @_; local $| = 1; # run hot pipes print "Executing system call '$call'"; # Start a thread to print out dots indicating that Perl hasn't hun +g my $thread_beat = 0.1; # Should be small, but not so small as to f +lood the screen with dots. my $stop :shared = 0; my $thread = threads->create( {'context' => 'void'}, sub { while ( not $stop ) { print '.'; select( undef,undef,undef, $t +hread_beat ) } }); $thread->detach(); # Run the command my $result; eval { chomp( $result = `$call` ) }; warn $@ if $@; # eval failed. # Signal the thread to end. $stop = 1; # And wait one beat for the thread to end. select( undef,undef,undef, $thread_beat + 0.01 ); print $/; # Present error message status, if any. warn "\$? = '$?'\n" if defined $? and $?; warn "\$! = '$!'\n" if defined $! and $!; warn "\$@ = '$@'\n" if defined $@ and $@; warn "\$^E = '$^E'\n" if defined $^E and $^E; warn "\$^W = '$^W'\n" if defined $^W and $^W; if ( $result ) { print $result, $/ } return defined $result ? $result : ''; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using a thread to provide visual feedback.
by BrowserUk (Patriarch) on Jun 21, 2018 at 20:03 UTC | |
by marioroy (Prior) on Jun 22, 2018 at 15:10 UTC | |
by stevieb (Canon) on Jun 22, 2018 at 02:45 UTC | |
|
Re: Using a thread to provide visual feedback.
by bliako (Abbot) on Jun 21, 2018 at 16:49 UTC | |
by Anonymous Monk on Jun 21, 2018 at 19:54 UTC | |
by bliako (Abbot) on Jun 22, 2018 at 00:06 UTC | |
|
Re: Using a thread to provide visual feedback.
by Anonymous Monk on Jun 21, 2018 at 16:38 UTC |