Utilities/BackgroundClient.py

changeset 5762
76ef5f340007
parent 5624
cdd346d8858b
child 6048
82ad8ec9548c
equal deleted inserted replaced
5761:f9e1e4e50b33 5762:76ef5f340007
27 27
28 class BackgroundClient(object): 28 class BackgroundClient(object):
29 """ 29 """
30 Class implementing the main part of the background client. 30 Class implementing the main part of the background client.
31 """ 31 """
32 def __init__(self, host, port): 32 def __init__(self, host, port, maxProcs):
33 """ 33 """
34 Constructor of the BackgroundClient class. 34 Constructor of the BackgroundClient class.
35 35
36 @param host ip address the background service is listening 36 @param host ip address the background service is listening
37 @type str
37 @param port port of the background service 38 @param port port of the background service
39 @type int
40 @param maxProcs maximum number of CPUs (processes) to use
41 (0 = determined automatically)
42 @type int
38 """ 43 """
39 self.services = {} 44 self.services = {}
40 self.batchServices = {} 45 self.batchServices = {}
41 46
42 self.connection = socket.create_connection((host, port)) 47 self.connection = socket.create_connection((host, port))
43 ver = b'Python2' if sys.version_info[0] == 2 else b'Python3' 48 ver = b'Python2' if sys.version_info[0] == 2 else b'Python3'
44 self.connection.sendall(ver) 49 self.connection.sendall(ver)
50 self.__maxProcs = maxProcs
45 51
46 def __initClientService(self, fn, path, module): 52 def __initClientService(self, fn, path, module):
47 """ 53 """
48 Private method to import the given module and register it as service. 54 Private method to import the given module and register it as service.
49 55
153 if fx == 'INIT': 159 if fx == 'INIT':
154 ret = self.__initClientService(fn, *data) 160 ret = self.__initClientService(fn, *data)
155 elif fx.startswith("batch_"): 161 elif fx.startswith("batch_"):
156 callback = self.batchServices.get(fx) 162 callback = self.batchServices.get(fx)
157 if callback: 163 if callback:
158 callback(data, self.__send, fx, self.__cancelled) 164 try:
165 callback(data, self.__send, fx, self.__cancelled,
166 maxProcesses=self.__maxProcs)
167 except TypeError:
168 # for backward compatibility
169 callback(data, self.__send, fx, self.__cancelled)
159 ret = "__DONE__" 170 ret = "__DONE__"
160 else: 171 else:
161 ret = 'Unknown batch service.' 172 ret = 'Unknown batch service.'
162 else: 173 else:
163 callback = self.services.get(fx) 174 callback = self.services.get(fx)
181 time.sleep(0.5) 192 time.sleep(0.5)
182 self.connection.shutdown(socket.SHUT_RDWR) 193 self.connection.shutdown(socket.SHUT_RDWR)
183 self.connection.close() 194 self.connection.close()
184 195
185 if __name__ == '__main__': 196 if __name__ == '__main__':
186 if len(sys.argv) != 3: 197 if len(sys.argv) != 4:
187 print('Host and port parameters are missing. Abort.') 198 print('Host, port and max. processes parameters are missing. Abort.')
188 sys.exit(1) 199 sys.exit(1)
189 200
190 host, port = sys.argv[1:] 201 host, port, maxProcs = sys.argv[1:]
191 backgroundClient = BackgroundClient(host, int(port)) 202 backgroundClient = BackgroundClient(host, int(port), int(maxProcs))
192 # Start the main loop 203 # Start the main loop
193 backgroundClient.run() 204 backgroundClient.run()
194 205
195 # 206 #
196 # eflag: noqa = M801 207 # eflag: noqa = M801

eric ide

mercurial