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