Package Bio :: Package MultiProc :: Module Scheduler
[hide private]
[frames] | no frames]

Source Code for Module Bio.MultiProc.Scheduler

 1  """ 
 2  Simple scheduling of threads to be run. 
 3   
 4  Classes: 
 5  Scheduler   Schedules threads to be run. 
 6   
 7  """ 
 8   
 9  import warnings 
10  warnings.warn("Bio.MultiProc is deprecated. If you want to use this code, please let the Biopython developers know by sending an email to biopython-dev@biopython.org to avoid permanent removal of Bio.MultiProc.", 
11                DeprecationWarning) 
12   
13   
14 -class Scheduler:
15 """Schedules threads to be run. No prioritization. Nothing fancy. 16 17 Methods: 18 add Add a thread to be run. 19 num_left Return the number of threads left. 20 num_running Return the number of threads currently running. 21 run Main loop. Returns whether there's still threads left. 22 23 """
24 - def __init__(self, max_threads, start_fn=None, finish_fn=None):
25 """Scheduler(max_threads[, start_fn][, finish_fn]) -> object 26 27 max_threads is the maximum number of threads to run at a time. 28 start_fn and finish_fn are optional callbacks that take a 29 thread as an argument. They are called before and after each 30 thread. 31 32 """ 33 if max_threads <= 0: 34 raise ValueError, "You must specify a positive number of threads!" 35 self._max_threads = max_threads # maximum active threads at a time 36 self._start_fn = start_fn # called before a thread starts 37 self._finish_fn = finish_fn # called when a thread is finished 38 self._waiting = [] # list of threads waiting to run 39 self._in_progress = [] # list of threads running
40
41 - def run(self):
42 """S.run() -> boolean 43 44 Execute the main loop. Return a boolean indicating whether 45 threads are still running. 46 47 """ 48 # See if the running threads are finished. Remove any that 49 # are. 50 i=0 51 while i < len(self._in_progress): 52 if not self._in_progress[i].isAlive(): 53 t = self._in_progress.pop(i) 54 if self._finish_fn: 55 self._finish_fn(t) 56 else: 57 i = i + 1 58 59 # If I have any more slots, start new threads. 60 while self._waiting and len(self._in_progress) < self._max_threads: 61 t = self._waiting.pop(0) 62 if self._start_fn: 63 self._start_fn(t) 64 t.start() 65 self._in_progress.append(t) 66 67 return self._waiting or self._in_progress
68
69 - def add(self, thread):
70 """S.add(thread)""" 71 self._waiting.append(thread)
72
73 - def num_left(self):
74 """S.num_left() -> number of threads left to run""" 75 return len(self._waiting)
76
77 - def num_running(self):
78 """S.num_running() -> number of threads currently running""" 79 return len(self._in_progress)
80