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

Source Code for Package Bio.PopGen.Async

  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  """Support for asynchronous execution.""" 
  7   
  8  import os 
  9  import threading 
 10   
 11   
12 -class Async(object):
13 """Abstract Asynchronous execution class. 14 15 This is the top abstract class. 16 Concrete classes must implement the _run_program method. 17 """ 18
19 - def __init__(self):
20 """Async constructor. 21 22 Initializes the queues, among other things. 23 Of notice, is the access_ds lock for controlling exclusive 24 access to this object. 25 """ 26 self.running = {} 27 self.waiting = [] 28 self.done = {} 29 self.id = 0 30 self.hooks = {} 31 self.access_ds = threading.Lock()
32
33 - def run_program(self, program, parameters, input_files):
34 """Runs a program. 35 36 Real _run_program to be implemented by concrete classes. 37 38 parameters: 39 program String identifying program. 40 parameters List of String parameters. 41 input_files Hash of Input file descriptors. 42 43 returns: 44 Task Id. 45 46 The input_files hash key is the path that is passed 47 to the program. It should always be relative. 48 Value is a stream. 49 """ 50 if program in self.hooks: 51 self.access_ds.acquire() 52 self.id += 1 53 id = self.id 54 self.access_ds.release() 55 self._run_program(id, self.hooks[program], parameters, input_files) 56 return id
57
58 - def _run_program(self, id, program, parameters, input_files):
59 """Actually run the program, handled by a subclass (PRIVATE). 60 61 This method should be replaced by any derived class to do 62 something useful. It will be called by the run_program method. 63 """ 64 raise NotImplementedError("This object should be subclassed")
65
66 - def get_result(self, id):
67 """ Returns results for a certain Id, the info for that Id is forgotten. 68 69 parameters: 70 id Id of the task. 71 72 returns: 73 (return_code, output_files) return code and file access object. 74 75 The output_files hash key is a relative file name, and the value a 76 output stream. 77 """ 78 self.access_ds.acquire() 79 if id in self.done: 80 returnCode, fileObject = self.done[id] 81 del self.done[id] 82 self.access_ds.release() 83 else: 84 self.access_ds.release() 85 return None
86 87
88 -class FileRetriever(object):
89 """An Abstract Support class to retrieve files.""" 90
91 - def __init__(self):
92 self.file_list = []
93
94 - def get_File_list(self):
95 """Returns the list of available files.""" 96 return self.file_list
97
98 - def get_file(self, name):
99 raise NotImplementedError('Abstract method')
100 101
102 -class DirectoryRetriever(FileRetriever):
103 """Retrieves a directory content.""" 104
105 - def __init__(self, directory):
106 FileRetriever.__init__(self) 107 self.directory = directory 108 walk_list = os.walk(directory) 109 for dir, dir_list, file_list in walk_list: 110 for file in file_list: 111 self.file_list.append(file[len(directory) + 1:])
112
113 - def get_file(self, name):
114 return open(self.directory + os.sep + name)
115