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 self.hooks.has_key(program):
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
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 self.done.has_key(id):
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
86 '''A Abstract Support class to retrieve files.
87 '''
88
91
93 '''Returns the list of available files.
94 '''
95 return self.file_list
96
98 raise NotImplementedError, 'Abstract method'
99
101 '''Retrieves a directory content.
102 '''
103
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
113 return open(self.directory + os.sep + name)
114