143 |
143 |
144 # Create queues |
144 # Create queues |
145 taskQueue = multiprocessing.Queue() |
145 taskQueue = multiprocessing.Queue() |
146 doneQueue = multiprocessing.Queue() |
146 doneQueue = multiprocessing.Queue() |
147 |
147 |
148 # Submit tasks (initially two time number of processes |
148 # Submit tasks (initially two times the number of processes) |
|
149 tasks = len(argumentsList) |
149 initialTasks = 2 * NumberOfProcesses |
150 initialTasks = 2 * NumberOfProcesses |
150 for task in argumentsList[:initialTasks]: |
151 for _ in range(initialTasks): |
151 taskQueue.put(task) |
152 taskQueue.put(argumentsList.pop(0)) |
152 |
153 |
153 # Start worker processes |
154 # Start worker processes |
154 workers = [ |
155 workers = [ |
155 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
156 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
156 for _ in range(NumberOfProcesses) |
157 for _ in range(NumberOfProcesses) |
157 ] |
158 ] |
158 for worker in workers: |
159 for worker in workers: |
159 worker.start() |
160 worker.start() |
160 |
161 |
161 # Get and send results |
162 # Get and send results |
162 endIndex = len(argumentsList) - initialTasks |
163 for _ in range(tasks): |
163 for i in range(len(argumentsList)): |
|
164 resultSent = False |
164 resultSent = False |
165 wasCancelled = False |
165 wasCancelled = False |
166 |
166 |
167 while not resultSent: |
167 while not resultSent: |
168 try: |
168 try: |
178 |
178 |
179 if wasCancelled or cancelled(): |
179 if wasCancelled or cancelled(): |
180 # just exit the loop ignoring the results of queued tasks |
180 # just exit the loop ignoring the results of queued tasks |
181 break |
181 break |
182 |
182 |
183 if i < endIndex: |
183 if argumentsList: |
184 taskQueue.put(argumentsList[i + initialTasks]) |
184 taskQueue.put(argumentsList.pop(0)) |
185 |
185 |
186 # Tell child processes to stop |
186 # Tell child processes to stop |
187 for _ in range(NumberOfProcesses): |
187 for _ in range(NumberOfProcesses): |
188 taskQueue.put("STOP") |
188 taskQueue.put("STOP") |
189 |
189 |