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)
| 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() |