in reply to Net::SSH2 not thread safe?

Try writing your program without Thread::Pool, this works fine here, showing that Net::SSH2 is threadsafe.

I'm running this on perl 5.12.4 on Ubuntu 11.10. Please help me save the little hair

Make sure your Ubuntu version of Perl is compiled to use threads.... I seem to recall Ubuntu may install a non-threaded Perl as default.

#!/usr/bin/perl use warnings; use strict; use threads; my $thr; for(1..10){ $thr = threads->new( \&sub1 )->detach; # Spawn the thread } while(1){ my $thread_count = threads->list(); print "\n\n\n\t\t",'num threads ', $thread_count, "\n\n\n"; print "Hit control c to exit \n\n\n\n"; sleep 1; } exit; sub sub1 { use Net::SSH2; # assuming a user named 'z' for demonstration # connecting to localhost, so you need your sshd running # see maillist archives at # http://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users # for deeper discussions my $self = threads->tid(); my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; #shell use my $chan = $ssh2->channel(); $chan->blocking(0); $chan->shell(); print $chan "ls -la\n"; print "thread $self : $_" while <$chan>; print $chan "who\n"; print "thread $self : $_" while <$chan>; print $chan "date\n"; print "thread $self : $_" while <$chan>; $chan->close; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Net::SSH2 not thread safe?
by Anonymous Monk on Nov 06, 2011 at 12:59 UTC

    Hi there!

    It is compiled with threads, and threads are working when not using Net::SSH2. The mind boggles :-)

    I need some sort of thread control, so Thread::Queue or Thread::Pool is needed. Currently using Thread::Queue with the suggestion to spawn processes in the above, and it is working fine.

    I am going to try this one out as well, slightly modified for thread control - It will certainly be more resourcefriendly if I can get it to work, so thank you very much for your answer.

    -Jesper