File: Local.py

package info (click to toggle)
python-biopython 1.64%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 44,416 kB
  • ctags: 12,472
  • sloc: python: 153,759; xml: 67,286; ansic: 9,003; sql: 1,488; makefile: 144; sh: 59
file content (66 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

'''
Asynchronous local execution.

Supports multicore architectures.
'''

from Bio.PopGen.Async import Async

import threading


class Local(Async):
    '''Execution on Local machine.
    '''

    def __init__(self, num_cores=1):
        '''Constructor.

           parameters:
           num_cores - Number of cores (for multiprocessor machines,
               multiply accordingly)
        '''
        Async.__init__(self)
        self.num_cores = num_cores
        self.cores_used = 0

    def _run_program(self, id, hook, parameters, input_files):
        '''Run program.

           For parameters, please check Async.run_program.

           Either runs a program if a core is available or
           schedules it.
        '''
        self.access_ds.acquire()
        self.waiting.append((id, hook, parameters, input_files))
        if self.cores_used < self.num_cores:
            self.cores_used += 1
            threading.Thread(target=self.start_work).run()
        self.access_ds.release()

    def start_work(self):
        '''Starts work.

           Thread initial point.
           While there are tasks to be done, runs them.
           The thread dies as soon as there is nothing waiting to be
           executed.
        '''
        self.access_ds.acquire()
        while (len(self.waiting) > 0):
            id, hook, parameters, input_files = self.waiting[0]
            del self.waiting[0]
            self.running[id] = True
            self.access_ds.release()
            ret_code, output_files = hook.run_job(parameters, input_files)
            self.access_ds.acquire()
            del self.running[id]
            self.done[id] = ret_code, output_files
        self.cores_used -= 1
        self.access_ds.release()