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