113 self.connection.setblocking(False) |
113 self.connection.setblocking(False) |
114 try: |
114 try: |
115 data = self.connection.recv(length, socket.MSG_PEEK) |
115 data = self.connection.recv(length, socket.MSG_PEEK) |
116 except socket.error: |
116 except socket.error: |
117 pass |
117 pass |
118 self.connection.setblocking(True) |
118 finally: |
|
119 self.connection.setblocking(True) |
|
120 |
119 return data |
121 return data |
120 |
122 |
121 def __cancelled(self): |
123 def __cancelled(self): |
122 """ |
124 """ |
123 Private method to check for a job cancellation. |
125 Private method to check for a job cancellation. |
125 @return flag indicating a cancellation (boolean) |
127 @return flag indicating a cancellation (boolean) |
126 """ |
128 """ |
127 msg = self.__peek(struct.calcsize(b'!II') + 6) |
129 msg = self.__peek(struct.calcsize(b'!II') + 6) |
128 if msg[-6:] == b"CANCEL": |
130 if msg[-6:] == b"CANCEL": |
129 # get rid of the message data |
131 # get rid of the message data |
130 self.__peek(struct.calcsize(b'!II') + 6) |
132 self.__receive(struct.calcsize(b'!II') + 6) |
131 return True |
133 return True |
132 else: |
134 else: |
133 return False |
135 return False |
134 |
136 |
135 def run(self): |
137 def run(self): |
176 ret = callback(fn, *data) |
178 ret = callback(fn, *data) |
177 else: |
179 else: |
178 ret = 'Unknown service.' |
180 ret = 'Unknown service.' |
179 |
181 |
180 self.__send(fx, fn, ret) |
182 self.__send(fx, fn, ret) |
|
183 except socket.error: |
|
184 pass |
181 except Exception: |
185 except Exception: |
182 exctype, excval, exctb = sys.exc_info() |
186 exctype, excval, exctb = sys.exc_info() |
183 tbinfofile = io.StringIO() |
187 tbinfofile = io.StringIO() |
184 traceback.print_tb(exctb, None, tbinfofile) |
188 traceback.print_tb(exctb, None, tbinfofile) |
185 tbinfofile.seek(0) |
189 tbinfofile.seek(0) |
186 tbinfo = tbinfofile.read() |
190 tbinfo = tbinfofile.read() |
187 del exctb |
191 del exctb |
188 self.__send( |
192 self.__send( |
189 'EXCEPTION', '?', [str(exctype), str(excval), tbinfo]) |
193 'EXCEPTION', '?', [str(exctype), str(excval), tbinfo]) |
190 |
194 |
191 # Give time to process latest response on server side |
195 finally: |
192 time.sleep(0.5) |
196 # Give time to process latest response on server side |
193 self.connection.shutdown(socket.SHUT_RDWR) |
197 time.sleep(0.5) |
194 self.connection.close() |
198 self.connection.shutdown(socket.SHUT_RDWR) |
|
199 self.connection.close() |
195 |
200 |
196 if __name__ == '__main__': |
201 if __name__ == '__main__': |
197 if len(sys.argv) != 4: |
202 if len(sys.argv) != 4: |
198 print('Host, port and max. processes parameters are missing. Abort.') |
203 print('Host, port and max. processes parameters are missing. Abort.') |
199 sys.exit(1) |
204 sys.exit(1) |