in reply to Re^2: Queuing in multithread context
in thread Queuing in multithread context

Okay. Now you've shown us the sample config file, it shows that you are running each of multiple DEPLOY sections on each of the servers, each in a separate thread; hence your conflicts.

The simple solution to your problem is to only start one thread for each server; and call the deployRoutine() multiple times within that thread; serially.

Ie. Something like this:

foreach my $targetServer (@targets) { # Ping host to check its available my $p = Net::Ping->new(); if ($p->ping($targetServer)) { my @subs; ### Accumulate the deployRoutines here foreach my $deploySection (@deployments) { if ($flagDeploy) { my $deploySource = "$config{$deploySection}{'Source'}" ; my $deployDest = "$config{$deploySection}{'Destination'}" +; if ($flagNoMove) { ## Add this sub to the array to be executed for this s +erver push @subs, sub { deployRoutine($targetServer, $deploy +Source, $deployDest, "true") }; $threadDetails{$thread}{"Section"} = "$deploySection"; $threadDetails{$thread}{"Target"} = "$targetServer"; } else { ## Add this sub to the array to be executed for this s +erver push @subs, sub { deployRoutine( $targetServer, $deplo +ySource, $deployDest ) }; $threadDetails{$thread}{"Section"} = "$deploySection"; $threadDetails{$thread}{"Target"} = "$targetServer"; } } } ## Now start one thread to execute them all; serially push @threads, threads->new( sub { $_->() for @subs; } ); $p->close(); }

Note: That is obviously untested code in the context of your application, but in isolation, the principle works:

{ use threads; my @subs = map{ eval "sub{ print $_; }" } 1 .. 10; threads->new( sub{ $_->() for @subs; } ); sleep 10; };; 1 2 3 4 5 6 7 8 9 10

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^4: Queuing in multithread context
by Hardin (Novice) on Jan 20, 2015 at 16:55 UTC

    That's it! Definitely what I want to achieve, thank you

    For now I'm unable to achieve a working implementation of that logic, if I proceed so thread aren't joined and the script exit while all haven't been joined and I need to retrieve their status anyway.

    I added a join for @threads but now I'm stuck on the first thread execution not sure why it's not going further...

    Still working on it and I appreciate your help!

      For now I'm unable to achieve a working implementation of that logic,

      Post what you've got and I'll try and help.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked