Hi Monks,

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(); }

In reply to Using Perl Threads with Expect.pm by vishi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.