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 : ''; }
In reply to Using a thread to provide visual feedback. by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |