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

monks,

i am sort of re-asking this question. I would really like to be able to start an FTP process and check on its status periodically so that I can continue to update my GUI while my (several and large) files are transferring.

Right now my application only needs to run on Win32, but I am thinking about making it platform-independent. It does look like Win32::Internet will do this via the InternetStatusCallback function. Right now that looks like my best option, as I don't know too much about forking or threading...

Is there a reasonably simple way to use a common module like LWP or Net::FTP to do something like this:

$ftp->start_job; while ($ftp->status != $the_code_for_complete) { update_gui(); }

-ariel

Replies are listed 'Best First'.
Re: Asynchronous FTP 2
by InfiniteSilence (Curate) on Sep 23, 2005 at 22:23 UTC
    The file sizes on both systems (target and host) will be in bytes, so you can check the size on the server at the beginning of your session of the files and compare it (by polling or otherwise) on the target system. If they are the same, your downloads are complete.

    Or

    If you check the perldoc for Net::FTP you will see that it knows how much data it has read thus far:

    bytes_read () Returns the number of bytes read so far.

    Celebrate Intellectual Diversity

      As a variation on using bytes_read(), one can use methods of the dataconn class of Net::FTP to report periodic progress of the transfer. For example, assuming an $ftp object has been created, one can use the read method for getting a file:
      my $file = 'some_file'; my $size = $ftp->size($file); my $bsize = 1024; my $report_size = $bsize * 1000; my ($len, $buffer); my $so_far = 0; my $chunk_size = 0; $ftp->binary; my $data = $ftp->retr($file); open(my $fh, '>', $file) or die qq{Cannot open $file: $!}; binmode $fh; do { $len = $data->read($buffer, $bsize); $so_far += $len; $chunk_size += $len; if ($chunk_size >= $report_size) { printf("Downloaded %d / %d so far\n", $so_far, $size); $chunk_size = 0; } } while ($len > 0 && syswrite($fh, $buffer, $len) == $len); print "Done!\n"; close($fh); $data->close;
      while for uploading a file one can use the write method:
      my $file = 'some_file'; my $size = -s $file; my $bsize = 1024; my $report_size = $bsize * 1000; my ($len, $buffer); my $so_far = 0; my $chunk_size = 0; $ftp->binary; my $data = $ftp->stor($file); open(my $fh, '<', $file) or die qq{Cannot open $file: $!}; binmode $fh; while (sysread $fh, $buffer = "", $bsize) { $len = $data->write($buffer, length($buffer)); $so_far += $len; $chunk_size += $len; if ($chunk_size >= $report_size) { printf("Uploaded %d / %d so far\n", $so_far, $size); $chunk_size = 0; } } print "Done!\n"; close($fh); $data->close;