93 |
93 |
94 # Create queues |
94 # Create queues |
95 taskQueue = multiprocessing.Queue() |
95 taskQueue = multiprocessing.Queue() |
96 doneQueue = multiprocessing.Queue() |
96 doneQueue = multiprocessing.Queue() |
97 |
97 |
98 # Submit tasks (initially two time number of processes |
98 # Submit tasks (initially two times the number of processes) |
|
99 tasks = len(argumentsList) |
99 initialTasks = 2 * NumberOfProcesses |
100 initialTasks = 2 * NumberOfProcesses |
100 for task in argumentsList[:initialTasks]: |
101 for _ in range(initialTasks): |
101 taskQueue.put(task) |
102 taskQueue.put(argumentsList.pop(0)) |
102 |
103 |
103 # Start worker processes |
104 # Start worker processes |
104 workers = [ |
105 workers = [ |
105 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
106 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
106 for _ in range(NumberOfProcesses) |
107 for _ in range(NumberOfProcesses) |
107 ] |
108 ] |
108 for worker in workers: |
109 for worker in workers: |
109 worker.start() |
110 worker.start() |
110 |
111 |
111 # Get and send results |
112 # Get and send results |
112 endIndex = len(argumentsList) - initialTasks |
113 for _ in range(tasks): |
113 for i in range(len(argumentsList)): |
|
114 resultSent = False |
114 resultSent = False |
115 wasCancelled = False |
115 wasCancelled = False |
116 |
116 |
117 while not resultSent: |
117 while not resultSent: |
118 try: |
118 try: |
128 |
128 |
129 if wasCancelled or cancelled(): |
129 if wasCancelled or cancelled(): |
130 # just exit the loop ignoring the results of queued tasks |
130 # just exit the loop ignoring the results of queued tasks |
131 break |
131 break |
132 |
132 |
133 if i < endIndex: |
133 if argumentsList: |
134 taskQueue.put(argumentsList[i + initialTasks]) |
134 taskQueue.put(argumentsList.pop(0)) |
135 |
135 |
136 # Tell child processes to stop |
136 # Tell child processes to stop |
137 for _ in range(NumberOfProcesses): |
137 for _ in range(NumberOfProcesses): |
138 taskQueue.put("STOP") |
138 taskQueue.put("STOP") |
139 |
139 |