Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm trying to split a large file into smaller chunks, and then use threads to upload the chunks to an FTP server. This is to speed up the transfer with multiple connections uploading a piece of the file. Here's what I've tried so far but its not working when using threads, the program seems to hang or block, and the file on the FTP server is 0 size. Any help would be appreciated.
#!/usr/bin/perl -w use strict; use warnings; use Net::FTP; use Config; $Config{useithreads} or die('Recompile Perl with threads to run this p +rogram.'); use threads; my $file = 'c:\sdwork\dla495.txt'; my $ftp = Net::FTP->new("ftpserver") or die "can't get ftp object\n"; $ftp->login("fakeusr", "secret") or die "can't login to ftp server\n"; $ftp->binary(); $ftp->cwd('/home/fakeusr/tmp'); open (FH, "<$file") or die "Could not open source file. $!"; binmode(FH); my $i = 0; my $start_byte = 0; my @THRD_LIST; my $thrd; while (1) { my $chunk; print "process part $i\n"; $i ++; if (!eof(FH)) { my $bytes_read = read(FH, $chunk, 10000); #bytes print "bytes read: $bytes_read\n"; #test with threads $thrd = threads->create (\&transfer_chunks, $start_byte, $chunk) + or die "Failed to start the thread: $@\n"; push (@THRD_LIST, $thrd); #test without threads #transfer_chunks($start_byte, $chunk); $start_byte += $bytes_read + 1; print "start byte: $start_byte\n"; } last if eof(FH); } # start the threads for $thrd (@THRD_LIST) { $thrd->join(); } $ftp->quit; sub transfer_chunks { my ($start_byte, $chunk) = @_; # convert perl string into a filehandle open(TEST, '<', \$chunk); $ftp->restart($start_byte); $ftp->put(*TEST, 'testfile.txt'); ##$ftp->append(*TEST, 'testfile.txt'); close(TEST); }

In reply to Split large file and upload to ftp server with multiple threads by mdc76

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found