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  ''' 
 7  Asynchronous local execution. 
 8   
 9  Supports multicore architectures. 
10  ''' 
11   
12  from Bio.PopGen.Async import Async 
13   
14  import threading 
15   
16  __docformat__ = "restructuredtext en" 
17   
18 -class Local(Async):
19 '''Execution on Local machine. 20 ''' 21
22 - def __init__(self, num_cores=1):
23 '''Constructor. 24 25 parameters: 26 27 - num_cores - Number of cores (for multiprocessor machines, 28 multiply accordingly) 29 ''' 30 Async.__init__(self) 31 self.num_cores = num_cores 32 self.cores_used = 0
33
34 - def _run_program(self, id, hook, parameters, input_files):
35 '''Run program. 36 37 For parameters, please check Async.run_program. 38 39 Either runs a program if a core is available or 40 schedules it. 41 ''' 42 self.access_ds.acquire() 43 self.waiting.append((id, hook, parameters, input_files)) 44 if self.cores_used < self.num_cores: 45 self.cores_used += 1 46 threading.Thread(target=self.start_work).run() 47 self.access_ds.release()
48
49 - def start_work(self):
50 '''Starts work. 51 52 Thread initial point. 53 While there are tasks to be done, runs them. 54 The thread dies as soon as there is nothing waiting to be 55 executed. 56 ''' 57 self.access_ds.acquire() 58 while (len(self.waiting) > 0): 59 id, hook, parameters, input_files = self.waiting[0] 60 del self.waiting[0] 61 self.running[id] = True 62 self.access_ds.release() 63 ret_code, output_files = hook.run_job(parameters, input_files) 64 self.access_ds.acquire() 65 del self.running[id] 66 self.done[id] = ret_code, output_files 67 self.cores_used -= 1 68 self.access_ds.release()
69