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-2014 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  import sys 
 28  import threading 
 29  import traceback 
 30   
 31  # multi module imports. 
 32  from multi.misc import raise_unimplemented 
 33  from multi.result_commands import Result_command, Result_exception 
 34   
 35  # relax module imports (for Python 3 compatibility - the compat module could be bundled with this package if separate). 
 36  from lib.compat import queue 
 37   
 38   
39 -class Exit_queue_result_command(Result_command):
40 - def __init__(self, completed=True):
41 pass
42 43 RESULT_QUEUE_EXIT_COMMAND = Exit_queue_result_command() 44 45 46
47 -class Result_queue(object):
48 - def __init__(self, processor):
49 self.processor = processor
50 51
52 - def put(self, job):
53 if isinstance(job, Result_exception) : 54 self.processor.process_result(job)
55 56
57 - def run_all(self):
59 60 61
62 -class Immediate_result_queue(Result_queue):
63 - def put(self, job):
64 super(Immediate_result_queue, self).put(job) 65 try: 66 self.processor.process_result(job) 67 except: 68 traceback.print_exc(file=sys.stdout) 69 # FIXME: this doesn't work because this isn't the main thread so sys.exit fails... 70 self.processor.abort()
71 72
73 - def run_all(self):
74 pass
75 76 77
78 -class Threaded_result_queue(Result_queue):
79 - def __init__(self, processor):
80 super(Threaded_result_queue, self).__init__(processor) 81 self.queue = queue.Queue() 82 self.sleep_time = 0.05 83 self.processor = processor 84 self.running = 1 85 # FIXME: syntax error here produces exception but no quit 86 self.thread1 = threading.Thread(target=self.workerThread) 87 self.thread1.setDaemon(1) 88 self.thread1.start()
89 90
91 - def put(self, job):
92 super(Threaded_result_queue, self).put(job) 93 self.queue.put_nowait(job)
94 95
96 - def run_all(self):
97 self.queue.put_nowait(RESULT_QUEUE_EXIT_COMMAND) 98 self.thread1.join()
99 100
101 - def workerThread(self):
102 try: 103 while True: 104 job = self.queue.get() 105 if job == RESULT_QUEUE_EXIT_COMMAND: 106 break 107 self.processor.process_result(job) 108 except: 109 traceback.print_exc(file=sys.stdout) 110 # FIXME: this doesn't work because this isn't the main thread so sys.exit fails... 111 self.processor.abort()
112