Package multi :: Module result_queue
[hide private]
[frames] | no frames]

Source Code for Module multi.result_queue

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007 Gary S Thompson (https://gna.org/users/varioustoxins)    # 
  4  # Copyright (C) 2011-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax 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 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax 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 relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module containing the results queue objects.""" 
 26   
 27  # Python module imports. 
 28  import Queue 
 29  import sys 
 30  import threading 
 31  import traceback 
 32   
 33  # multi module imports. 
 34  from multi.misc import raise_unimplemented 
 35  from multi.result_commands import Result_command, Result_exception 
 36   
 37   
38 -class Exit_queue_result_command(Result_command):
39 - def __init__(self, completed=True):
40 pass
41 42 RESULT_QUEUE_EXIT_COMMAND = Exit_queue_result_command() 43 44 45
46 -class Result_queue(object):
47 - def __init__(self, processor):
48 self.processor = processor
49 50
51 - def put(self, job):
52 if isinstance(job, Result_exception) : 53 self.processor.process_result(job)
54 55
56 - def run_all(self):
58 59 60
61 -class Immediate_result_queue(Result_queue):
62 - def put(self, job):
63 super(Immediate_result_queue, self).put(job) 64 try: 65 self.processor.process_result(job) 66 except: 67 traceback.print_exc(file=sys.stdout) 68 # FIXME: this doesn't work because this isn't the main thread so sys.exit fails... 69 self.processor.abort()
70 71
72 - def run_all(self):
73 pass
74 75 76
77 -class Threaded_result_queue(Result_queue):
78 - def __init__(self, processor):
79 super(Threaded_result_queue, self).__init__(processor) 80 self.queue = Queue.Queue() 81 self.sleep_time = 0.05 82 self.processor = processor 83 self.running = 1 84 # FIXME: syntax error here produces exception but no quit 85 self.thread1 = threading.Thread(target=self.workerThread) 86 self.thread1.setDaemon(1) 87 self.thread1.start()
88 89
90 - def put(self, job):
91 super(Threaded_result_queue, self).put(job) 92 self.queue.put_nowait(job)
93 94
95 - def run_all(self):
96 self.queue.put_nowait(RESULT_QUEUE_EXIT_COMMAND) 97 self.thread1.join()
98 99
100 - def workerThread(self):
101 try: 102 while True: 103 job = self.queue.get() 104 if job == RESULT_QUEUE_EXIT_COMMAND: 105 break 106 self.processor.process_result(job) 107 except: 108 traceback.print_exc(file=sys.stdout) 109 # FIXME: this doesn't work because this isn't the main thread so sys.exit fails... 110 self.processor.abort()
111