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