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>
32 class WorkerThread(threading.Thread):
40 task = self.queue.get()
43 self.queue.task_done()
45 elif task is self.QUIT:
47 self.queue.task_done()
49 elif task is self.REASSIGNED:
56 callable, args, kwargs = task
57 rv = callable(*args, **kwargs)
59 if self.rvqueue is not None:
62 self.queue.task_done()
64 traceback.print_exc(file = sys.stderr)
65 self.delayed_exceptions.append(sys.exc_info())
68 while not self.queue.empty() and not self.done:
71 def attach(self, queue, rvqueue, delayed_exceptions):
76 self.rvqueue = rvqueue
77 self.delayed_exceptions = delayed_exceptions
79 oldqueue.put(self.REASSIGNED)
84 self.oldqueue = self.queue
85 self.queue = Queue.Queue()
87 self.delayed_exceptions = []
89 def detach_signal(self):
91 self.oldqueue.put(self.REASSIGNED)
95 self.queue.put(self.QUIT)
98 class ParallelMap(object):
99 def __init__(self, maxthreads = None, maxqueue = None, results = True):
102 global THREADCACHEPID
104 if maxthreads is None:
107 f = open("/proc/cpuinfo")
109 N_PROCS = sum("processor" in l for l in f)
116 if maxthreads is None:
119 self.queue = Queue.Queue(maxqueue or 0)
121 self.delayed_exceptions = []
124 self.rvqueue = Queue.Queue()
129 if THREADCACHEPID is None or THREADCACHEPID != os.getpid():
131 THREADCACHEPID = os.getpid()
134 for x in xrange(maxthreads):
138 t = THREADCACHE.pop()
146 t.attach(self.queue, self.rvqueue, self.delayed_exceptions)
147 self.workers.append(t)
155 global THREADCACHEPID
156 if THREADCACHEPID is None or THREADCACHEPID != os.getpid():
158 THREADCACHEPID = os.getpid()
160 for worker in self.workers:
162 for worker in self.workers:
164 for worker in self.workers:
165 worker.detach_signal()
166 THREADCACHE.extend(self.workers)
169 def put(self, callable, *args, **kwargs):
170 self.queue.put((callable, args, kwargs))
172 def put_nowait(self, callable, *args, **kwargs):
173 self.queue.put_nowait((callable, args, kwargs))
176 for thread in self.workers:
177 if not thread.isAlive():
181 for thread in self.workers:
182 # That's the sync signal
186 for thread in self.workers:
189 if self.delayed_exceptions:
190 typ,val,loc = self.delayed_exceptions[0]
191 del self.delayed_exceptions[:]
198 if self.delayed_exceptions:
199 typ,val,loc = self.delayed_exceptions[0]
200 del self.delayed_exceptions[:]
204 if self.rvqueue is not None:
207 yield self.rvqueue.get_nowait()
211 yield self.rvqueue.get_nowait()
216 class ParallelFilter(ParallelMap):
220 def __filter(self, x):
221 if self.filter_condition(x):
224 return self._FILTERED
226 def __init__(self, filter_condition, maxthreads = None, maxqueue = None):
227 super(ParallelFilter, self).__init__(maxthreads, maxqueue, True)
228 self.filter_condition = filter_condition
231 super(ParallelFilter, self).put(self.__filter, what)
233 def put_nowait(self, what):
234 super(ParallelFilter, self).put_nowait(self.__filter, what)
237 for rv in super(ParallelFilter, self).__iter__():
238 if rv is not self._FILTERED:
241 class ParallelRun(ParallelMap):
244 return fn(*args, **kwargs)
246 def __init__(self, maxthreads = None, maxqueue = None):
247 super(ParallelRun, self).__init__(maxthreads, maxqueue, True)
249 def put(self, what, *args, **kwargs):
250 super(ParallelRun, self).put(self.__run, (what, args, kwargs))
252 def put_nowait(self, what, *args, **kwargs):
253 super(ParallelRun, self).put_nowait(self.__filter, (what, args, kwargs))
256 def pmap(mapping, iterable, maxthreads = None, maxqueue = None):
257 mapper = ParallelMap(
258 maxthreads = maxthreads,
262 for elem in iterable:
268 def pfilter(condition, iterable, maxthreads = None, maxqueue = None):
269 filtrer = ParallelFilter(
271 maxthreads = maxthreads,
274 for elem in iterable: