perl 的thread下载多台FTP服务器文件

perl 的thread下载多台FTP服务器文件

#!/usr/bin/perl
use strict;
use threads;
use threads::shared;
use Net::FTP;
use FileHandle;
use File:ath;
my $date=`date '+%Y%m%d'`;
my $dates=$date-1;
my $dir="/record_date/$dates";

sub ftpdown {
my $name:shared="root";
my $passwd:shared="root";
my $localpath:shared="/file/$dates";
my @HOST=@_;
print "$HOST[0]\t$HOST[1]\n";
our $ftp=Net::FTP->new("$HOST[0]",Port=>21);
our $ftp->login("$name","$passwd";
our $ftp->binary;
our $ftp->cwd("$HOST[1]";
print "login to server is ok!\n";
my @filelist=$ftp->dir();
##
my $haom:shared="192.168";
my $wav:shared=" ->";
foreach (@filelist){
if ($_ =~ "l"{
my $start=index($_,$haom);
lock($start);
my $starts=substr($_,$start);
lock($starts);
my $ends=index($starts,$wav);
lock($ends);
our $localfile=substr($starts,0,$ends);
lock($localfile);
my $downs=rindex($_,$haom);
lock($downs);
our $remotefile=substr($_,$downs);
#lock($remotefile);
}
if (!($_ =~ "xxx"){
my $downs=rindex($_,$haom);
lock($downs);
our $remotefile=substr($_,$downs);
#lock($remotefile);
our $localfile=$remotefile;
lock($localfile);
}
my $remote='"$remotefile"';
lock($remote);
print "$remote\n";
our $local='"$localpath/$localfile"';
print "$local\n";
lock($local);
our $ftp->get("$remote,$local";
print "$_\n",foreach @filelist;
our $ftp->quit;
print "logout to server is OK!\n";
}
}
my $thr1=threads->create(\&ftpdown,"192.168.0.221",$dir);
$thr1->join();
my $thr2=threads->create(\&ftpdown,"192.168.0.224",$dir);
$thr2->join();
my $thr3=threads->create(\&ftpdown,"192.168.0.225",$dir);
$thr3->join();
my $thr4=threads->create(\&ftpdown,"192.168.0.227",$dir);
$thr4->join();
你的thread还是串行执行的……
#!/usr/bin/perl
    #use strict;
    use threads;
    use Net::FTP;
    use FileHandle;
    use File:ath;
my $name="root";
my $passwd="downfile";
my $date=`date '+%Y%m%d'`;
my $dates=$date-1;
my $localpath="/file/$dates";
system("mkdir -p $localpath";
my $remotepath="/record_data/$dates";
my $haom="192.168";
my $wav=" ->";
    my $thr1 = threads->new(\&thrsub, "192.168.0.221",$remotepath);
    my $thr2 = threads->new(\&thrsub, "192.168.0.224",$remotepath);
    sub thrsub {
my @message=@_;
#my $name="root";my $passwd="zinglabs";
print "$message[0]\n";
print "$message[1]\n";
$ftp=Net::FTP->new("$message[0]",Port=>21);
$ftp->login("$name","$passwd";
        $ftp->cwd("$message[1]";
        my @filelist=$ftp->dir();
        lock(@filelist);
        foreach (@filelist){
#print "$_\n";
        if($_ =~ "l"{
        my $start=index($_,$haom);
        lock($start);
        my $starts=substr($_,$start);
        lock($starts);
        my $ends=index($starts,$wav);
        lock($ends);
        my $localfile:shared=substr($starts,0,$ends);
        lock($localfile);
        my $downs=rindex($_,$haom);
        lock($downs);
        my $remotefile:shared=substr($_,$downs);
        lock($remotefile);
my $localfiles="$localpath/$localfile";
lock($localfiles);
$ftp->get($remotefile,$localfiles);
}
if (!($_ =~ "xxx"){
my $downs=rindex($_,$haom);
lock($downs);
my $remotefile=substr($_,$downs);
lock($remotefile);
my $localfile=$remotefile;
lock($localfile);
my $localfiles="$localpath/$localfile";
lock($localfiles);
$ftp->get($remotefile,$localfiles);
}
$ftp->quit();
}
}
$thr1->join;
$thr2->join;
为什么我这个不能下载资料?如果不使用thread的话可以下载到资料。就是时间太长。
请教是不是不能用thread。应该要使用fork();
请教 ,我应该怎么写才能并行运行这个程式?
同意二楼。
建议先粗略了解一下线程编程接口,搞明白 join 是什么意思再说。
个人理解 join是等线程执行完了。回收线程的内存。执行的结果是否成功!
不知道这样的理解对不对。我是一个新手。不太明白,请指教!这段程式为什么不能并行执行!
下次写代码,要CODE一下,
我是新手code是什么意思不明白。能帮我改下不?


[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl

use strict;
use warnings;
use Net::IRC;