Package Bio :: Package PopGen :: Package Async :: Module Local
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.Async.Local

 1  # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """Asynchronous local execution. 
 7   
 8  Supports multicore architectures. 
 9  """ 
10   
11  from Bio.PopGen.Async import Async 
12   
13  import threading 
14   
15   
16 -class Local(Async):
17 """Execution on Local machine.""" 18
19 - def __init__(self, num_cores=1):
20 """Constructor. 21 22 parameters: 23 24 - num_cores - Number of cores (for multiprocessor machines, 25 multiply accordingly) 26 """ 27 Async.__init__(self) 28 self.num_cores = num_cores 29 self.cores_used = 0
30
31 - def _run_program(self, id, hook, parameters, input_files):
32 """Run program. 33 34 For parameters, please check Async.run_program. 35 36 Either runs a program if a core is available or 37 schedules it. 38 """ 39 self.access_ds.acquire() 40 self.waiting.append((id, hook, parameters, input_files)) 41 if self.cores_used < self.num_cores: 42 self.cores_used += 1 43 threading.Thread(target=self.start_work).run() 44 self.access_ds.release()
45
46 - def start_work(self):
47 """Starts work. 48 49 Thread initial point. 50 While there are tasks to be done, runs them. 51 The thread dies as soon as there is nothing waiting to be 52 executed. 53 """ 54 self.access_ds.acquire() 55 while (len(self.waiting) > 0): 56 id, hook, parameters, input_files = self.waiting[0] 57 del self.waiting[0] 58 self.running[id] = True 59 self.access_ds.release() 60 ret_code, output_files = hook.run_job(parameters, input_files) 61 self.access_ds.acquire() 62 del self.running[id] 63 self.done[id] = ret_code, output_files 64 self.cores_used -= 1 65 self.access_ds.release()
66