A bunch of threads.
| 1977 | |
| 1978 | |
| 1979 | class Bunch(object): |
| 1980 | """ |
| 1981 | A bunch of threads. |
| 1982 | """ |
| 1983 | def __init__(self, namespace, f, args, n, wait_before_exit=False): |
| 1984 | """ |
| 1985 | Construct a bunch of `n` threads running the same function `f`. |
| 1986 | If `wait_before_exit` is True, the threads won't terminate until |
| 1987 | do_finish() is called. |
| 1988 | """ |
| 1989 | self.f = f |
| 1990 | self.args = args |
| 1991 | self.n = n |
| 1992 | self.started = namespace.DummyList() |
| 1993 | self.finished = namespace.DummyList() |
| 1994 | self._can_exit = namespace.Event() |
| 1995 | if not wait_before_exit: |
| 1996 | self._can_exit.set() |
| 1997 | |
| 1998 | threads = [] |
| 1999 | for i in range(n): |
| 2000 | p = namespace.Process(target=self.task) |
| 2001 | p.daemon = True |
| 2002 | p.start() |
| 2003 | threads.append(p) |
| 2004 | |
| 2005 | def finalize(threads): |
| 2006 | for p in threads: |
| 2007 | p.join() |
| 2008 | |
| 2009 | self._finalizer = weakref.finalize(self, finalize, threads) |
| 2010 | |
| 2011 | def task(self): |
| 2012 | pid = os.getpid() |
| 2013 | self.started.append(pid) |
| 2014 | try: |
| 2015 | self.f(*self.args) |
| 2016 | finally: |
| 2017 | self.finished.append(pid) |
| 2018 | self._can_exit.wait(30) |
| 2019 | assert self._can_exit.is_set() |
| 2020 | |
| 2021 | def wait_for_started(self): |
| 2022 | while len(self.started) < self.n: |
| 2023 | _wait() |
| 2024 | |
| 2025 | def wait_for_finished(self): |
| 2026 | while len(self.finished) < self.n: |
| 2027 | _wait() |
| 2028 | |
| 2029 | def do_finish(self): |
| 2030 | self._can_exit.set() |
| 2031 | |
| 2032 | def close(self): |
| 2033 | self._finalizer() |
| 2034 | |
| 2035 | |
| 2036 | class AppendTrue(object): |