Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Perl 5.8.8 threads, clean exit

by BrowserUk (Patriarch)
on Feb 27, 2013 at 11:21 UTC ( [id://1020847]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl 5.8.8 threads, clean exit
in thread Perl 5.8.8 threads, clean exit

The best suggestion I can come up with given a) your need to stay with 5.8.8; and b) the still scant information of what the threads are doing; is:

  1. discard all the signaling stuff; it isn't doing you any good anyway.
  2. detach the threads as you create them.
  3. Have your main thread sleep for the timeout period and then just fall off the end of the script.

    When the main thread ends; all the others will end also.

    foreach $lpar ( @lparList ) { threads->create( sub { threads->yield(); manageLPAR ($action,$lpar); } )->detach; } sleep $timeout; ## fall of the end of the script ## the detach threads will end when the main thread does.

    I rarely advocate abandoning threads this way; but since even if the signals 'worked', you would have no possibility for cleanup as you could never know what your code was doing when the signal to exit arrived; it makes no difference in this case.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Perl 5.8.8 threads, clean exit
by T_I (Novice) on Feb 27, 2013 at 12:10 UTC

    This is an option, but then I can better al together abandon threads. The work that's done in this thread has to end before I can execute the fallback option.

    This function askes the virtual system to die, the friendly way, via the management server. When it's not dead in the allotted timeframe, the command has to be killed so I can issue the unfriendly command to turn the system off. (i.e. pull the virtual plug) The system (hatrdware the virtual machine is running on) won't accept a second command for the virtual system when there is still a command being processed.

    I was looking at threads to solve this issue with a neat single script, to replace the bunch of KSH scripts I now have and that are to slow, but as it seems this is not possible. (with this old perl/threads module) To bad.

      I was looking at threads to solve this issue with a neat single script, to replace the bunch of KSH scripts I now have and that are to slow, but as it seems this is not possible. (with this old perl/threads module) To bad.

      Actually, it is probably quite easy to solve using even that ancient perl; given enough information about what you are trying to do.

      For example:

      sub thread { ## spawn a process to run the first command my $pid = open SSH, '|-', 'ssh' or die $!; ## send the command print SSH 'the first command'; ## Sleep for a while sleep $timeout; ## if the process is still running if( kill 0, $pid ) { ## kill it kill 9, $pid; ## wait until it goes away sleep 1 while kill 0, $pid; ## Open a new ssh session $pid = open SSH, '|-', 'ssh' or die $!; } print SSH 'the second command'; close SSH; }

      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Social

        What I want to do is the following (in wierd prototype):

        main: our %ssh (hash of active ssh sessions, Net::OpenSSH) read file with list of vm's to turn off. for each line with 1 or more vm's start thread1 kill_all_thread1 after timeout for each line with 1 or more vm's start thread2 thread1: @vms = split (',',$0) for each vm gather all info needed if (! exists $ssh{vm-manager} { $ssh{vm-manager} = Net::OpenSSH->new(vm-manager) (stdout,stderr)=$ssh{vm-manager}->capture2(request vm to stop) *check errors* (status,stderr)=$ssh{vm-manager}->capture2(request status vm) while (status != dead) { (status,stderr)=$ssh{vm-manager}->capture2(request status + vm) sleep some time } thread2: @vms = split (',',$0) for each vm gather all info needed if (! exists $ssh{vm-manager} { $ssh{vm-manager} = Net::OpenSSH->new(vm-manager) (stdout,stderr)=$ssh{vm-manager}->capture2(pull plug vm)

        The current solution with ksh has several problems:

        • It's all sequential.
          Not a problem with 2-3 vm's, but the total we have is 70+.
        • It initiates a new ssh session per command issued.
          When trying to split the list into seperate commands, the vm-manager gets swamped with ssh sessions. (old hardware, no budget to replace with something fast enough to handle lots of ssh logins)
        • Most vms can be stopped parallel, but some combination of vms may only be stopped in sequence. (application requirements)
        • Sometimes a vm refuses to die within the allotted time.
        • When a vm refuses to die and there are dependent vms, the nice version has to be killed and the rude version has to be started for the same set.
          I.e. in list lpar1,lpar2 when lpar1 refuses to die, it's not allowed to stop lpar2 and then kill lpar1, but it's required to kill lpar1 and then stop/kill lpar2.

        Now I'm typing this, I'm thinking of relocating the timeout inside the while loop of thread1, combine thread 1 and 2 and have the resulting thread handle the stopping of it's list. When a timeout within the thread occurs, kill and be friendly with the next. (or surrender after second timeout and exit with a list of vms that have not yet been disabled.)

        With the scenario above, is it possible to have a second timeout in main and just simply exit when the main timeout is reached?

        $_->join for @jobs;

        This waits for the threads to end, how to escape this line? As far as I can find, that's where I need the signal (and the threads->exit() in the signal handler) for.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1020847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found