Construct a bunch of `n` threads running the same function `f`. If `wait_before_exit` is True, the threads won't terminate until do_finish() is called.
(self, namespace, f, args, n, wait_before_exit=False)
| 2185 | A bunch of threads. |
| 2186 | """ |
| 2187 | def __init__(self, namespace, f, args, n, wait_before_exit=False): |
| 2188 | """ |
| 2189 | Construct a bunch of `n` threads running the same function `f`. |
| 2190 | If `wait_before_exit` is True, the threads won't terminate until |
| 2191 | do_finish() is called. |
| 2192 | """ |
| 2193 | self.f = f |
| 2194 | self.args = args |
| 2195 | self.n = n |
| 2196 | self.started = namespace.DummyList() |
| 2197 | self.finished = namespace.DummyList() |
| 2198 | self._can_exit = namespace.Event() |
| 2199 | if not wait_before_exit: |
| 2200 | self._can_exit.set() |
| 2201 | |
| 2202 | threads = [] |
| 2203 | for i in range(n): |
| 2204 | p = namespace.Process(target=self.task) |
| 2205 | p.daemon = True |
| 2206 | p.start() |
| 2207 | threads.append(p) |
| 2208 | |
| 2209 | def finalize(threads): |
| 2210 | for p in threads: |
| 2211 | p.join() |
| 2212 | |
| 2213 | self._finalizer = weakref.finalize(self, finalize, threads) |
| 2214 | |
| 2215 | def task(self): |
| 2216 | pid = os.getpid() |