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