2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Claudio Freire <claudio-daniel.freire@inria.fr>
19 # Alina Quereilhac <alina.quereilhac@inria.fr>
30 class WorkerThread(threading.Thread):
38 task = self.queue.get()
41 self.queue.task_done()
43 elif task is self.QUIT:
45 self.queue.task_done()
47 elif task is self.REASSIGNED:
54 callable, args, kwargs = task
55 rv = callable(*args, **kwargs)
57 if self.rvqueue is not None:
60 self.queue.task_done()
62 traceback.print_exc(file = sys.stderr)
63 self.delayed_exceptions.append(sys.exc_info())
66 while not self.queue.empty() and not self.done:
69 def attach(self, queue, rvqueue, delayed_exceptions):
74 self.rvqueue = rvqueue
75 self.delayed_exceptions = delayed_exceptions
77 oldqueue.put(self.REASSIGNED)
82 self.oldqueue = self.queue
83 self.queue = Queue.Queue()
85 self.delayed_exceptions = []
87 def detach_signal(self):
89 self.oldqueue.put(self.REASSIGNED)
93 self.queue.put(self.QUIT)
96 class ParallelMap(object):
97 def __init__(self, maxthreads = None, maxqueue = None, results = True):
100 # Compute maximum number of threads allowed by the system
101 if maxthreads is None:
104 f = open("/proc/cpuinfo")
106 N_PROCS = sum("processor" in l for l in f)
113 if maxthreads is None:
116 self.queue = Queue.Queue(maxqueue or 0)
118 self.delayed_exceptions = []
121 self.rvqueue = Queue.Queue()
128 for x in xrange(maxthreads):
136 t.attach(self.queue, self.rvqueue, self.delayed_exceptions)
137 self.workers.append(t)
143 for worker in self.workers:
145 for worker in self.workers:
147 for worker in self.workers:
148 worker.detach_signal()
149 for worker in self.workers:
154 def put(self, callable, *args, **kwargs):
155 self.queue.put((callable, args, kwargs))
157 def put_nowait(self, callable, *args, **kwargs):
158 self.queue.put_nowait((callable, args, kwargs))
161 for thread in self.workers:
162 if not thread.isAlive():
166 for thread in self.workers:
167 # That's the sync signal
171 for thread in self.workers:
174 if self.delayed_exceptions:
175 typ,val,loc = self.delayed_exceptions[0]
176 del self.delayed_exceptions[:]
183 if self.delayed_exceptions:
184 typ,val,loc = self.delayed_exceptions[0]
185 del self.delayed_exceptions[:]
189 if self.rvqueue is not None:
192 yield self.rvqueue.get_nowait()
196 yield self.rvqueue.get_nowait()
200 class ParallelRun(ParallelMap):
203 return fn(*args, **kwargs)
205 def __init__(self, maxthreads = None, maxqueue = None):
206 super(ParallelRun, self).__init__(maxthreads, maxqueue, True)
208 def put(self, what, *args, **kwargs):
209 super(ParallelRun, self).put(self.__run, (what, args, kwargs))
211 def put_nowait(self, what, *args, **kwargs):
212 super(ParallelRun, self).put_nowait(self.__filter, (what, args, kwargs))