Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Reading progress of "copy" executed asynchronously

by vr (Curate)
on Aug 10, 2018 at 12:48 UTC ( [id://1220191]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Reading progress of "copy" executed asynchronously
in thread Reading progress of "copy" executed asynchronously

This works for me on Windows:

use strict; use warnings; use feature 'say'; use POSIX ":sys_wait_h"; use Capture::Tiny ':all'; my $cmd = 'cmd /c copy a1GBfile a1GBfile_copy /z'; my $out = IO::File-> new_tmpfile; capture_stdout { my $pid = system( 1, $cmd ); while () { last if waitpid( $pid, WNOHANG ); undef local $/; seek $out, 0, 0; my $s = <$out>; next unless $s and $s =~ m/.*\D(\d+)%/s; say STDERR "progress so far: $1 percent"; sleep 1; } } stdout => $out; close $out; __END__ progress so far: 0 percent progress so far: 60 percent progress so far: 70 percent progress so far: 80 percent progress so far: 89 percent progress so far: 99 percent

I'd prefer to use filehandle opened on Perl scalar, but Capture::Tiny isn't happy about the idea. + You can tee_stdout instead of capture_stdout, to see "normal" output in console, and replace say STDERR ... line with real logic of what you trying to do. Or, to see if something is still happening, just check -s $out for change instead of slurping.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found