OLD | NEW |
1 # Copyright 2009 Google Inc. All Rights Reserved. | 1 # Copyright 2009 Google Inc. All Rights Reserved. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 """Flush threads that have terminated.""" | 656 """Flush threads that have terminated.""" |
657 running = self.running | 657 running = self.running |
658 self.running = [] | 658 self.running = [] |
659 for t in running: | 659 for t in running: |
660 if t.isAlive(): | 660 if t.isAlive(): |
661 self.running.append(t) | 661 self.running.append(t) |
662 else: | 662 else: |
663 t.join() | 663 t.join() |
664 sys.stdout.full_flush() | 664 sys.stdout.full_flush() |
665 if self.progress: | 665 if self.progress: |
666 self.progress.update(1) | 666 self.progress.update(1, t.item.name) |
667 assert not t.item.name in self.ran | 667 assert not t.item.name in self.ran |
668 if not t.item.name in self.ran: | 668 if not t.item.name in self.ran: |
669 self.ran.append(t.item.name) | 669 self.ran.append(t.item.name) |
670 | 670 |
671 def _run_one_task(self, task_item, args, kwargs): | 671 def _run_one_task(self, task_item, args, kwargs): |
672 if self.jobs > 1: | 672 if self.jobs > 1: |
673 # Start the thread. | 673 # Start the thread. |
674 index = len(self.ran) + len(self.running) + 1 | 674 index = len(self.ran) + len(self.running) + 1 |
675 new_thread = self._Worker(task_item, index, args, kwargs) | 675 new_thread = self._Worker(task_item, index, args, kwargs) |
676 self.running.append(new_thread) | 676 self.running.append(new_thread) |
677 new_thread.start() | 677 new_thread.start() |
678 else: | 678 else: |
679 # Run the 'thread' inside the main thread. Don't try to catch any | 679 # Run the 'thread' inside the main thread. Don't try to catch any |
680 # exception. | 680 # exception. |
681 task_item.run(*args, **kwargs) | 681 task_item.run(*args, **kwargs) |
682 self.ran.append(task_item.name) | 682 self.ran.append(task_item.name) |
683 if self.progress: | 683 if self.progress: |
684 self.progress.update(1) | 684 self.progress.update(1, ', '.join(t.item.name for t in self.running)) |
685 | 685 |
686 class _Worker(threading.Thread): | 686 class _Worker(threading.Thread): |
687 """One thread to execute one WorkItem.""" | 687 """One thread to execute one WorkItem.""" |
688 def __init__(self, item, index, args, kwargs): | 688 def __init__(self, item, index, args, kwargs): |
689 threading.Thread.__init__(self, name=item.name or 'Worker') | 689 threading.Thread.__init__(self, name=item.name or 'Worker') |
690 logging.info(item.name) | 690 logging.info(item.name) |
691 self.item = item | 691 self.item = item |
692 self.index = index | 692 self.index = index |
693 self.args = args | 693 self.args = args |
694 self.kwargs = kwargs | 694 self.kwargs = kwargs |
695 | 695 |
696 def run(self): | 696 def run(self): |
697 """Runs in its own thread.""" | 697 """Runs in its own thread.""" |
698 logging.debug('running(%s)' % self.item.name) | 698 logging.debug('running(%s)' % self.item.name) |
699 work_queue = self.kwargs['work_queue'] | 699 work_queue = self.kwargs['work_queue'] |
700 try: | 700 try: |
701 self.item.run(*self.args, **self.kwargs) | 701 self.item.run(*self.args, **self.kwargs) |
702 except Exception: | 702 except Exception: |
703 # Catch exception location. | 703 # Catch exception location. |
704 logging.info('Caught exception in thread %s' % self.item.name) | 704 logging.info('Caught exception in thread %s' % self.item.name) |
705 logging.info(str(sys.exc_info())) | 705 logging.info(str(sys.exc_info())) |
706 work_queue.exceptions.put(sys.exc_info()) | 706 work_queue.exceptions.put(sys.exc_info()) |
707 logging.info('Task %s done' % self.item.name) | 707 logging.info('Task %s done' % self.item.name) |
708 | 708 |
709 work_queue.ready_cond.acquire() | 709 work_queue.ready_cond.acquire() |
710 try: | 710 try: |
711 work_queue.ready_cond.notifyAll() | 711 work_queue.ready_cond.notifyAll() |
712 finally: | 712 finally: |
713 work_queue.ready_cond.release() | 713 work_queue.ready_cond.release() |
OLD | NEW |