Jeremy Hylton | 92bb6e7 | 2002-08-14 19:25:42 +0000 | [diff] [blame] | 1 | """Thread module emulating a subset of Java's threading model.""" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 2 | |
Fred Drake | a872595 | 2002-12-30 23:32:50 +0000 | [diff] [blame] | 3 | import sys as _sys |
| 4 | |
| 5 | try: |
| 6 | import thread |
| 7 | except ImportError: |
| 8 | del _sys.modules[__name__] |
| 9 | raise |
| 10 | |
Jeffrey Yasskin | 105f3d4 | 2008-03-31 00:35:53 +0000 | [diff] [blame] | 11 | import warnings |
Benjamin Peterson | f439560 | 2008-06-11 17:50:00 +0000 | [diff] [blame] | 12 | |
Fred Drake | a872595 | 2002-12-30 23:32:50 +0000 | [diff] [blame] | 13 | from time import time as _time, sleep as _sleep |
Neil Schemenauer | f607fc5 | 2003-11-05 23:03:00 +0000 | [diff] [blame] | 14 | from traceback import format_exc as _format_exc |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 15 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 16 | # Note regarding PEP 8 compliant aliases |
| 17 | # This threading model was originally inspired by Java, and inherited |
| 18 | # the convention of camelCase function and method names from that |
| 19 | # language. While those names are not in any imminent danger of being |
| 20 | # deprecated, starting with Python 2.6, the module now provides a |
| 21 | # PEP 8 compliant alias for any such method name. |
| 22 | # Using the new PEP 8 compliant names also facilitates substitution |
| 23 | # with the multiprocessing module, which doesn't provide the old |
| 24 | # Java inspired names. |
| 25 | |
| 26 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 27 | # Rename some stuff so "from threading import *" is safe |
Benjamin Peterson | 13f7382 | 2008-06-11 18:02:31 +0000 | [diff] [blame] | 28 | __all__ = ['activeCount', 'active_count', 'Condition', 'currentThread', |
| 29 | 'current_thread', 'enumerate', 'Event', |
Tim Peters | 685e697 | 2003-06-29 16:50:06 +0000 | [diff] [blame] | 30 | 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 31 | 'Timer', 'setprofile', 'settrace', 'local', 'stack_size'] |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 33 | _start_new_thread = thread.start_new_thread |
| 34 | _allocate_lock = thread.allocate_lock |
| 35 | _get_ident = thread.get_ident |
Jeremy Hylton | b5fc749 | 2000-06-01 01:17:17 +0000 | [diff] [blame] | 36 | ThreadError = thread.error |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 37 | del thread |
| 38 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 39 | |
Jeffrey Yasskin | 105f3d4 | 2008-03-31 00:35:53 +0000 | [diff] [blame] | 40 | # sys.exc_clear is used to work around the fact that except blocks |
| 41 | # don't fully clear the exception until 3.0. |
| 42 | warnings.filterwarnings('ignore', category=DeprecationWarning, |
| 43 | module='threading', message='sys.exc_clear') |
| 44 | |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 45 | # Debug support (adapted from ihooks.py). |
| 46 | # All the major classes here derive from _Verbose. We force that to |
| 47 | # be a new-style class so that all the major classes here are new-style. |
| 48 | # This helps debugging (type(instance) is more revealing for instances |
| 49 | # of new-style classes). |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 50 | |
Tim Peters | 0939fac | 2003-07-01 19:28:44 +0000 | [diff] [blame] | 51 | _VERBOSE = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 52 | |
| 53 | if __debug__: |
| 54 | |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 55 | class _Verbose(object): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 56 | |
| 57 | def __init__(self, verbose=None): |
| 58 | if verbose is None: |
| 59 | verbose = _VERBOSE |
| 60 | self.__verbose = verbose |
| 61 | |
| 62 | def _note(self, format, *args): |
| 63 | if self.__verbose: |
| 64 | format = format % args |
Antoine Pitrou | 47900cf | 2010-12-17 17:45:12 +0000 | [diff] [blame] | 65 | # Issue #4188: calling current_thread() can incur an infinite |
| 66 | # recursion if it has to create a DummyThread on the fly. |
| 67 | ident = _get_ident() |
| 68 | try: |
| 69 | name = _active[ident].name |
| 70 | except KeyError: |
| 71 | name = "<OS thread %d>" % ident |
| 72 | format = "%s: %s\n" % (name, format) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 73 | _sys.stderr.write(format) |
| 74 | |
| 75 | else: |
| 76 | # Disable this when using "python -O" |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 77 | class _Verbose(object): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 78 | def __init__(self, verbose=None): |
| 79 | pass |
| 80 | def _note(self, *args): |
| 81 | pass |
| 82 | |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 83 | # Support for profile and trace hooks |
| 84 | |
| 85 | _profile_hook = None |
| 86 | _trace_hook = None |
| 87 | |
| 88 | def setprofile(func): |
| 89 | global _profile_hook |
| 90 | _profile_hook = func |
Tim Peters | d1b108b | 2003-06-29 17:24:17 +0000 | [diff] [blame] | 91 | |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 92 | def settrace(func): |
| 93 | global _trace_hook |
| 94 | _trace_hook = func |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 95 | |
| 96 | # Synchronization classes |
| 97 | |
| 98 | Lock = _allocate_lock |
| 99 | |
| 100 | def RLock(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 101 | return _RLock(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 102 | |
| 103 | class _RLock(_Verbose): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 105 | def __init__(self, verbose=None): |
| 106 | _Verbose.__init__(self, verbose) |
| 107 | self.__block = _allocate_lock() |
| 108 | self.__owner = None |
| 109 | self.__count = 0 |
| 110 | |
| 111 | def __repr__(self): |
Nick Coghlan | f8bbaa9 | 2007-07-31 13:38:01 +0000 | [diff] [blame] | 112 | owner = self.__owner |
Antoine Pitrou | d7158d4 | 2009-11-09 16:00:11 +0000 | [diff] [blame] | 113 | try: |
| 114 | owner = _active[owner].name |
| 115 | except KeyError: |
| 116 | pass |
| 117 | return "<%s owner=%r count=%d>" % ( |
| 118 | self.__class__.__name__, owner, self.__count) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 119 | |
| 120 | def acquire(self, blocking=1): |
Antoine Pitrou | d7158d4 | 2009-11-09 16:00:11 +0000 | [diff] [blame] | 121 | me = _get_ident() |
| 122 | if self.__owner == me: |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 123 | self.__count = self.__count + 1 |
| 124 | if __debug__: |
| 125 | self._note("%s.acquire(%s): recursive success", self, blocking) |
| 126 | return 1 |
| 127 | rc = self.__block.acquire(blocking) |
| 128 | if rc: |
| 129 | self.__owner = me |
| 130 | self.__count = 1 |
| 131 | if __debug__: |
Brett Cannon | 90cece7 | 2005-01-27 22:48:30 +0000 | [diff] [blame] | 132 | self._note("%s.acquire(%s): initial success", self, blocking) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 133 | else: |
| 134 | if __debug__: |
| 135 | self._note("%s.acquire(%s): failure", self, blocking) |
| 136 | return rc |
| 137 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 138 | __enter__ = acquire |
| 139 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 140 | def release(self): |
Antoine Pitrou | d7158d4 | 2009-11-09 16:00:11 +0000 | [diff] [blame] | 141 | if self.__owner != _get_ident(): |
Georg Brandl | e1254d7 | 2009-10-14 15:51:48 +0000 | [diff] [blame] | 142 | raise RuntimeError("cannot release un-acquired lock") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 143 | self.__count = count = self.__count - 1 |
| 144 | if not count: |
| 145 | self.__owner = None |
| 146 | self.__block.release() |
| 147 | if __debug__: |
| 148 | self._note("%s.release(): final release", self) |
| 149 | else: |
| 150 | if __debug__: |
| 151 | self._note("%s.release(): non-final release", self) |
| 152 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 153 | def __exit__(self, t, v, tb): |
| 154 | self.release() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 156 | # Internal methods used by condition variables |
| 157 | |
Brett Cannon | 2005050 | 2008-08-02 03:13:46 +0000 | [diff] [blame] | 158 | def _acquire_restore(self, count_owner): |
| 159 | count, owner = count_owner |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 160 | self.__block.acquire() |
| 161 | self.__count = count |
| 162 | self.__owner = owner |
| 163 | if __debug__: |
| 164 | self._note("%s._acquire_restore()", self) |
| 165 | |
| 166 | def _release_save(self): |
| 167 | if __debug__: |
| 168 | self._note("%s._release_save()", self) |
| 169 | count = self.__count |
| 170 | self.__count = 0 |
| 171 | owner = self.__owner |
| 172 | self.__owner = None |
| 173 | self.__block.release() |
| 174 | return (count, owner) |
| 175 | |
| 176 | def _is_owned(self): |
Antoine Pitrou | d7158d4 | 2009-11-09 16:00:11 +0000 | [diff] [blame] | 177 | return self.__owner == _get_ident() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def Condition(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 181 | return _Condition(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 182 | |
| 183 | class _Condition(_Verbose): |
| 184 | |
| 185 | def __init__(self, lock=None, verbose=None): |
| 186 | _Verbose.__init__(self, verbose) |
| 187 | if lock is None: |
| 188 | lock = RLock() |
| 189 | self.__lock = lock |
| 190 | # Export the lock's acquire() and release() methods |
| 191 | self.acquire = lock.acquire |
| 192 | self.release = lock.release |
| 193 | # If the lock defines _release_save() and/or _acquire_restore(), |
| 194 | # these override the default implementations (which just call |
| 195 | # release() and acquire() on the lock). Ditto for _is_owned(). |
| 196 | try: |
| 197 | self._release_save = lock._release_save |
| 198 | except AttributeError: |
| 199 | pass |
| 200 | try: |
| 201 | self._acquire_restore = lock._acquire_restore |
| 202 | except AttributeError: |
| 203 | pass |
| 204 | try: |
| 205 | self._is_owned = lock._is_owned |
| 206 | except AttributeError: |
| 207 | pass |
| 208 | self.__waiters = [] |
| 209 | |
Guido van Rossum | da5b701 | 2006-05-02 19:47:52 +0000 | [diff] [blame] | 210 | def __enter__(self): |
| 211 | return self.__lock.__enter__() |
| 212 | |
| 213 | def __exit__(self, *args): |
| 214 | return self.__lock.__exit__(*args) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 216 | def __repr__(self): |
| 217 | return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters)) |
| 218 | |
| 219 | def _release_save(self): |
| 220 | self.__lock.release() # No state to save |
| 221 | |
| 222 | def _acquire_restore(self, x): |
| 223 | self.__lock.acquire() # Ignore saved state |
| 224 | |
| 225 | def _is_owned(self): |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 226 | # Return True if lock is owned by current_thread. |
Jeremy Hylton | af7fde7 | 2002-08-14 17:43:59 +0000 | [diff] [blame] | 227 | # This method is called only if __lock doesn't have _is_owned(). |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 228 | if self.__lock.acquire(0): |
| 229 | self.__lock.release() |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 230 | return False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 231 | else: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 232 | return True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 233 | |
| 234 | def wait(self, timeout=None): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 235 | if not self._is_owned(): |
Georg Brandl | e1254d7 | 2009-10-14 15:51:48 +0000 | [diff] [blame] | 236 | raise RuntimeError("cannot wait on un-acquired lock") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 237 | waiter = _allocate_lock() |
| 238 | waiter.acquire() |
| 239 | self.__waiters.append(waiter) |
| 240 | saved_state = self._release_save() |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 241 | try: # restore state no matter what (e.g., KeyboardInterrupt) |
| 242 | if timeout is None: |
| 243 | waiter.acquire() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 244 | if __debug__: |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 245 | self._note("%s.wait(): got it", self) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 246 | else: |
Tim Peters | a6a4f27 | 2001-08-12 00:41:33 +0000 | [diff] [blame] | 247 | # Balancing act: We can't afford a pure busy loop, so we |
| 248 | # have to sleep; but if we sleep the whole timeout time, |
| 249 | # we'll be unresponsive. The scheme here sleeps very |
| 250 | # little at first, longer as time goes on, but never longer |
| 251 | # than 20 times per second (or the timeout time remaining). |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 252 | endtime = _time() + timeout |
Tim Peters | a6a4f27 | 2001-08-12 00:41:33 +0000 | [diff] [blame] | 253 | delay = 0.0005 # 500 us -> initial delay of 1 ms |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 254 | while True: |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 255 | gotit = waiter.acquire(0) |
Tim Peters | a6a4f27 | 2001-08-12 00:41:33 +0000 | [diff] [blame] | 256 | if gotit: |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 257 | break |
Tim Peters | a6a4f27 | 2001-08-12 00:41:33 +0000 | [diff] [blame] | 258 | remaining = endtime - _time() |
| 259 | if remaining <= 0: |
| 260 | break |
| 261 | delay = min(delay * 2, remaining, .05) |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 262 | _sleep(delay) |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 263 | if not gotit: |
| 264 | if __debug__: |
| 265 | self._note("%s.wait(%s): timed out", self, timeout) |
| 266 | try: |
| 267 | self.__waiters.remove(waiter) |
| 268 | except ValueError: |
| 269 | pass |
| 270 | else: |
| 271 | if __debug__: |
| 272 | self._note("%s.wait(%s): got it", self, timeout) |
| 273 | finally: |
| 274 | self._acquire_restore(saved_state) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 275 | |
| 276 | def notify(self, n=1): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 277 | if not self._is_owned(): |
Georg Brandl | e1254d7 | 2009-10-14 15:51:48 +0000 | [diff] [blame] | 278 | raise RuntimeError("cannot notify on un-acquired lock") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 279 | __waiters = self.__waiters |
| 280 | waiters = __waiters[:n] |
| 281 | if not waiters: |
| 282 | if __debug__: |
| 283 | self._note("%s.notify(): no waiters", self) |
| 284 | return |
| 285 | self._note("%s.notify(): notifying %d waiter%s", self, n, |
| 286 | n!=1 and "s" or "") |
| 287 | for waiter in waiters: |
| 288 | waiter.release() |
| 289 | try: |
| 290 | __waiters.remove(waiter) |
| 291 | except ValueError: |
| 292 | pass |
| 293 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 294 | def notifyAll(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 295 | self.notify(len(self.__waiters)) |
| 296 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 297 | notify_all = notifyAll |
Benjamin Peterson | f439560 | 2008-06-11 17:50:00 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 299 | |
| 300 | def Semaphore(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 301 | return _Semaphore(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 302 | |
| 303 | class _Semaphore(_Verbose): |
| 304 | |
Andrew M. Kuchling | 39d3bfc | 2000-02-29 00:10:24 +0000 | [diff] [blame] | 305 | # After Tim Peters' semaphore class, but not quite the same (no maximum) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 306 | |
| 307 | def __init__(self, value=1, verbose=None): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 308 | if value < 0: |
| 309 | raise ValueError("semaphore initial value must be >= 0") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 310 | _Verbose.__init__(self, verbose) |
| 311 | self.__cond = Condition(Lock()) |
| 312 | self.__value = value |
| 313 | |
| 314 | def acquire(self, blocking=1): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 315 | rc = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 316 | self.__cond.acquire() |
| 317 | while self.__value == 0: |
| 318 | if not blocking: |
| 319 | break |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 320 | if __debug__: |
| 321 | self._note("%s.acquire(%s): blocked waiting, value=%s", |
| 322 | self, blocking, self.__value) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 323 | self.__cond.wait() |
| 324 | else: |
| 325 | self.__value = self.__value - 1 |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 326 | if __debug__: |
Skip Montanaro | ae8454a | 2001-08-19 05:53:47 +0000 | [diff] [blame] | 327 | self._note("%s.acquire: success, value=%s", |
| 328 | self, self.__value) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 329 | rc = True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 330 | self.__cond.release() |
| 331 | return rc |
| 332 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 333 | __enter__ = acquire |
| 334 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 335 | def release(self): |
| 336 | self.__cond.acquire() |
| 337 | self.__value = self.__value + 1 |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 338 | if __debug__: |
Skip Montanaro | ae8454a | 2001-08-19 05:53:47 +0000 | [diff] [blame] | 339 | self._note("%s.release: success, value=%s", |
| 340 | self, self.__value) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 341 | self.__cond.notify() |
| 342 | self.__cond.release() |
| 343 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 344 | def __exit__(self, t, v, tb): |
| 345 | self.release() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 347 | |
Skip Montanaro | e428bb7 | 2001-08-20 20:27:58 +0000 | [diff] [blame] | 348 | def BoundedSemaphore(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 349 | return _BoundedSemaphore(*args, **kwargs) |
Skip Montanaro | e428bb7 | 2001-08-20 20:27:58 +0000 | [diff] [blame] | 350 | |
| 351 | class _BoundedSemaphore(_Semaphore): |
| 352 | """Semaphore that checks that # releases is <= # acquires""" |
| 353 | def __init__(self, value=1, verbose=None): |
| 354 | _Semaphore.__init__(self, value, verbose) |
| 355 | self._initial_value = value |
| 356 | |
| 357 | def release(self): |
| 358 | if self._Semaphore__value >= self._initial_value: |
| 359 | raise ValueError, "Semaphore released too many times" |
| 360 | return _Semaphore.release(self) |
| 361 | |
| 362 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 363 | def Event(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 364 | return _Event(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 365 | |
| 366 | class _Event(_Verbose): |
| 367 | |
| 368 | # After Tim Peters' event class (without is_posted()) |
| 369 | |
| 370 | def __init__(self, verbose=None): |
| 371 | _Verbose.__init__(self, verbose) |
| 372 | self.__cond = Condition(Lock()) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 373 | self.__flag = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 374 | |
Gregory P. Smith | 2b79a81 | 2011-01-04 01:10:08 +0000 | [diff] [blame] | 375 | def _reset_internal_locks(self): |
| 376 | # private! called by Thread._reset_internal_locks by _after_fork() |
| 377 | self.__cond.__init__() |
| 378 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 379 | def isSet(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 380 | return self.__flag |
| 381 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 382 | is_set = isSet |
Benjamin Peterson | f439560 | 2008-06-11 17:50:00 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 384 | def set(self): |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 385 | self.__cond.acquire() |
| 386 | try: |
Guido van Rossum | 21b6014 | 2002-11-21 21:08:39 +0000 | [diff] [blame] | 387 | self.__flag = True |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 388 | self.__cond.notify_all() |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 389 | finally: |
| 390 | self.__cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 391 | |
| 392 | def clear(self): |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 393 | self.__cond.acquire() |
| 394 | try: |
Guido van Rossum | 21b6014 | 2002-11-21 21:08:39 +0000 | [diff] [blame] | 395 | self.__flag = False |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 396 | finally: |
| 397 | self.__cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 398 | |
| 399 | def wait(self, timeout=None): |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 400 | self.__cond.acquire() |
| 401 | try: |
Guido van Rossum | 21b6014 | 2002-11-21 21:08:39 +0000 | [diff] [blame] | 402 | if not self.__flag: |
| 403 | self.__cond.wait(timeout) |
Georg Brandl | ef660e8 | 2009-03-31 20:41:08 +0000 | [diff] [blame] | 404 | return self.__flag |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 405 | finally: |
| 406 | self.__cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 408 | # Helper to generate new thread names |
| 409 | _counter = 0 |
| 410 | def _newname(template="Thread-%d"): |
| 411 | global _counter |
| 412 | _counter = _counter + 1 |
| 413 | return template % _counter |
| 414 | |
| 415 | # Active thread administration |
| 416 | _active_limbo_lock = _allocate_lock() |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 417 | _active = {} # maps thread id to Thread object |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 418 | _limbo = {} |
| 419 | |
| 420 | |
| 421 | # Main class for threads |
| 422 | |
| 423 | class Thread(_Verbose): |
| 424 | |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 425 | __initialized = False |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 426 | # Need to store a reference to sys.exc_info for printing |
| 427 | # out exceptions when a thread tries to use a global var. during interp. |
| 428 | # shutdown and thus raises an exception about trying to perform some |
| 429 | # operation on/with a NoneType |
| 430 | __exc_info = _sys.exc_info |
Jeffrey Yasskin | 8b9091f | 2008-03-28 04:11:18 +0000 | [diff] [blame] | 431 | # Keep sys.exc_clear too to clear the exception just before |
| 432 | # allowing .join() to return. |
| 433 | __exc_clear = _sys.exc_clear |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 434 | |
| 435 | def __init__(self, group=None, target=None, name=None, |
Georg Brandl | a4a8b82 | 2005-07-15 09:13:21 +0000 | [diff] [blame] | 436 | args=(), kwargs=None, verbose=None): |
Guido van Rossum | 5a43e1a | 1998-06-09 19:04:26 +0000 | [diff] [blame] | 437 | assert group is None, "group argument must be None for now" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 438 | _Verbose.__init__(self, verbose) |
Georg Brandl | a4a8b82 | 2005-07-15 09:13:21 +0000 | [diff] [blame] | 439 | if kwargs is None: |
| 440 | kwargs = {} |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 441 | self.__target = target |
| 442 | self.__name = str(name or _newname()) |
| 443 | self.__args = args |
| 444 | self.__kwargs = kwargs |
| 445 | self.__daemonic = self._set_daemon() |
Gregory P. Smith | 8856dda | 2008-06-01 23:48:47 +0000 | [diff] [blame] | 446 | self.__ident = None |
Jeffrey Yasskin | 69e1309 | 2008-02-28 06:09:19 +0000 | [diff] [blame] | 447 | self.__started = Event() |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 448 | self.__stopped = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 449 | self.__block = Condition(Lock()) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 450 | self.__initialized = True |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 451 | # sys.stderr is not stored in the class like |
| 452 | # sys.exc_info since it can be changed between instances |
| 453 | self.__stderr = _sys.stderr |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 454 | |
Gregory P. Smith | 2b79a81 | 2011-01-04 01:10:08 +0000 | [diff] [blame] | 455 | def _reset_internal_locks(self): |
| 456 | # private! Called by _after_fork() to reset our internal locks as |
| 457 | # they may be in an invalid state leading to a deadlock or crash. |
Gregory P. Smith | c876202 | 2011-01-04 18:43:54 +0000 | [diff] [blame] | 458 | if hasattr(self, '_Thread__block'): # DummyThread deletes self.__block |
| 459 | self.__block.__init__() |
Gregory P. Smith | 2b79a81 | 2011-01-04 01:10:08 +0000 | [diff] [blame] | 460 | self.__started._reset_internal_locks() |
| 461 | |
| 462 | @property |
| 463 | def _block(self): |
| 464 | # used by a unittest |
| 465 | return self.__block |
| 466 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 467 | def _set_daemon(self): |
| 468 | # Overridden in _MainThread and _DummyThread |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 469 | return current_thread().daemon |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 470 | |
| 471 | def __repr__(self): |
| 472 | assert self.__initialized, "Thread.__init__() was not called" |
| 473 | status = "initial" |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 474 | if self.__started.is_set(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 475 | status = "started" |
| 476 | if self.__stopped: |
| 477 | status = "stopped" |
| 478 | if self.__daemonic: |
Gregory P. Smith | 8856dda | 2008-06-01 23:48:47 +0000 | [diff] [blame] | 479 | status += " daemon" |
| 480 | if self.__ident is not None: |
| 481 | status += " %s" % self.__ident |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 482 | return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status) |
| 483 | |
| 484 | def start(self): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 485 | if not self.__initialized: |
| 486 | raise RuntimeError("thread.__init__() not called") |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 487 | if self.__started.is_set(): |
Senthil Kumaran | b02b311 | 2010-04-06 03:23:33 +0000 | [diff] [blame] | 488 | raise RuntimeError("threads can only be started once") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 489 | if __debug__: |
| 490 | self._note("%s.start(): starting thread", self) |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 491 | with _active_limbo_lock: |
| 492 | _limbo[self] = self |
Gregory P. Smith | 613c7a5 | 2010-02-28 18:36:09 +0000 | [diff] [blame] | 493 | try: |
| 494 | _start_new_thread(self.__bootstrap, ()) |
| 495 | except Exception: |
| 496 | with _active_limbo_lock: |
| 497 | del _limbo[self] |
| 498 | raise |
Jeffrey Yasskin | 69e1309 | 2008-02-28 06:09:19 +0000 | [diff] [blame] | 499 | self.__started.wait() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 500 | |
| 501 | def run(self): |
Jeffrey Yasskin | a885c15 | 2008-02-23 20:40:35 +0000 | [diff] [blame] | 502 | try: |
| 503 | if self.__target: |
| 504 | self.__target(*self.__args, **self.__kwargs) |
| 505 | finally: |
| 506 | # Avoid a refcycle if the thread is running a function with |
| 507 | # an argument that has a member that points to the thread. |
| 508 | del self.__target, self.__args, self.__kwargs |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 509 | |
| 510 | def __bootstrap(self): |
Guido van Rossum | 54ec61e | 2007-08-20 15:18:04 +0000 | [diff] [blame] | 511 | # Wrapper around the real bootstrap code that ignores |
| 512 | # exceptions during interpreter cleanup. Those typically |
| 513 | # happen when a daemon thread wakes up at an unfortunate |
| 514 | # moment, finds the world around it destroyed, and raises some |
| 515 | # random exception *** while trying to report the exception in |
| 516 | # __bootstrap_inner() below ***. Those random exceptions |
| 517 | # don't help anybody, and they confuse users, so we suppress |
| 518 | # them. We suppress them only when it appears that the world |
| 519 | # indeed has already been destroyed, so that exceptions in |
| 520 | # __bootstrap_inner() during normal business hours are properly |
| 521 | # reported. Also, we only suppress them for daemonic threads; |
| 522 | # if a non-daemonic encounters this, something else is wrong. |
| 523 | try: |
| 524 | self.__bootstrap_inner() |
| 525 | except: |
| 526 | if self.__daemonic and _sys is None: |
| 527 | return |
| 528 | raise |
| 529 | |
Benjamin Peterson | d906ea6 | 2009-03-31 21:34:42 +0000 | [diff] [blame] | 530 | def _set_ident(self): |
| 531 | self.__ident = _get_ident() |
| 532 | |
Guido van Rossum | 54ec61e | 2007-08-20 15:18:04 +0000 | [diff] [blame] | 533 | def __bootstrap_inner(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 534 | try: |
Benjamin Peterson | d906ea6 | 2009-03-31 21:34:42 +0000 | [diff] [blame] | 535 | self._set_ident() |
Jeffrey Yasskin | 69e1309 | 2008-02-28 06:09:19 +0000 | [diff] [blame] | 536 | self.__started.set() |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 537 | with _active_limbo_lock: |
| 538 | _active[self.__ident] = self |
| 539 | del _limbo[self] |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 540 | if __debug__: |
| 541 | self._note("%s.__bootstrap(): thread started", self) |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 542 | |
| 543 | if _trace_hook: |
| 544 | self._note("%s.__bootstrap(): registering trace hook", self) |
| 545 | _sys.settrace(_trace_hook) |
| 546 | if _profile_hook: |
| 547 | self._note("%s.__bootstrap(): registering profile hook", self) |
| 548 | _sys.setprofile(_profile_hook) |
Tim Peters | d1b108b | 2003-06-29 17:24:17 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 550 | try: |
| 551 | self.run() |
| 552 | except SystemExit: |
| 553 | if __debug__: |
| 554 | self._note("%s.__bootstrap(): raised SystemExit", self) |
| 555 | except: |
| 556 | if __debug__: |
| 557 | self._note("%s.__bootstrap(): unhandled exception", self) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 558 | # If sys.stderr is no more (most likely from interpreter |
| 559 | # shutdown) use self.__stderr. Otherwise still use sys (as in |
| 560 | # _sys) in case sys.stderr was redefined since the creation of |
| 561 | # self. |
| 562 | if _sys: |
| 563 | _sys.stderr.write("Exception in thread %s:\n%s\n" % |
Benjamin Peterson | b6a9556 | 2008-08-22 20:43:48 +0000 | [diff] [blame] | 564 | (self.name, _format_exc())) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 565 | else: |
| 566 | # Do the best job possible w/o a huge amt. of code to |
| 567 | # approximate a traceback (code ideas from |
| 568 | # Lib/traceback.py) |
| 569 | exc_type, exc_value, exc_tb = self.__exc_info() |
| 570 | try: |
| 571 | print>>self.__stderr, ( |
Benjamin Peterson | b6a9556 | 2008-08-22 20:43:48 +0000 | [diff] [blame] | 572 | "Exception in thread " + self.name + |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 573 | " (most likely raised during interpreter shutdown):") |
| 574 | print>>self.__stderr, ( |
| 575 | "Traceback (most recent call last):") |
| 576 | while exc_tb: |
| 577 | print>>self.__stderr, ( |
| 578 | ' File "%s", line %s, in %s' % |
| 579 | (exc_tb.tb_frame.f_code.co_filename, |
| 580 | exc_tb.tb_lineno, |
| 581 | exc_tb.tb_frame.f_code.co_name)) |
| 582 | exc_tb = exc_tb.tb_next |
| 583 | print>>self.__stderr, ("%s: %s" % (exc_type, exc_value)) |
| 584 | # Make sure that exc_tb gets deleted since it is a memory |
| 585 | # hog; deleting everything else is just for thoroughness |
| 586 | finally: |
| 587 | del exc_type, exc_value, exc_tb |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 588 | else: |
| 589 | if __debug__: |
| 590 | self._note("%s.__bootstrap(): normal return", self) |
Jeffrey Yasskin | 8b9091f | 2008-03-28 04:11:18 +0000 | [diff] [blame] | 591 | finally: |
| 592 | # Prevent a race in |
| 593 | # test_threading.test_no_refcycle_through_target when |
| 594 | # the exception keeps the target alive past when we |
| 595 | # assert that it's dead. |
Amaury Forgeot d'Arc | 504a48f | 2008-03-29 01:41:08 +0000 | [diff] [blame] | 596 | self.__exc_clear() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 597 | finally: |
Gregory P. Smith | 95cd5c0 | 2008-01-22 01:20:42 +0000 | [diff] [blame] | 598 | with _active_limbo_lock: |
| 599 | self.__stop() |
| 600 | try: |
| 601 | # We don't call self.__delete() because it also |
| 602 | # grabs _active_limbo_lock. |
| 603 | del _active[_get_ident()] |
| 604 | except: |
| 605 | pass |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 606 | |
| 607 | def __stop(self): |
Antoine Pitrou | 52849bf | 2012-04-19 23:55:01 +0200 | [diff] [blame] | 608 | # DummyThreads delete self.__block, but they have no waiters to |
| 609 | # notify anyway (join() is forbidden on them). |
| 610 | if not hasattr(self, '_Thread__block'): |
| 611 | return |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 612 | self.__block.acquire() |
| 613 | self.__stopped = True |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 614 | self.__block.notify_all() |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 615 | self.__block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 616 | |
| 617 | def __delete(self): |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 618 | "Remove current thread from the dict of currently running threads." |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 619 | |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 620 | # Notes about running with dummy_thread: |
| 621 | # |
| 622 | # Must take care to not raise an exception if dummy_thread is being |
| 623 | # used (and thus this module is being used as an instance of |
| 624 | # dummy_threading). dummy_thread.get_ident() always returns -1 since |
| 625 | # there is only one thread if dummy_thread is being used. Thus |
| 626 | # len(_active) is always <= 1 here, and any Thread instance created |
| 627 | # overwrites the (if any) thread currently registered in _active. |
| 628 | # |
| 629 | # An instance of _MainThread is always created by 'threading'. This |
| 630 | # gets overwritten the instant an instance of Thread is created; both |
| 631 | # threads return -1 from dummy_thread.get_ident() and thus have the |
| 632 | # same key in the dict. So when the _MainThread instance created by |
| 633 | # 'threading' tries to clean itself up when atexit calls this method |
| 634 | # it gets a KeyError if another Thread instance was created. |
| 635 | # |
| 636 | # This all means that KeyError from trying to delete something from |
| 637 | # _active if dummy_threading is being used is a red herring. But |
| 638 | # since it isn't if dummy_threading is *not* being used then don't |
| 639 | # hide the exception. |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 640 | |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 641 | try: |
Amaury Forgeot d'Arc | d7a2651 | 2008-04-03 23:07:55 +0000 | [diff] [blame] | 642 | with _active_limbo_lock: |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 643 | del _active[_get_ident()] |
Amaury Forgeot d'Arc | d7a2651 | 2008-04-03 23:07:55 +0000 | [diff] [blame] | 644 | # There must not be any python code between the previous line |
| 645 | # and after the lock is released. Otherwise a tracing function |
| 646 | # could try to acquire the lock again in the same thread, (in |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 647 | # current_thread()), and would block. |
Amaury Forgeot d'Arc | d7a2651 | 2008-04-03 23:07:55 +0000 | [diff] [blame] | 648 | except KeyError: |
| 649 | if 'dummy_threading' not in _sys.modules: |
| 650 | raise |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 651 | |
| 652 | def join(self, timeout=None): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 653 | if not self.__initialized: |
| 654 | raise RuntimeError("Thread.__init__() not called") |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 655 | if not self.__started.is_set(): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 656 | raise RuntimeError("cannot join thread before it is started") |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 657 | if self is current_thread(): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 658 | raise RuntimeError("cannot join current thread") |
| 659 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 660 | if __debug__: |
| 661 | if not self.__stopped: |
| 662 | self._note("%s.join(): waiting until thread stops", self) |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 663 | self.__block.acquire() |
| 664 | try: |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 665 | if timeout is None: |
| 666 | while not self.__stopped: |
| 667 | self.__block.wait() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 668 | if __debug__: |
| 669 | self._note("%s.join(): thread stopped", self) |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 670 | else: |
| 671 | deadline = _time() + timeout |
| 672 | while not self.__stopped: |
| 673 | delay = deadline - _time() |
| 674 | if delay <= 0: |
| 675 | if __debug__: |
| 676 | self._note("%s.join(): timed out", self) |
| 677 | break |
| 678 | self.__block.wait(delay) |
| 679 | else: |
| 680 | if __debug__: |
| 681 | self._note("%s.join(): thread stopped", self) |
Raymond Hettinger | 70ec29d | 2008-01-24 18:12:23 +0000 | [diff] [blame] | 682 | finally: |
| 683 | self.__block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 684 | |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 685 | @property |
| 686 | def name(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 687 | assert self.__initialized, "Thread.__init__() not called" |
| 688 | return self.__name |
| 689 | |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 690 | @name.setter |
| 691 | def name(self, name): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 692 | assert self.__initialized, "Thread.__init__() not called" |
| 693 | self.__name = str(name) |
| 694 | |
Benjamin Peterson | d8a8972 | 2008-08-18 16:40:03 +0000 | [diff] [blame] | 695 | @property |
| 696 | def ident(self): |
Gregory P. Smith | 8856dda | 2008-06-01 23:48:47 +0000 | [diff] [blame] | 697 | assert self.__initialized, "Thread.__init__() not called" |
| 698 | return self.__ident |
| 699 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 700 | def isAlive(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 701 | assert self.__initialized, "Thread.__init__() not called" |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 702 | return self.__started.is_set() and not self.__stopped |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 703 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 704 | is_alive = isAlive |
Benjamin Peterson | 6ee1a31 | 2008-08-18 21:53:29 +0000 | [diff] [blame] | 705 | |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 706 | @property |
| 707 | def daemon(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 708 | assert self.__initialized, "Thread.__init__() not called" |
| 709 | return self.__daemonic |
| 710 | |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 711 | @daemon.setter |
| 712 | def daemon(self, daemonic): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 713 | if not self.__initialized: |
| 714 | raise RuntimeError("Thread.__init__() not called") |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 715 | if self.__started.is_set(): |
Collin Winter | 50b79ce | 2007-06-06 00:17:35 +0000 | [diff] [blame] | 716 | raise RuntimeError("cannot set daemon status of active thread"); |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 717 | self.__daemonic = daemonic |
| 718 | |
Benjamin Peterson | d810626 | 2008-08-18 18:13:17 +0000 | [diff] [blame] | 719 | def isDaemon(self): |
| 720 | return self.daemon |
| 721 | |
| 722 | def setDaemon(self, daemonic): |
| 723 | self.daemon = daemonic |
| 724 | |
| 725 | def getName(self): |
| 726 | return self.name |
| 727 | |
| 728 | def setName(self, name): |
| 729 | self.name = name |
| 730 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 731 | # The timer class was contributed by Itamar Shtull-Trauring |
| 732 | |
| 733 | def Timer(*args, **kwargs): |
| 734 | return _Timer(*args, **kwargs) |
| 735 | |
| 736 | class _Timer(Thread): |
| 737 | """Call a function after a specified number of seconds: |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 738 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 739 | t = Timer(30.0, f, args=[], kwargs={}) |
| 740 | t.start() |
| 741 | t.cancel() # stop the timer's action if it's still waiting |
| 742 | """ |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 743 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 744 | def __init__(self, interval, function, args=[], kwargs={}): |
| 745 | Thread.__init__(self) |
| 746 | self.interval = interval |
| 747 | self.function = function |
| 748 | self.args = args |
| 749 | self.kwargs = kwargs |
| 750 | self.finished = Event() |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 751 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 752 | def cancel(self): |
| 753 | """Stop the timer if it hasn't finished yet""" |
| 754 | self.finished.set() |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 755 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 756 | def run(self): |
| 757 | self.finished.wait(self.interval) |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 758 | if not self.finished.is_set(): |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 759 | self.function(*self.args, **self.kwargs) |
| 760 | self.finished.set() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 761 | |
| 762 | # Special thread class to represent the main thread |
| 763 | # This is garbage collected through an exit handler |
| 764 | |
| 765 | class _MainThread(Thread): |
| 766 | |
| 767 | def __init__(self): |
| 768 | Thread.__init__(self, name="MainThread") |
Jeffrey Yasskin | 69e1309 | 2008-02-28 06:09:19 +0000 | [diff] [blame] | 769 | self._Thread__started.set() |
Benjamin Peterson | d906ea6 | 2009-03-31 21:34:42 +0000 | [diff] [blame] | 770 | self._set_ident() |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 771 | with _active_limbo_lock: |
| 772 | _active[_get_ident()] = self |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 773 | |
| 774 | def _set_daemon(self): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 775 | return False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 776 | |
Martin v. Löwis | 7b7c9d4 | 2007-01-04 21:06:12 +0000 | [diff] [blame] | 777 | def _exitfunc(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 778 | self._Thread__stop() |
| 779 | t = _pickSomeNonDaemonThread() |
| 780 | if t: |
| 781 | if __debug__: |
| 782 | self._note("%s: waiting for other threads", self) |
| 783 | while t: |
| 784 | t.join() |
| 785 | t = _pickSomeNonDaemonThread() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 786 | if __debug__: |
| 787 | self._note("%s: exiting", self) |
| 788 | self._Thread__delete() |
| 789 | |
| 790 | def _pickSomeNonDaemonThread(): |
| 791 | for t in enumerate(): |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 792 | if not t.daemon and t.is_alive(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 793 | return t |
| 794 | return None |
| 795 | |
| 796 | |
| 797 | # Dummy thread class to represent threads not started here. |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 798 | # These aren't garbage collected when they die, nor can they be waited for. |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 799 | # If they invoke anything in threading.py that calls current_thread(), they |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 800 | # leave an entry in the _active dict forever after. |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 801 | # Their purpose is to return *something* from current_thread(). |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 802 | # They are marked as daemon threads so we won't wait for them |
| 803 | # when we exit (conform previous semantics). |
| 804 | |
| 805 | class _DummyThread(Thread): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 806 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 807 | def __init__(self): |
| 808 | Thread.__init__(self, name=_newname("Dummy-%d")) |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 809 | |
| 810 | # Thread.__block consumes an OS-level locking primitive, which |
| 811 | # can never be used by a _DummyThread. Since a _DummyThread |
| 812 | # instance is immortal, that's bad, so release this resource. |
Brett Cannon | e6539c4 | 2005-01-08 02:43:53 +0000 | [diff] [blame] | 813 | del self._Thread__block |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 814 | |
Jeffrey Yasskin | 69e1309 | 2008-02-28 06:09:19 +0000 | [diff] [blame] | 815 | self._Thread__started.set() |
Benjamin Peterson | d906ea6 | 2009-03-31 21:34:42 +0000 | [diff] [blame] | 816 | self._set_ident() |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 817 | with _active_limbo_lock: |
| 818 | _active[_get_ident()] = self |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 819 | |
| 820 | def _set_daemon(self): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 821 | return True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 822 | |
Neal Norwitz | 45bec8c | 2002-02-19 03:01:36 +0000 | [diff] [blame] | 823 | def join(self, timeout=None): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 824 | assert False, "cannot join a dummy thread" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 825 | |
| 826 | |
| 827 | # Global API functions |
| 828 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 829 | def currentThread(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 830 | try: |
| 831 | return _active[_get_ident()] |
| 832 | except KeyError: |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 833 | ##print "current_thread(): no current thread for", _get_ident() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 834 | return _DummyThread() |
| 835 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 836 | current_thread = currentThread |
Benjamin Peterson | f439560 | 2008-06-11 17:50:00 +0000 | [diff] [blame] | 837 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 838 | def activeCount(): |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 839 | with _active_limbo_lock: |
| 840 | return len(_active) + len(_limbo) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 841 | |
Benjamin Peterson | 973e6c2 | 2008-09-01 23:12:58 +0000 | [diff] [blame] | 842 | active_count = activeCount |
Benjamin Peterson | f439560 | 2008-06-11 17:50:00 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | 99c160b | 2009-11-05 13:42:29 +0000 | [diff] [blame] | 844 | def _enumerate(): |
| 845 | # Same as enumerate(), but without the lock. Internal use only. |
| 846 | return _active.values() + _limbo.values() |
| 847 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 848 | def enumerate(): |
Benjamin Peterson | bd9dd31 | 2009-03-31 21:06:30 +0000 | [diff] [blame] | 849 | with _active_limbo_lock: |
| 850 | return _active.values() + _limbo.values() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 851 | |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 852 | from thread import stack_size |
| 853 | |
Martin v. Löwis | 7b7c9d4 | 2007-01-04 21:06:12 +0000 | [diff] [blame] | 854 | # Create the main thread object, |
| 855 | # and make it available for the interpreter |
| 856 | # (Py_Main) as threading._shutdown. |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 857 | |
Martin v. Löwis | 7b7c9d4 | 2007-01-04 21:06:12 +0000 | [diff] [blame] | 858 | _shutdown = _MainThread()._exitfunc |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 859 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 860 | # get thread-local implementation, either from the thread |
| 861 | # module, or from the python fallback |
| 862 | |
| 863 | try: |
| 864 | from thread import _local as local |
| 865 | except ImportError: |
| 866 | from _threading_local import local |
| 867 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 868 | |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 869 | def _after_fork(): |
| 870 | # This function is called by Python/ceval.c:PyEval_ReInitThreads which |
| 871 | # is called from PyOS_AfterFork. Here we cleanup threading module state |
| 872 | # that should not exist after a fork. |
| 873 | |
| 874 | # Reset _active_limbo_lock, in case we forked while the lock was held |
| 875 | # by another (non-forked) thread. http://bugs.python.org/issue874900 |
| 876 | global _active_limbo_lock |
| 877 | _active_limbo_lock = _allocate_lock() |
| 878 | |
| 879 | # fork() only copied the current thread; clear references to others. |
| 880 | new_active = {} |
| 881 | current = current_thread() |
| 882 | with _active_limbo_lock: |
Antoine Pitrou | 9fb1aca | 2008-09-06 23:04:32 +0000 | [diff] [blame] | 883 | for thread in _active.itervalues(): |
Charles-François Natali | 4161630 | 2011-12-18 18:22:24 +0100 | [diff] [blame] | 884 | # Any lock/condition variable may be currently locked or in an |
| 885 | # invalid state, so we reinitialize them. |
| 886 | if hasattr(thread, '_reset_internal_locks'): |
| 887 | thread._reset_internal_locks() |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 888 | if thread is current: |
Antoine Pitrou | 9fb1aca | 2008-09-06 23:04:32 +0000 | [diff] [blame] | 889 | # There is only one active thread. We reset the ident to |
| 890 | # its new value since it can have changed. |
| 891 | ident = _get_ident() |
| 892 | thread._Thread__ident = ident |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 893 | new_active[ident] = thread |
| 894 | else: |
| 895 | # All the others are already stopped. |
Charles-François Natali | 4161630 | 2011-12-18 18:22:24 +0100 | [diff] [blame] | 896 | thread._Thread__stop() |
Jesse Noller | 5e62ca4 | 2008-07-16 20:03:47 +0000 | [diff] [blame] | 897 | |
| 898 | _limbo.clear() |
| 899 | _active.clear() |
| 900 | _active.update(new_active) |
| 901 | assert len(_active) == 1 |
| 902 | |
| 903 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 904 | # Self-test code |
| 905 | |
| 906 | def _test(): |
| 907 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 908 | class BoundedQueue(_Verbose): |
| 909 | |
| 910 | def __init__(self, limit): |
| 911 | _Verbose.__init__(self) |
| 912 | self.mon = RLock() |
| 913 | self.rc = Condition(self.mon) |
| 914 | self.wc = Condition(self.mon) |
| 915 | self.limit = limit |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 916 | self.queue = deque() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 917 | |
| 918 | def put(self, item): |
| 919 | self.mon.acquire() |
| 920 | while len(self.queue) >= self.limit: |
| 921 | self._note("put(%s): queue full", item) |
| 922 | self.wc.wait() |
| 923 | self.queue.append(item) |
| 924 | self._note("put(%s): appended, length now %d", |
| 925 | item, len(self.queue)) |
| 926 | self.rc.notify() |
| 927 | self.mon.release() |
| 928 | |
| 929 | def get(self): |
| 930 | self.mon.acquire() |
| 931 | while not self.queue: |
| 932 | self._note("get(): queue empty") |
| 933 | self.rc.wait() |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 934 | item = self.queue.popleft() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 935 | self._note("get(): got %s, %d left", item, len(self.queue)) |
| 936 | self.wc.notify() |
| 937 | self.mon.release() |
| 938 | return item |
| 939 | |
| 940 | class ProducerThread(Thread): |
| 941 | |
| 942 | def __init__(self, queue, quota): |
| 943 | Thread.__init__(self, name="Producer") |
| 944 | self.queue = queue |
| 945 | self.quota = quota |
| 946 | |
| 947 | def run(self): |
Guido van Rossum | b26a1b4 | 1998-05-20 17:05:52 +0000 | [diff] [blame] | 948 | from random import random |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 949 | counter = 0 |
| 950 | while counter < self.quota: |
| 951 | counter = counter + 1 |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 952 | self.queue.put("%s.%d" % (self.name, counter)) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 953 | _sleep(random() * 0.00001) |
| 954 | |
| 955 | |
| 956 | class ConsumerThread(Thread): |
| 957 | |
| 958 | def __init__(self, queue, count): |
| 959 | Thread.__init__(self, name="Consumer") |
| 960 | self.queue = queue |
| 961 | self.count = count |
| 962 | |
| 963 | def run(self): |
| 964 | while self.count > 0: |
| 965 | item = self.queue.get() |
| 966 | print item |
| 967 | self.count = self.count - 1 |
| 968 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 969 | NP = 3 |
| 970 | QL = 4 |
| 971 | NI = 5 |
| 972 | |
| 973 | Q = BoundedQueue(QL) |
| 974 | P = [] |
| 975 | for i in range(NP): |
| 976 | t = ProducerThread(Q, NI) |
Benjamin Peterson | cbae869 | 2008-08-18 17:45:09 +0000 | [diff] [blame] | 977 | t.name = ("Producer-%d" % (i+1)) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 978 | P.append(t) |
| 979 | C = ConsumerThread(Q, NI*NP) |
| 980 | for t in P: |
| 981 | t.start() |
| 982 | _sleep(0.000001) |
| 983 | C.start() |
| 984 | for t in P: |
| 985 | t.join() |
| 986 | C.join() |
| 987 | |
| 988 | if __name__ == '__main__': |
| 989 | _test() |