| OLD | NEW |
| 1 # Copyright (C) 2011 Google Inc. All rights reserved. | 1 # Copyright (C) 2011 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 self._caller.handle(message.name, message.src, *message.args
) | 176 self._caller.handle(message.name, message.src, *message.args
) |
| 177 continue | 177 continue |
| 178 method = getattr(self, '_handle_' + message.name) | 178 method = getattr(self, '_handle_' + message.name) |
| 179 assert method, 'bad message %s' % repr(message) | 179 assert method, 'bad message %s' % repr(message) |
| 180 method(message.src, *message.args) | 180 method(message.src, *message.args) |
| 181 except Queue.Empty: | 181 except Queue.Empty: |
| 182 pass | 182 pass |
| 183 | 183 |
| 184 | 184 |
| 185 class WorkerException(BaseException): | 185 class WorkerException(BaseException): |
| 186 |
| 186 """Raised when we receive an unexpected/unknown exception from a worker.""" | 187 """Raised when we receive an unexpected/unknown exception from a worker.""" |
| 187 pass | 188 pass |
| 188 | 189 |
| 189 | 190 |
| 190 class _Message(object): | 191 class _Message(object): |
| 191 | 192 |
| 192 def __init__(self, src, message_name, message_args, from_user, logs): | 193 def __init__(self, src, message_name, message_args, from_user, logs): |
| 193 self.src = src | 194 self.src = src |
| 194 self.name = message_name | 195 self.name = message_name |
| 195 self.args = message_args | 196 self.args = message_args |
| 196 self.from_user = from_user | 197 self.from_user = from_user |
| 197 self.logs = logs | 198 self.logs = logs |
| 198 | 199 |
| 199 def __repr__(self): | 200 def __repr__(self): |
| 200 return '_Message(src=%s, name=%s, args=%s, from_user=%s, logs=%s)' % (se
lf.src, self.name, self.args, self.from_user, self.logs) | 201 return '_Message(src=%s, name=%s, args=%s, from_user=%s, logs=%s)' % ( |
| 202 self.src, self.name, self.args, self.from_user, self.logs) |
| 201 | 203 |
| 202 | 204 |
| 203 class _Worker(multiprocessing.Process): | 205 class _Worker(multiprocessing.Process): |
| 204 | 206 |
| 205 def __init__(self, host, messages_to_manager, messages_to_worker, worker_fac
tory, worker_number, running_inline, manager, log_level): | 207 def __init__(self, host, messages_to_manager, messages_to_worker, |
| 208 worker_factory, worker_number, running_inline, manager, log_lev
el): |
| 206 super(_Worker, self).__init__() | 209 super(_Worker, self).__init__() |
| 207 self.host = host | 210 self.host = host |
| 208 self.worker_number = worker_number | 211 self.worker_number = worker_number |
| 209 self.name = 'worker/%d' % worker_number | 212 self.name = 'worker/%d' % worker_number |
| 210 self.log_messages = [] | 213 self.log_messages = [] |
| 211 self.log_level = log_level | 214 self.log_level = log_level |
| 212 self._running = False | 215 self._running = False |
| 213 self._running_inline = running_inline | 216 self._running_inline = running_inline |
| 214 self._manager = manager | 217 self._manager = manager |
| 215 | 218 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 if message.from_user: | 259 if message.from_user: |
| 257 worker.handle(message.name, message.src, *message.args) | 260 worker.handle(message.name, message.src, *message.args) |
| 258 self._yield_to_manager() | 261 self._yield_to_manager() |
| 259 else: | 262 else: |
| 260 assert message.name == 'stop', 'bad message %s' % repr(messa
ge) | 263 assert message.name == 'stop', 'bad message %s' % repr(messa
ge) |
| 261 break | 264 break |
| 262 | 265 |
| 263 _log.debug("%s exiting" % self.name) | 266 _log.debug("%s exiting" % self.name) |
| 264 except Queue.Empty: | 267 except Queue.Empty: |
| 265 assert False, '%s: ran out of messages in worker queue.' % self.name | 268 assert False, '%s: ran out of messages in worker queue.' % self.name |
| 266 except KeyboardInterrupt, e: | 269 except KeyboardInterrupt as e: |
| 267 self._raise(sys.exc_info()) | 270 self._raise(sys.exc_info()) |
| 268 except Exception, e: | 271 except Exception as e: |
| 269 self._raise(sys.exc_info()) | 272 self._raise(sys.exc_info()) |
| 270 finally: | 273 finally: |
| 271 try: | 274 try: |
| 272 if hasattr(worker, 'stop'): | 275 if hasattr(worker, 'stop'): |
| 273 worker.stop() | 276 worker.stop() |
| 274 finally: | 277 finally: |
| 275 self._post(name='done', args=(), from_user=False) | 278 self._post(name='done', args=(), from_user=False) |
| 276 self._close() | 279 self._close() |
| 277 | 280 |
| 278 def stop_running(self): | 281 def stop_running(self): |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 324 |
| 322 class _WorkerLogHandler(logging.Handler): | 325 class _WorkerLogHandler(logging.Handler): |
| 323 | 326 |
| 324 def __init__(self, worker): | 327 def __init__(self, worker): |
| 325 logging.Handler.__init__(self) | 328 logging.Handler.__init__(self) |
| 326 self._worker = worker | 329 self._worker = worker |
| 327 self.setLevel(worker.log_level) | 330 self.setLevel(worker.log_level) |
| 328 | 331 |
| 329 def emit(self, record): | 332 def emit(self, record): |
| 330 self._worker.log_messages.append(record) | 333 self._worker.log_messages.append(record) |
| OLD | NEW |