From: Bryan B. <br...@bu...> - 2007-07-15 22:29:49
|
> 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 |