MCPcopy Index your code
hub / github.com/python/cpython / Bunch

Class Bunch

Lib/test/_test_multiprocessing.py:2183–2237  ·  view source on GitHub ↗

A bunch of threads.

Source from the content-addressed store, hash-verified

2181
2182
2183class Bunch(object):
2184 """
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()
2217 self.started.append(pid)
2218 try:
2219 self.f(*self.args)
2220 finally:
2221 self.finished.append(pid)
2222 self._can_exit.wait(30)
2223 assert self._can_exit.is_set()
2224
2225 def wait_for_started(self):
2226 while len(self.started) < self.n:
2227 _wait()
2228
2229 def wait_for_finished(self):
2230 while len(self.finished) < self.n:
2231 _wait()
2232
2233 def do_finish(self):
2234 self._can_exit.set()
2235
2236 def close(self):
2237 self._finalizer()
2238
2239
2240class AppendTrue(object):

Callers 1

run_threadsMethod · 0.70

Calls

no outgoing calls

Tested by 1

run_threadsMethod · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…