<?xml version="1.0" encoding="windows-1252"?>
<node id="1009669" title="Re^2: Threaded or Forked FTP Backup with chdir" created="2012-12-19 21:20:41" updated="2012-12-19 21:20:41">
<type id="11">
note</type>
<author id="1009651">
Mimir</author>
<data>
<field name="doctext">
&lt;ol&gt;
&lt;li&gt;chdir isn't unique to each thread. If you chdir in one thread they all change.&lt;/li&gt;
&lt;li&gt;changing to fork would allow chdir per fork because the memory is copied when forked&lt;/li&gt;
&lt;li&gt;This is an excellent point. You can't pass the full path to put but, you can pass a file handle.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here is the updated code:&lt;/p&gt;
&lt;code&gt;
sub worker {
   my $tid = threads-&gt;tid;
   my( $Qwork, $Qresults ) = @_;

   my $ftp = Net::FTP-&gt;new($gFtpServer, Debug =&gt; 0);
   $ftp-&gt;login($gFtpUser, $gFtpPass) or print "unable to loging to $gFtpServer: $ftp-&gt;message\n";

   while(my $work = $Qwork-&gt;dequeue) {
      my ($result, $localFile);
      my (@workItem);

      @workItem = split (",", $work);

      chomp($workItem[0]);
      chomp($workItem[1]);
      chomp($workItem[2]);
      print "ftp dir: $workItem[0]\n";
      print "local dir: $workItem[1]\n";
      print "file: $workItem[2]\n";
      $localFile = sprintf("%s%s",$workItem[1],$workItem[2]);

      open (my $fh, "&lt;", $localFile) or print "unable to open file";
      $ftp-&gt;cwd($workItem[0]) or print "unable to FTP change dir $workItem[0]: $ftp-&gt;message\n";
      $ftp-&gt;put($fh, $workItem[2]) or print "unable to FTP $workItem[2]: $ftp-&gt;message\n";
      ## Process $work to produce $result ##
      $result = "$tid : result for workitem ";
      $Qresults-&gt;enqueue( $result );
   }
   $Qresults-&gt;enqueue( undef ); ## Signal this thread is finished
   $ftp-&gt;quit;
}
&lt;/code&gt;
</field>
<field name="root_node">
1009657</field>
<field name="parent_node">
1009663</field>
</data>
</node>
