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>
21 # A.Q. TODO: BUG FIX THREADCACHE. Not needed!! remove it completely!
31 class WorkerThread(threading.Thread):
39 task = self.queue.get()
42 self.queue.task_done()
44 elif task is self.QUIT:
46 self.queue.task_done()
48 elif task is self.REASSIGNED:
55 callable, args, kwargs = task
56 rv = callable(*args, **kwargs)
58 if self.rvqueue is not None:
61 self.queue.task_done()
63 traceback.print_exc(file = sys.stderr)
64 self.delayed_exceptions.append(sys.exc_info())
67 while not self.queue.empty() and not self.done:
70 def attach(self, queue, rvqueue, delayed_exceptions):
75 self.rvqueue = rvqueue
76 self.delayed_exceptions = delayed_exceptions
78 oldqueue.put(self.REASSIGNED)
83 self.oldqueue = self.queue
84 self.queue = Queue.Queue()
86 self.delayed_exceptions = []
88 def detach_signal(self):
90 self.oldqueue.put(self.REASSIGNED)
94 self.queue.put(self.QUIT)
97 class ParallelMap(object):
98 def __init__(self, maxthreads = None, maxqueue = None, results = True):
101 # Compute maximum number of threads allowed by the system
102 if maxthreads is None:
105 f = open("/proc/cpuinfo")
107 N_PROCS = sum("processor" in l for l in f)
114 if maxthreads is None:
117 self.queue = Queue.Queue(maxqueue or 0)
119 self.delayed_exceptions = []
122 self.rvqueue = Queue.Queue()
129 for x in xrange(maxthreads):
137 t.attach(self.queue, self.rvqueue, self.delayed_exceptions)
138 self.workers.append(t)
144 for worker in self.workers:
146 for worker in self.workers:
148 for worker in self.workers:
149 worker.detach_signal()
150 for worker in self.workers:
155 def put(self, callable, *args, **kwargs):
156 self.queue.put((callable, args, kwargs))
158 def put_nowait(self, callable, *args, **kwargs):
159 self.queue.put_nowait((callable, args, kwargs))
162 for thread in self.workers:
163 if not thread.isAlive():
167 for thread in self.workers:
168 # That's the sync signal
172 for thread in self.workers:
175 if self.delayed_exceptions:
176 typ,val,loc = self.delayed_exceptions[0]
177 del self.delayed_exceptions[:]
184 if self.delayed_exceptions:
185 typ,val,loc = self.delayed_exceptions[0]
186 del self.delayed_exceptions[:]
190 if self.rvqueue is not None:
193 yield self.rvqueue.get_nowait()
197 yield self.rvqueue.get_nowait()
201 class ParallelRun(ParallelMap):
204 return fn(*args, **kwargs)
206 def __init__(self, maxthreads = None, maxqueue = None):
207 super(ParallelRun, self).__init__(maxthreads, maxqueue, True)
209 def put(self, what, *args, **kwargs):
210 super(ParallelRun, self).put(self.__run, (what, args, kwargs))
212 def put_nowait(self, what, *args, **kwargs):
213 super(ParallelRun, self).put_nowait(self.__filter, (what, args, kwargs))