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