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