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

Hello

I ran into a strange (to my eyes) behaviour of perl 5.8s threads. Maybe I'm just doing things wrong or have the wrong impression on how things are meant to work.

I'm using 5.8 threads and Net::FTP, to execute one command on a couple FTP Servers at the same time. After connecting to all FTP Servers and storing the connect hash, i create a thread for every FTP to execute a command. It seems to me that when creating the threads to execute the command, the threads are blocking. Eg the commands don't happen in parallel but in serial.

I would be glad if someone could enlighten me if I misunderstood something about the threading implementation of perl, or if i'm doing something seriously wrong about the threads. The whole thing is running on RH7.2 with perl compiled from cpan.

Here some snippets of my code (the whole thing is too big to paste here):

## @ftp is an array holding the name of the respective ftp servers for ($i=0;$i<scalar(@ftp);$i++) { $ftpconnect[$i] = ftp_connect($host[$i],$port[$i],$user[$i],$pass[ +$i],$dir[$i]); } for ($i=0;$i<scalar(@ftp);$i++) { $thread[$i] = threads->create("ftp_exec",$ftpconnect[$i],$command[ +$i]); ## this is where it seems it's blocking? or just taking an awful l +ong time to create each thread. ## although it's necessary that the commands get executed at the e +xact same time everywhere. } ######## maximum time a command takes is around 30 seconds ######## sleep(30); for ($i=0;$i<scalar(@ftp);$i++) { $thread[$i]->join(); } ### subs sub ftp_connect { my ($remoteHost, $remotePort, $remoteUser, $remotePwd, $remote +dir) = @_; my $ftp = Net::FTP->new($remoteHost,Port=>$remotePort,Debug=>1 +); if ($ftp) { $ftp->login($remoteUser,$remotePwd); if (!$ftp->cwd($remotedir)) { $ftp->quit; return ("dir +ectory doesn't exist?"); } $ftp->type('I'); } return ($ftp); } sub ftp_exec { my ($ftp,$cmd) = @_; $ftp->quot("$cmd"); $ftp->quit(); }

thanks in advance
.emanuel

Replies are listed 'Best First'.
Re: 5.8 threads blocking/slow
by djantzen (Priest) on Jan 18, 2003 at 17:23 UTC

    I'm getting this too under 5.8 on MDK 9. It appears that it's not possible to set the local port from which Net::FTP connects, which means that unless there's some smart code in the XS module underneath IO::Socket choosing a local port depending on the threading environment, it's just using the same port. Supposedly you can make that non-blocking by setting the "Timeout" argument when you create your Net::FTP, so try giving that a shot.