1
2
3
4
5
6 '''
7 Asynchronous local execution.
8
9 Supports multicore architectures.
10 '''
11
12 from Bio.PopGen.Async import Async
13
14 import thread
15
16
18 '''Execution on Local machine.
19 '''
20
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
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 thread.start_new_thread(self.start_work, ())
45 self.access_ds.release()
46
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