aramsey has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use threads; use threads::shared; use Term::ProgressBar; ${$task . '_src_filesize'}; share(${$task . '_src_filesize'}); ${$task . '_dst_filesize'}; share(${$task . '_dst_filesize'}); # fork copy to a new thread, so it runs in the background ${'copy_' . $task . '_thread'} = threads->create(sub { print "Copying: $task/dist/$task.zip to /mnt/nfs/$task/$rel.zip\n" +; system("cp $task/dist/$task.zip /mnt/nfs/$task/$rel.zip"); while (${$task . '_src_filesize'} != ${$task . '_dst_filesize'} ) +{ ${$task . '_dst_filesize'} = stat("/mnt/nfs/$task/$rel.zip")-> +size; } if (-e "/mnt/nfs/$task/$rel.zip") { print "Copied: $task/dist/$task.zip to /mnt/nfs/$task/$rel.zip +\n"; unlink("$task/dist/$task.zip"); } }); $progress = Term::ProgressBar->new({ name => 'Copy', count => ${$task . '_src_filesize'}, ETA => 'linear', }); $progress->minor(0); while (${$task . '_src_filesize'} != ${$task . '_dst_filesize'}) { $progress->update(${$task . '_dst_filesize'}) }
I'm having some trouble using the progress bar for the first time. I'm running a copy in a thread and once everything else completes I want to monitor the progress. The example snippets I gave result in the bar completing @ 5%.

Replies are listed 'Best First'.
Re: Term::Progress bar completes at 5%
by bart (Canon) on Jun 19, 2012 at 20:07 UTC
    Nowhere you are setting $task.

    And please, don't abuse symbolic references this way. It just make me cringe.

    Also: I'm not convinced you'll see the destination of a file copy grow over time. More likely, the filesize will just jump from 0 to 100%; that's my guess, anyway.

      It's a snippit, the full script is assinging that variable. It's assigned in a foreach loop. The mount I'm copying to is an nfs share over a T1, so I would expect to see it grow over time. I can observe it grow from another shell. I'm not sure how else I should reference my thread objects while keeping them unique. Would you rather I nest it in a hash or what's best?
Re: Term::Progress bar completes at 5%
by zentara (Cardinal) on Jun 19, 2012 at 17:59 UTC
    Just off the top of my head, one thing worth trying is locking your shared variable everywhere you use it.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Sorry my code was horrible. That system call is going to block my while loop.