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 (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program 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 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module containing command objects sent from the master to the slaves.""" 
 25   
 26  # Python module imports. 
 27  import sys 
 28   
 29  # multi module imports. 
 30  from multi.misc import raise_unimplemented, Result, Result_string 
 31   
 32   
33 -class Slave_command(object):
34 """A command to executed remotely on the slave processor - designed to be subclassed by users. 35 36 The command should be queued with the command queue using the add_to_queue command of the master 37 and B{must} return at least one Result_command even if it is a processor.NULL_RESULT. Results 38 are returned from the Slave_command to the master processor using the return_object method of 39 the processor passed to the command. Any exceptions raised will be caught wrapped and returned 40 to the master processor by the slave processor. 41 42 @note: Good examples of subclassing a slave command include multi.commands.MF_minimise_command 43 and multi.commands.Get_name_command. 44 @see: multi.commands.MF_minimise_command. 45 @see: multi.commands.Get_name_command. 46 """ 47
48 - def __init__(self):
49 self.memo_id = None
50 51
52 - def run(self, processor, completed):
53 """Run the slave command on the slave processor 54 55 This is a base method which must be overridden. 56 57 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. 58 59 60 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 61 @type processor: Processor instance 62 @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. 63 @type completed: bool 64 """ 65 66 # This must be overridden! 67 raise_unimplemented(self.run)
68 69
70 - def set_memo_id(self, memo):
71 """Called by the master processor to remember this Slave_commands memo. 72 73 @param memo: The memo to remember, memos are remembered by getting the memo_id of the 74 memo. 75 """ 76 77 if memo != None: 78 self.memo_id = memo.memo_id() 79 else: 80 self.memo_id = None
81 82 83
84 -class Exit_command(Slave_command):
85 """Special command for terminating slave processors. 86 87 This sets the processor's do_quit flag, terminating the Processor.run() loop for the slaves. 88 """ 89
90 - def run(self, processor, completed):
91 """Set the slave processor's do_quit flag to terminate. 92 93 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 94 @type processor: Processor instance 95 @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. 96 @type completed: bool 97 """ 98 99 # First return no result. 100 processor.return_object(processor.NULL_RESULT) 101 102 # Then set the flag. 103 processor.do_quit = True
104 105 106
107 -class Slave_storage_command(Slave_command):
108 """Special command for sending data for storage on the slaves.""" 109
110 - def __init__(self):
111 """Set up the command.""" 112 113 # Initialise the base class. 114 super(Slave_command, self).__init__() 115 116 # Initialise variables for holding data in transit. 117 self.names = [] 118 self.values = []
119 120
121 - def add(self, name, value):
122 """Pump in data to be held and transfered to the slave by the command. 123 124 @keyword name: The name of the data structure to store. 125 @type name: str 126 @keyword value: The data structure. 127 @type value: anything 128 """ 129 130 # Store the data. 131 self.names.append(name) 132 self.values.append(value)
133 134
135 - def clear(self):
136 """Remove all data from the slave.""" 137 138 # Reinitialise the structures. 139 self.names = [] 140 self.values = []
141 142
143 - def run(self, processor, completed):
144 """Set the slave processor's do_quit flag to terminate. 145 146 @param processor: The slave processor the command is running on. Results from the command are returned via calls to processor.return_object. 147 @type processor: Processor instance 148 @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. 149 @type completed: bool 150 """ 151 152 # First return no result. 153 processor.return_object(processor.NULL_RESULT) 154 155 # Loop over and store the data. 156 for i in range(len(self.names)): 157 setattr(processor.data_store, self.names[i], self.values[i]) 158 159 # Clear the data. 160 self.clear()
161