Package multi :: Module slave_commands
[hide private]
[frames] | no frames]

Source Code for Module multi.slave_commands

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007 Gary S Thompson (https://gna.org/users/varioustoxins)    # 
  4  # Copyright (C) 2008-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module containing command objects sent from the master to the slaves.""" 
 26   
 27  # Python module imports. 
 28  import sys 
 29   
 30  # multi module imports. 
 31  from multi.misc import raise_unimplemented, Result, Result_string 
 32   
 33   
34 -class Slave_command(object):
35 """A command to executed remotely on the slave processor - designed to be subclassed by users. 36 37 The command should be queued with the command queue using the add_to_queue command of the master 38 and B{must} return at least one Result_command even if it is a processor.NULL_RESULT. Results 39 are returned from the Slave_command to the master processor using the return_object method of 40 the processor passed to the command. Any exceptions raised will be caught wrapped and returned 41 to the master processor by the slave processor. 42 43 @note: Good examples of subclassing a slave command include multi.commands.MF_minimise_command 44 and multi.commands.Get_name_command. 45 @see: multi.commands.MF_minimise_command. 46 @see: multi.commands.Get_name_command. 47 """ 48
49 - def __init__(self):
50 self.memo_id = None
51 52
53 - def run(self, processor, completed):
54 """Run the slave command on the slave processor 55 56 This is a base method which must be overridden. 57 58 The run command B{must} return at least one Result_command even if it is a processor.NULL_RESULT. Results are returned from the Slave_command to the master processor using the return_object method of the processor passed to the command. Any exceptions raised will be caught wrapped and returned to the master processor by the slave processor. 59 60 61 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 62 @type processor: Processor instance 63 @param completed: The flag used in batching result returns to indicate that the sequence of batched result commands has completed. This value should be returned via the last result object retuned by this method or methods it calls. All other Result_commands should be initialised with completed=False. This is an optimisation to prevent the sending an extra batched result queue completion result command being sent, it may be an over early optimisation. 64 @type completed: bool 65 """ 66 67 # This must be overridden! 68 raise_unimplemented(self.run)
69 70
71 - def set_memo_id(self, memo):
72 """Called by the master processor to remember this Slave_commands memo. 73 74 @param memo: The memo to remember, memos are remembered by getting the memo_id of the 75 memo. 76 """ 77 78 if memo != None: 79 self.memo_id = memo.memo_id() 80 else: 81 self.memo_id = None
82 83 84
85 -class Exit_command(Slave_command):
86 """Special command for terminating slave processors. 87 88 This sets the processor's do_quit flag, terminating the Processor.run() loop for the slaves. 89 """ 90
91 - def run(self, processor, completed):
92 """Set the slave processor's do_quit flag to terminate. 93 94 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 95 @type processor: Processor instance 96 @param completed: The flag used in batching result returns to indicate that the sequence of batched result commands has completed. This value should be returned via the last result object retuned by this method or methods it calls. All other Result_commands should be initialised with completed=False. This is an optimisation to prevent the sending an extra batched result queue completion result command being sent, it may be an over early optimisation. 97 @type completed: bool 98 """ 99 100 # First return no result. 101 processor.return_object(processor.NULL_RESULT) 102 103 # Then set the flag. 104 processor.do_quit = True
105 106 107
108 -class Slave_storage_command(Slave_command):
109 """Special command for sending data for storage on the slaves.""" 110
111 - def __init__(self):
112 """Set up the command.""" 113 114 # Initialise the base class. 115 super(Slave_command, self).__init__() 116 117 # Initialise variables for holding data in transit. 118 self.names = [] 119 self.values = []
120 121
122 - def add(self, name, value):
123 """Pump in data to be held and transfered to the slave by the command. 124 125 @keyword name: The name of the data structure to store. 126 @type name: str 127 @keyword value: The data structure. 128 @type value: anything 129 """ 130 131 # Store the data. 132 self.names.append(name) 133 self.values.append(value)
134 135
136 - def clear(self):
137 """Remove all data from the slave.""" 138 139 # Reinitialise the structures. 140 self.names = [] 141 self.values = []
142 143
144 - def run(self, processor, completed):
145 """Set the slave processor's do_quit flag to terminate. 146 147 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 148 @type processor: Processor instance 149 @param completed: The flag used in batching result returns to indicate that the sequence of batched result commands has completed. This value should be returned via the last result object retuned by this method or methods it calls. All other Result_commands should be initialised with completed=False. This is an optimisation to prevent the sending an extra batched result queue completion result command being sent, it may be an over early optimisation. 150 @type completed: bool 151 """ 152 153 # First return no result. 154 processor.return_object(processor.NULL_RESULT) 155 156 # Loop over and store the data. 157 for i in range(len(self.names)): 158 setattr(processor.data_store, self.names[i], self.values[i]) 159 160 # Clear the data. 161 self.clear()
162