From: Roland G. <rgi...@cp...> - 2007-07-16 10:38:28
|
Hmm, could be the wait delay in soft/hard_close which is trying to send signals to the spawned process via "kill". Maybe that also doesn't work in a thread (like fork()). Could you try closing the Expect objects in the main thread? Maybe that gets rid of the delay... Seems a sensible rule to me: "Create and destroy Expect objects only in the main thread." BTW, anybody tried to use Net::SSH2 instead of spawning ssh? Net::SSH2 objects are tied filehandles, so they should be usable via exp_init()... Cheers! Roland On 7/16/07, Bryan Bueter <br...@bu...> wrote: > > Austin/Roland, > > > > Ok, there have been a few threads lately about threading so I thought i > > would re-visit the issue. I wrote a script that seems to work, but I > > would like your opinions on if it really should be working, and/or any > > snafu's i may run into if I continue doing it this way. > > > > Ok, responding to my own thread... I've been playing with this because > even though the login portion is single threaded, it still outperforms > forking and running in parallel. However, I think i ran into one bug that > I am consistently able to re-produce. > > My previous script works fine and is pretty quick to spawn and join the > threads. However, it seems that if I exit and soft_close()/hard_close() > the expect object after the thread is joined, it hangs for a couple of > seconds before joining the first thread. > > Here is the script that causes the join() delay. You can compare this to > my previous script to demonstrate what I'm talking about. > > --- start ---> > #!/usr/bin/perl -w > > use strict; > use threads; > > use Expect; > $Expect::Log_Stdout = 0; > > our $prompt = '[>#\$] $'; > my @hosts = ("mythtv", "valhalla", "eldorado" ); > my %hosts_exp; > > foreach my $host (@hosts) { > print "Getting expect object for $host\n"; > $hosts_exp{$host} = new Expect; > > $hosts_exp{$host}->log_file("$host.log"); > $hosts_exp{$host}->spawn("ssh -x $host"); > $hosts_exp{$host}->expect(5, '-re', $prompt); > } > > my $thread_id = 0; > my @thread_ids = (); > my @thread_hosts; > my @commands = ("uptime", "who", "who am i", "pwd"); > foreach my $host ( keys %hosts_exp ) { > $thread_ids[$thread_id] = threads->new("run_threaded", > $hosts_exp{$host}, $host, @commands); > $thread_hosts[$thread_id] = $host; > > $thread_id++; > } > > for (my $t=0; $t<$thread_id; $t++) { > print "Joining thread $t (". $thread_hosts[$t] .")\n"; > my $result = $thread_ids[$t]->join(); > print "Completed joining thread $t (". $thread_hosts[$t] .")\n"; > > if ($result) { > $hosts_exp{$thread_hosts[$t]}->send("exit;\n exit;\n exit;\n"); > $hosts_exp{$thread_hosts[$t]}->soft_close(); > } else { > print "Thread $t returned bad result\n"; > $hosts_exp{$thread_hosts[$t]}->hard_close(); > } > > } > > sub run_threaded { > my $exp = shift; > my $hostname = shift; > my @commands = @_; > > my $result = 0; > foreach my $command (@commands) { > print "Executing on $hostname: $command\n"; > $exp->send("$command \n"); > $result = $exp->expect(5, '-re', $prompt); > last if ! $result; > } > > return($result); > } > <--- end --- > > Thanks, please let me know what you think. > > > Bryan Bueter > http://sourceforge.net/projects/rover > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |