Re: about SFTP and Thread dependency



Rajnikant wrote:

I'm using perl 5.8.8.
I don't have any clue about whether Net::SFTP is thread safe or not.

But I used Net::SFTP::Foreign instead of Net::SFTP and it started working
:).

Now I'm facing some different problem :(.

If I try calling Net::SFTP::Foreign->ls method in any thread function it
won't work.
Following is the snipet:

use threads;
use Net::SFTP::Foreign;

$Net::SFTP::Foreign::debug = 1;
$remDir = '/var/opt/stcroamcommon/data/ready';

my $thread = threads->new (\&ftpThreadEntry,$host,$user,$password,$cnt);

sub ftpThreadEntry()
{
my $host_l = shift;
my $usr = shift;
my $passwd = shift;
my $id = shift;
my $prot = 1;
my $sftp = undef;

print ("In ftpThreadEntry func thr id $id\n");
my %args = (
"user", $usr,
"password", $passwd,
"more", '-v',
"port", 6789
);
$sftp = Net::SFTP::Foreign->new($host_l,%args);
my $err = $sftp->error(); # I get 'Password not requested as expected:
-1' msg
my $stat = $sftp->status(); # $stat = 'No connection'
if ($sftp)
{
my $files_r=$sftp->ls($remDir); # Here I get nothing in $files_r :(
my $filename;

foreach my $file_r (@$files_r)
{
$filename=$file_r->{filename};
if($filename ne "." && $filename ne "..")
{
$filename=$remoteDir."/".$filename;
print("File name is $filename\n");
if($sftp->get($filename,$filename))
{
print("Returned something \n");
}
else
{
print("=== not returned anything\n");
}
}
}
}
$thread->join();

First of all, please bottom-post replies to threads in this group, so that
extended threads can remain comprehensible. It is something that is said almost
every day here and you really should try to be polite.

Secondly, the only obvious bug I can see is that you have assigned to $remDir
and then used $remoteDir. Bugs like this are inexcusable, as something else that
is said here almost every day is that you /must/

use strict;
and
use warnings;

on every program that you post here. There are no exceptions.

Thirdly, the code you have posted doesn't compile. It is broken because there is
a missing closing brace. I can guess where it belongs, but I really shouldn't
have to.

Finally, you're a C programmer aren't you? Go on, admit it! There is no reason
for all those nested blocks. Look

foreach my $file (@$files) {

my $filename = $file->{filename};
next unless $filename ne '.' and $filename ne '..';
$filename = "$remoteDir/$filename";

print("File name is $filename\n");
if ($sftp->get($filename, $filename)) {
print("Returned something \n");
}
else {
print("=== not returned anything\n");
}
}

Oh, and don't prototype subroutines. It should be

sub ftpThreadEntry {
:
}

HTH,

Rob

.



Relevant Pages

  • Re: Google Groups filtering emails?
    ... >>> This is NOT a bug, ... >>> SHIFT or CTRL, then when you want to drag, the file you drag will ... >>> click on to start the drag will get a selection box, ... >> easiest way to do that is to click on the last first, hold SHIFT ...
    (news.software.readers)
  • Re: Viele Dateien markieren
    ... rechts das Rechteck langsam nach unten scrollen ... ... Bug, ... Der Versuch SHIFT erstes Icon und letztes Icon scheitert jedoch, ... Nach den Dateioperationen ist es mir nicht mehr möglich mit KNode, ...
    (de.comp.os.unix.apps.kde)
  • Re: [PATCH] fix adjtimex() freq return error
    ... Instead of dividing, we shift, then mult, then shift again, ... I don't think adjtimexis a terribly hot path, so this patch simply ... uses div_s64to convert back to the external resolution and removes ... And Martin for the perfect bug report. ...
    (Linux-Kernel)
  • Re: Problem with Straight Arrows!
    ... This bug has already been reported (by the PPT group months ago), ... ...If I hold down SHIFT the arrow grows to a massive size! ... I'll report it. ...
    (microsoft.public.word.drawing.graphics)
  • Re: reading a file
    ... lineX QQQ ... Kim Helliwell ... open IN, shift; ... foreach my $line { ...
    (perl.beginners)