vishi has asked for the wisdom of the Perl Monks concerning the following question:
I am using the Expect.pm module to automate some routine tasks across different servers. I am using threads to do this. I have one main thread, called "threadA" (a long running job) and other smaller jobs running on other threads, called threadB and threadC.
Here's my problem: I need to connect to 2 different servers to accomplish one task. Using Expect.pm, I connect to ServerB using threadB and ServerC using threadC. All this is happening in parallel - while threadA is connected to ServerA.
Now, I need to run the stuff in threadC only when the statements in threadB finish. To achieve this, I am using shared variables across the threads using $varName:shared; . This task of "B completes ... then C completes" needs to be done 2 times. I have this in a while loop, checking the counter as well as a flag for the conditions.
My code runs perfectly for the first iteration of the loop. Somehow, on the second iteration, threads are getting spawned correctly, but threadB gets blocked. Because threadB is blocked, threadC waits forever and never finishes. As I am waiting for these threads to complete using a "join" my code simply hangs. How do I resolve this?
Here's my code
#### Main Program ### $count = 0; our $sshFD = Expect->spawn("ssh $User\@$mtHostName"); $sshFD->log_stdout(0); our $sshCI = Expect->spawn("ssh $User\@$dbHost1"); $sshCI->log_stdout(0); while (($deltaFound eq "true") && ($count < 2)) { print "******** LOOP ".$count." *******\n"; our $thrFD = threads->create('findDelta',$sshFD); our $thrCI = threads->create('checkInv',$sshCI); $thrFD->join(); $thrCI1->join(); $sshFD->close(); $sshCI1->close(); } sub checkInv() { our $compInvFlag = ''; our $fndDeltaFlag:shared; my $sshAuth = shift; #### this passes the first time... but just keeps printing dots in the + second iteration ###### while ($fndDeltaFlag ne "true") { sleep(1); print "."; } #### Crunch some numbers here ##### $sshAuth->close(); } sub findDelta() { our $deltaFound; our $fndDeltaFlag = ''; my $counter = 0; my $sshAuth = shift; ##### Do some stuff here ####### $counter = 0; $deltaFound = "false"; while ($counter <2 ) { $deltaFound = "true"; $counter++; } $fndDeltaFlag = "true"; $sshAuth->close(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using Perl Threads with Expect.pm
by bart (Canon) on Feb 17, 2011 at 12:32 UTC | |
by vishi (Beadle) on Feb 17, 2011 at 13:32 UTC | |
by vishi (Beadle) on Feb 17, 2011 at 13:55 UTC | |
by bart (Canon) on Feb 17, 2011 at 19:33 UTC | |
by vishi (Beadle) on Feb 17, 2011 at 20:22 UTC | |
by fidesachates (Monk) on Feb 17, 2011 at 21:15 UTC | |
| |
|
Re: Using Perl Threads with Expect.pm
by olibertu (Initiate) on Feb 29, 2012 at 20:58 UTC |