1
2
3
4
5
6
7 '''
8 Support for asynchronous execution.
9
10 '''
11
12 import os
13 import thread
14
15
17 '''Abstract Asynchronous execution class.
18
19 This is the top abstract class.
20 Concrete classes must implement the _run_program method.
21 '''
22
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 _run_program(self, id, program, parameters, input_files):
63 """Actually run the program, handled by a subclass (PRIVATE).
64
65 This method should be replaced by any derived class to do
66 something useful. It will be called by the run_program method.
67 """
68 raise NotImplementedError("This object should be subclassed")
69
71 ''' Returns the results for a certain Id, the info for that Id is
72 forgotten.
73
74 parameters:
75 id Id of the task.
76
77 returns:
78 (return_code, output_files) return code and file access
79 object.
80
81 The output_files hash key is a relative file name, and the value a
82 output stream.
83 '''
84 self.access_ds.acquire()
85 if id in self.done:
86 returnCode, fileObject = self.done[id]
87 del self.done[id]
88 self.access_ds.release()
89 else:
90 self.access_ds.release()
91 return None
92
93
95 '''An Abstract Support class to retrieve files.
96 '''
97
100
102 '''Returns the list of available files.
103 '''
104 return self.file_list
105
107 raise NotImplementedError('Abstract method')
108
109
111 '''Retrieves a directory content.
112 '''
113
115 FileRetriever.__init__(self)
116 self.directory = directory
117 walk_list = os.walk(directory)
118 for dir, dir_list, file_list in walk_list:
119 for file in file_list:
120 self.file_list.append(file[len(directory)+1:])
121
123 return open(self.directory + os.sep + name)
124