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