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 |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 4 | import _thread |
Fred Drake | a872595 | 2002-12-30 23:32:50 +0000 | [diff] [blame] | 5 | |
Fred Drake | a872595 | 2002-12-30 23:32:50 +0000 | [diff] [blame] | 6 | from time import time as _time, sleep as _sleep |
Neil Schemenauer | f607fc5 | 2003-11-05 23:03:00 +0000 | [diff] [blame] | 7 | from traceback import format_exc as _format_exc |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 8 | from collections import deque |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 10 | # Note regarding PEP 8 compliant names |
| 11 | # This threading model was originally inspired by Java, and inherited |
| 12 | # the convention of camelCase function and method names from that |
| 13 | # language. Those originaly names are not in any imminent danger of |
| 14 | # being deprecated (even for Py3k),so this module provides them as an |
| 15 | # alias for the PEP 8 compliant names |
| 16 | # Note that using the new PEP 8 compliant names facilitates substitution |
| 17 | # with the multiprocessing module, which doesn't provide the old |
| 18 | # Java inspired names. |
| 19 | |
| 20 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 21 | # Rename some stuff so "from threading import *" is safe |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 22 | __all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event', |
Tim Peters | 685e697 | 2003-06-29 16:50:06 +0000 | [diff] [blame] | 23 | 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 24 | 'Timer', 'setprofile', 'settrace', 'local', 'stack_size'] |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 26 | _start_new_thread = _thread.start_new_thread |
| 27 | _allocate_lock = _thread.allocate_lock |
| 28 | _get_ident = _thread.get_ident |
| 29 | ThreadError = _thread.error |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 30 | try: |
| 31 | _CRLock = _thread.RLock |
| 32 | except AttributeError: |
| 33 | _CRLock = None |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 34 | TIMEOUT_MAX = _thread.TIMEOUT_MAX |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 35 | del _thread |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 37 | |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 38 | # Debug support (adapted from ihooks.py). |
| 39 | # All the major classes here derive from _Verbose. We force that to |
| 40 | # be a new-style class so that all the major classes here are new-style. |
| 41 | # This helps debugging (type(instance) is more revealing for instances |
| 42 | # of new-style classes). |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 43 | |
Tim Peters | 0939fac | 2003-07-01 19:28:44 +0000 | [diff] [blame] | 44 | _VERBOSE = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 45 | |
| 46 | if __debug__: |
| 47 | |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 48 | class _Verbose(object): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 49 | |
| 50 | def __init__(self, verbose=None): |
| 51 | if verbose is None: |
| 52 | verbose = _VERBOSE |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 53 | self._verbose = verbose |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 54 | |
| 55 | def _note(self, format, *args): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 56 | if self._verbose: |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 57 | format = format % args |
| 58 | format = "%s: %s\n" % ( |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 59 | current_thread().name, format) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 60 | _sys.stderr.write(format) |
| 61 | |
| 62 | else: |
| 63 | # Disable this when using "python -O" |
Tim Peters | 59aba12 | 2003-07-01 20:01:55 +0000 | [diff] [blame] | 64 | class _Verbose(object): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 65 | def __init__(self, verbose=None): |
| 66 | pass |
| 67 | def _note(self, *args): |
| 68 | pass |
| 69 | |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 70 | # Support for profile and trace hooks |
| 71 | |
| 72 | _profile_hook = None |
| 73 | _trace_hook = None |
| 74 | |
| 75 | def setprofile(func): |
| 76 | global _profile_hook |
| 77 | _profile_hook = func |
Tim Peters | d1b108b | 2003-06-29 17:24:17 +0000 | [diff] [blame] | 78 | |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 79 | def settrace(func): |
| 80 | global _trace_hook |
| 81 | _trace_hook = func |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 82 | |
| 83 | # Synchronization classes |
| 84 | |
| 85 | Lock = _allocate_lock |
| 86 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 87 | def RLock(verbose=None, *args, **kwargs): |
| 88 | if verbose is None: |
| 89 | verbose = _VERBOSE |
| 90 | if (__debug__ and verbose) or _CRLock is None: |
| 91 | return _PyRLock(verbose, *args, **kwargs) |
| 92 | return _CRLock(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 93 | |
| 94 | class _RLock(_Verbose): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 96 | def __init__(self, verbose=None): |
| 97 | _Verbose.__init__(self, verbose) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 98 | self._block = _allocate_lock() |
| 99 | self._owner = None |
| 100 | self._count = 0 |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 101 | |
| 102 | def __repr__(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 103 | owner = self._owner |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 104 | try: |
| 105 | owner = _active[owner].name |
| 106 | except KeyError: |
| 107 | pass |
| 108 | return "<%s owner=%r count=%d>" % ( |
| 109 | self.__class__.__name__, owner, self._count) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 111 | def acquire(self, blocking=True, timeout=-1): |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 112 | me = _get_ident() |
| 113 | if self._owner == me: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 114 | self._count = self._count + 1 |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 115 | if __debug__: |
| 116 | self._note("%s.acquire(%s): recursive success", self, blocking) |
| 117 | return 1 |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 118 | rc = self._block.acquire(blocking, timeout) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 119 | if rc: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 120 | self._owner = me |
| 121 | self._count = 1 |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 122 | if __debug__: |
Brett Cannon | 90cece7 | 2005-01-27 22:48:30 +0000 | [diff] [blame] | 123 | self._note("%s.acquire(%s): initial success", self, blocking) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 124 | else: |
| 125 | if __debug__: |
| 126 | self._note("%s.acquire(%s): failure", self, blocking) |
| 127 | return rc |
| 128 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 129 | __enter__ = acquire |
| 130 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 131 | def release(self): |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 132 | if self._owner != _get_ident(): |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 133 | raise RuntimeError("cannot release un-acquired lock") |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 134 | self._count = count = self._count - 1 |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 135 | if not count: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 136 | self._owner = None |
| 137 | self._block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 138 | if __debug__: |
| 139 | self._note("%s.release(): final release", self) |
| 140 | else: |
| 141 | if __debug__: |
| 142 | self._note("%s.release(): non-final release", self) |
| 143 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 144 | def __exit__(self, t, v, tb): |
| 145 | self.release() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 147 | # Internal methods used by condition variables |
| 148 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 149 | def _acquire_restore(self, state): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 150 | self._block.acquire() |
| 151 | self._count, self._owner = state |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 152 | if __debug__: |
| 153 | self._note("%s._acquire_restore()", self) |
| 154 | |
| 155 | def _release_save(self): |
| 156 | if __debug__: |
| 157 | self._note("%s._release_save()", self) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 158 | count = self._count |
| 159 | self._count = 0 |
| 160 | owner = self._owner |
| 161 | self._owner = None |
| 162 | self._block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 163 | return (count, owner) |
| 164 | |
| 165 | def _is_owned(self): |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 166 | return self._owner == _get_ident() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 168 | _PyRLock = _RLock |
| 169 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 170 | |
| 171 | def Condition(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 172 | return _Condition(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 173 | |
| 174 | class _Condition(_Verbose): |
| 175 | |
| 176 | def __init__(self, lock=None, verbose=None): |
| 177 | _Verbose.__init__(self, verbose) |
| 178 | if lock is None: |
| 179 | lock = RLock() |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 180 | self._lock = lock |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 181 | # Export the lock's acquire() and release() methods |
| 182 | self.acquire = lock.acquire |
| 183 | self.release = lock.release |
| 184 | # If the lock defines _release_save() and/or _acquire_restore(), |
| 185 | # these override the default implementations (which just call |
| 186 | # release() and acquire() on the lock). Ditto for _is_owned(). |
| 187 | try: |
| 188 | self._release_save = lock._release_save |
| 189 | except AttributeError: |
| 190 | pass |
| 191 | try: |
| 192 | self._acquire_restore = lock._acquire_restore |
| 193 | except AttributeError: |
| 194 | pass |
| 195 | try: |
| 196 | self._is_owned = lock._is_owned |
| 197 | except AttributeError: |
| 198 | pass |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 199 | self._waiters = [] |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 200 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 201 | def __enter__(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 202 | return self._lock.__enter__() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 204 | def __exit__(self, *args): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 205 | return self._lock.__exit__(*args) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 207 | def __repr__(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 208 | return "<Condition(%s, %d)>" % (self._lock, len(self._waiters)) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 209 | |
| 210 | def _release_save(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 211 | self._lock.release() # No state to save |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 212 | |
| 213 | def _acquire_restore(self, x): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 214 | self._lock.acquire() # Ignore saved state |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 215 | |
| 216 | def _is_owned(self): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 217 | # Return True if lock is owned by current_thread. |
Jeremy Hylton | af7fde7 | 2002-08-14 17:43:59 +0000 | [diff] [blame] | 218 | # This method is called only if __lock doesn't have _is_owned(). |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 219 | if self._lock.acquire(0): |
| 220 | self._lock.release() |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 221 | return False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 222 | else: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 223 | return True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 224 | |
| 225 | def wait(self, timeout=None): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 226 | if not self._is_owned(): |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 227 | raise RuntimeError("cannot wait on un-acquired lock") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 228 | waiter = _allocate_lock() |
| 229 | waiter.acquire() |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 230 | self._waiters.append(waiter) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 231 | saved_state = self._release_save() |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 232 | try: # restore state no matter what (e.g., KeyboardInterrupt) |
| 233 | if timeout is None: |
| 234 | waiter.acquire() |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 235 | gotit = True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 236 | if __debug__: |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 237 | self._note("%s.wait(): got it", self) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 238 | else: |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 239 | if timeout > 0: |
| 240 | gotit = waiter.acquire(True, timeout) |
| 241 | else: |
| 242 | gotit = waiter.acquire(False) |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 243 | if not gotit: |
| 244 | if __debug__: |
| 245 | self._note("%s.wait(%s): timed out", self, timeout) |
| 246 | try: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 247 | self._waiters.remove(waiter) |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 248 | except ValueError: |
| 249 | pass |
| 250 | else: |
| 251 | if __debug__: |
| 252 | self._note("%s.wait(%s): got it", self, timeout) |
Georg Brandl | b9a4391 | 2010-10-28 09:03:20 +0000 | [diff] [blame] | 253 | return gotit |
Tim Peters | c951bf9 | 2001-04-02 20:15:57 +0000 | [diff] [blame] | 254 | finally: |
| 255 | self._acquire_restore(saved_state) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 256 | |
Kristján Valur Jónsson | 6331520 | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 257 | def wait_for(self, predicate, timeout=None): |
| 258 | endtime = None |
| 259 | waittime = timeout |
| 260 | result = predicate() |
| 261 | while not result: |
| 262 | if waittime is not None: |
| 263 | if endtime is None: |
| 264 | endtime = _time() + waittime |
| 265 | else: |
| 266 | waittime = endtime - _time() |
| 267 | if waittime <= 0: |
| 268 | if __debug__: |
| 269 | self._note("%s.wait_for(%r, %r): Timed out.", |
| 270 | self, predicate, timeout) |
| 271 | break |
| 272 | if __debug__: |
| 273 | self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.", |
| 274 | self, predicate, timeout, waittime) |
| 275 | self.wait(waittime) |
| 276 | result = predicate() |
| 277 | else: |
| 278 | if __debug__: |
| 279 | self._note("%s.wait_for(%r, %r): Success.", |
| 280 | self, predicate, timeout) |
| 281 | return result |
| 282 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 283 | def notify(self, n=1): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 284 | if not self._is_owned(): |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 285 | raise RuntimeError("cannot notify on un-acquired lock") |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 286 | __waiters = self._waiters |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 287 | waiters = __waiters[:n] |
| 288 | if not waiters: |
| 289 | if __debug__: |
| 290 | self._note("%s.notify(): no waiters", self) |
| 291 | return |
| 292 | self._note("%s.notify(): notifying %d waiter%s", self, n, |
| 293 | n!=1 and "s" or "") |
| 294 | for waiter in waiters: |
| 295 | waiter.release() |
| 296 | try: |
| 297 | __waiters.remove(waiter) |
| 298 | except ValueError: |
| 299 | pass |
| 300 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 301 | def notify_all(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 302 | self.notify(len(self._waiters)) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 303 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 304 | notifyAll = notify_all |
| 305 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 306 | |
| 307 | def Semaphore(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 308 | return _Semaphore(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 309 | |
| 310 | class _Semaphore(_Verbose): |
| 311 | |
Andrew M. Kuchling | 39d3bfc | 2000-02-29 00:10:24 +0000 | [diff] [blame] | 312 | # 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] | 313 | |
| 314 | def __init__(self, value=1, verbose=None): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 315 | if value < 0: |
| 316 | raise ValueError("semaphore initial value must be >= 0") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 317 | _Verbose.__init__(self, verbose) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 318 | self._cond = Condition(Lock()) |
| 319 | self._value = value |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | 0454af9 | 2010-04-17 23:51:58 +0000 | [diff] [blame] | 321 | def acquire(self, blocking=True, timeout=None): |
| 322 | if not blocking and timeout is not None: |
| 323 | raise ValueError("can't specify timeout for non-blocking acquire") |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 324 | rc = False |
Antoine Pitrou | 0454af9 | 2010-04-17 23:51:58 +0000 | [diff] [blame] | 325 | endtime = None |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 326 | self._cond.acquire() |
| 327 | while self._value == 0: |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 328 | if not blocking: |
| 329 | break |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 330 | if __debug__: |
| 331 | self._note("%s.acquire(%s): blocked waiting, value=%s", |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 332 | self, blocking, self._value) |
Antoine Pitrou | 0454af9 | 2010-04-17 23:51:58 +0000 | [diff] [blame] | 333 | if timeout is not None: |
| 334 | if endtime is None: |
| 335 | endtime = _time() + timeout |
| 336 | else: |
| 337 | timeout = endtime - _time() |
| 338 | if timeout <= 0: |
| 339 | break |
| 340 | self._cond.wait(timeout) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 341 | else: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 342 | self._value = self._value - 1 |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 343 | if __debug__: |
Skip Montanaro | ae8454a | 2001-08-19 05:53:47 +0000 | [diff] [blame] | 344 | self._note("%s.acquire: success, value=%s", |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 345 | self, self._value) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 346 | rc = True |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 347 | self._cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 348 | return rc |
| 349 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 350 | __enter__ = acquire |
| 351 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 352 | def release(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 353 | self._cond.acquire() |
| 354 | self._value = self._value + 1 |
Skip Montanaro | b446fc7 | 2001-08-19 04:25:24 +0000 | [diff] [blame] | 355 | if __debug__: |
Skip Montanaro | ae8454a | 2001-08-19 05:53:47 +0000 | [diff] [blame] | 356 | self._note("%s.release: success, value=%s", |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 357 | self, self._value) |
| 358 | self._cond.notify() |
| 359 | self._cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 361 | def __exit__(self, t, v, tb): |
| 362 | self.release() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 364 | |
Skip Montanaro | e428bb7 | 2001-08-20 20:27:58 +0000 | [diff] [blame] | 365 | def BoundedSemaphore(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 366 | return _BoundedSemaphore(*args, **kwargs) |
Skip Montanaro | e428bb7 | 2001-08-20 20:27:58 +0000 | [diff] [blame] | 367 | |
| 368 | class _BoundedSemaphore(_Semaphore): |
| 369 | """Semaphore that checks that # releases is <= # acquires""" |
| 370 | def __init__(self, value=1, verbose=None): |
| 371 | _Semaphore.__init__(self, value, verbose) |
| 372 | self._initial_value = value |
| 373 | |
| 374 | def release(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 375 | if self._value >= self._initial_value: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 376 | raise ValueError("Semaphore released too many times") |
Skip Montanaro | e428bb7 | 2001-08-20 20:27:58 +0000 | [diff] [blame] | 377 | return _Semaphore.release(self) |
| 378 | |
| 379 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 380 | def Event(*args, **kwargs): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 381 | return _Event(*args, **kwargs) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 382 | |
| 383 | class _Event(_Verbose): |
| 384 | |
| 385 | # After Tim Peters' event class (without is_posted()) |
| 386 | |
| 387 | def __init__(self, verbose=None): |
| 388 | _Verbose.__init__(self, verbose) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 389 | self._cond = Condition(Lock()) |
| 390 | self._flag = False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 391 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 392 | def is_set(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 393 | return self._flag |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 394 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 395 | isSet = is_set |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 397 | def set(self): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 398 | self._cond.acquire() |
| 399 | try: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 400 | self._flag = True |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 401 | self._cond.notify_all() |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 402 | finally: |
| 403 | self._cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 404 | |
| 405 | def clear(self): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 406 | self._cond.acquire() |
| 407 | try: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 408 | self._flag = False |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 409 | finally: |
| 410 | self._cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 411 | |
| 412 | def wait(self, timeout=None): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 413 | self._cond.acquire() |
| 414 | try: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 415 | if not self._flag: |
| 416 | self._cond.wait(timeout) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 417 | return self._flag |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 418 | finally: |
| 419 | self._cond.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 420 | |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 421 | |
| 422 | # A barrier class. Inspired in part by the pthread_barrier_* api and |
| 423 | # the CyclicBarrier class from Java. See |
| 424 | # http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and |
| 425 | # http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ |
| 426 | # CyclicBarrier.html |
| 427 | # for information. |
| 428 | # We maintain two main states, 'filling' and 'draining' enabling the barrier |
| 429 | # to be cyclic. Threads are not allowed into it until it has fully drained |
| 430 | # since the previous cycle. In addition, a 'resetting' state exists which is |
| 431 | # similar to 'draining' except that threads leave with a BrokenBarrierError, |
| 432 | # and a 'broken' state in which all threads get get the exception. |
| 433 | class Barrier(_Verbose): |
| 434 | """ |
| 435 | Barrier. Useful for synchronizing a fixed number of threads |
| 436 | at known synchronization points. Threads block on 'wait()' and are |
| 437 | simultaneously once they have all made that call. |
| 438 | """ |
| 439 | def __init__(self, parties, action=None, timeout=None, verbose=None): |
| 440 | """ |
| 441 | Create a barrier, initialised to 'parties' threads. |
| 442 | 'action' is a callable which, when supplied, will be called |
| 443 | by one of the threads after they have all entered the |
| 444 | barrier and just prior to releasing them all. |
| 445 | If a 'timeout' is provided, it is uses as the default for |
| 446 | all subsequent 'wait()' calls. |
| 447 | """ |
| 448 | _Verbose.__init__(self, verbose) |
| 449 | self._cond = Condition(Lock()) |
| 450 | self._action = action |
| 451 | self._timeout = timeout |
| 452 | self._parties = parties |
| 453 | self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken |
| 454 | self._count = 0 |
| 455 | |
| 456 | def wait(self, timeout=None): |
| 457 | """ |
| 458 | Wait for the barrier. When the specified number of threads have |
| 459 | started waiting, they are all simultaneously awoken. If an 'action' |
| 460 | was provided for the barrier, one of the threads will have executed |
| 461 | that callback prior to returning. |
| 462 | Returns an individual index number from 0 to 'parties-1'. |
| 463 | """ |
| 464 | if timeout is None: |
| 465 | timeout = self._timeout |
| 466 | with self._cond: |
| 467 | self._enter() # Block while the barrier drains. |
| 468 | index = self._count |
| 469 | self._count += 1 |
| 470 | try: |
| 471 | if index + 1 == self._parties: |
| 472 | # We release the barrier |
| 473 | self._release() |
| 474 | else: |
| 475 | # We wait until someone releases us |
| 476 | self._wait(timeout) |
| 477 | return index |
| 478 | finally: |
| 479 | self._count -= 1 |
| 480 | # Wake up any threads waiting for barrier to drain. |
| 481 | self._exit() |
| 482 | |
| 483 | # Block until the barrier is ready for us, or raise an exception |
| 484 | # if it is broken. |
| 485 | def _enter(self): |
| 486 | while self._state in (-1, 1): |
| 487 | # It is draining or resetting, wait until done |
| 488 | self._cond.wait() |
| 489 | #see if the barrier is in a broken state |
| 490 | if self._state < 0: |
| 491 | raise BrokenBarrierError |
| 492 | assert self._state == 0 |
| 493 | |
| 494 | # Optionally run the 'action' and release the threads waiting |
| 495 | # in the barrier. |
| 496 | def _release(self): |
| 497 | try: |
| 498 | if self._action: |
| 499 | self._action() |
| 500 | # enter draining state |
| 501 | self._state = 1 |
| 502 | self._cond.notify_all() |
| 503 | except: |
| 504 | #an exception during the _action handler. Break and reraise |
| 505 | self._break() |
| 506 | raise |
| 507 | |
| 508 | # Wait in the barrier until we are relased. Raise an exception |
| 509 | # if the barrier is reset or broken. |
| 510 | def _wait(self, timeout): |
Kristján Valur Jónsson | 6331520 | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 511 | if not self._cond.wait_for(lambda : self._state != 0, timeout): |
| 512 | #timed out. Break the barrier |
| 513 | self._break() |
| 514 | raise BrokenBarrierError |
| 515 | if self._state < 0: |
| 516 | raise BrokenBarrierError |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 517 | assert self._state == 1 |
| 518 | |
| 519 | # If we are the last thread to exit the barrier, signal any threads |
| 520 | # waiting for the barrier to drain. |
| 521 | def _exit(self): |
| 522 | if self._count == 0: |
| 523 | if self._state in (-1, 1): |
| 524 | #resetting or draining |
| 525 | self._state = 0 |
| 526 | self._cond.notify_all() |
| 527 | |
| 528 | def reset(self): |
| 529 | """ |
| 530 | Reset the barrier to the initial state. |
| 531 | Any threads currently waiting will get the BrokenBarrier exception |
| 532 | raised. |
| 533 | """ |
| 534 | with self._cond: |
| 535 | if self._count > 0: |
| 536 | if self._state == 0: |
| 537 | #reset the barrier, waking up threads |
| 538 | self._state = -1 |
| 539 | elif self._state == -2: |
| 540 | #was broken, set it to reset state |
| 541 | #which clears when the last thread exits |
| 542 | self._state = -1 |
| 543 | else: |
| 544 | self._state = 0 |
| 545 | self._cond.notify_all() |
| 546 | |
| 547 | def abort(self): |
| 548 | """ |
| 549 | Place the barrier into a 'broken' state. |
| 550 | Useful in case of error. Any currently waiting threads and |
| 551 | threads attempting to 'wait()' will have BrokenBarrierError |
| 552 | raised. |
| 553 | """ |
| 554 | with self._cond: |
| 555 | self._break() |
| 556 | |
| 557 | def _break(self): |
| 558 | # An internal error was detected. The barrier is set to |
| 559 | # a broken state all parties awakened. |
| 560 | self._state = -2 |
| 561 | self._cond.notify_all() |
| 562 | |
| 563 | @property |
| 564 | def parties(self): |
| 565 | """ |
| 566 | Return the number of threads required to trip the barrier. |
| 567 | """ |
| 568 | return self._parties |
| 569 | |
| 570 | @property |
| 571 | def n_waiting(self): |
| 572 | """ |
| 573 | Return the number of threads that are currently waiting at the barrier. |
| 574 | """ |
| 575 | # We don't need synchronization here since this is an ephemeral result |
| 576 | # anyway. It returns the correct value in the steady state. |
| 577 | if self._state == 0: |
| 578 | return self._count |
| 579 | return 0 |
| 580 | |
| 581 | @property |
| 582 | def broken(self): |
| 583 | """ |
| 584 | Return True if the barrier is in a broken state |
| 585 | """ |
| 586 | return self._state == -2 |
| 587 | |
| 588 | #exception raised by the Barrier class |
| 589 | class BrokenBarrierError(RuntimeError): pass |
| 590 | |
| 591 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 592 | # Helper to generate new thread names |
| 593 | _counter = 0 |
| 594 | def _newname(template="Thread-%d"): |
| 595 | global _counter |
| 596 | _counter = _counter + 1 |
| 597 | return template % _counter |
| 598 | |
| 599 | # Active thread administration |
| 600 | _active_limbo_lock = _allocate_lock() |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 601 | _active = {} # maps thread id to Thread object |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 602 | _limbo = {} |
| 603 | |
| 604 | |
| 605 | # Main class for threads |
| 606 | |
| 607 | class Thread(_Verbose): |
| 608 | |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 609 | __initialized = False |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 610 | # Need to store a reference to sys.exc_info for printing |
| 611 | # out exceptions when a thread tries to use a global var. during interp. |
| 612 | # shutdown and thus raises an exception about trying to perform some |
| 613 | # operation on/with a NoneType |
| 614 | __exc_info = _sys.exc_info |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 615 | # Keep sys.exc_clear too to clear the exception just before |
| 616 | # allowing .join() to return. |
| 617 | #XXX __exc_clear = _sys.exc_clear |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 618 | |
| 619 | def __init__(self, group=None, target=None, name=None, |
Georg Brandl | a4a8b82 | 2005-07-15 09:13:21 +0000 | [diff] [blame] | 620 | args=(), kwargs=None, verbose=None): |
Guido van Rossum | 5a43e1a | 1998-06-09 19:04:26 +0000 | [diff] [blame] | 621 | assert group is None, "group argument must be None for now" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 622 | _Verbose.__init__(self, verbose) |
Georg Brandl | a4a8b82 | 2005-07-15 09:13:21 +0000 | [diff] [blame] | 623 | if kwargs is None: |
| 624 | kwargs = {} |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 625 | self._target = target |
| 626 | self._name = str(name or _newname()) |
| 627 | self._args = args |
| 628 | self._kwargs = kwargs |
| 629 | self._daemonic = self._set_daemon() |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 630 | self._ident = None |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 631 | self._started = Event() |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 632 | self._stopped = False |
| 633 | self._block = Condition(Lock()) |
| 634 | self._initialized = True |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 635 | # sys.stderr is not stored in the class like |
| 636 | # sys.exc_info since it can be changed between instances |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 637 | self._stderr = _sys.stderr |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 638 | |
| 639 | def _set_daemon(self): |
| 640 | # Overridden in _MainThread and _DummyThread |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 641 | return current_thread().daemon |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 642 | |
| 643 | def __repr__(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 644 | assert self._initialized, "Thread.__init__() was not called" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 645 | status = "initial" |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 646 | if self._started.is_set(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 647 | status = "started" |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 648 | if self._stopped: |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 649 | status = "stopped" |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 650 | if self._daemonic: |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 651 | status += " daemon" |
| 652 | if self._ident is not None: |
| 653 | status += " %s" % self._ident |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 654 | return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 655 | |
| 656 | def start(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 657 | if not self._initialized: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 658 | raise RuntimeError("thread.__init__() not called") |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 659 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 660 | if self._started.is_set(): |
Senthil Kumaran | fdd4d0f | 2010-04-06 03:30:18 +0000 | [diff] [blame] | 661 | raise RuntimeError("threads can only be started once") |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 662 | if __debug__: |
| 663 | self._note("%s.start(): starting thread", self) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 664 | with _active_limbo_lock: |
| 665 | _limbo[self] = self |
Gregory P. Smith | 3fdd964 | 2010-02-28 18:57:46 +0000 | [diff] [blame] | 666 | try: |
| 667 | _start_new_thread(self._bootstrap, ()) |
| 668 | except Exception: |
| 669 | with _active_limbo_lock: |
| 670 | del _limbo[self] |
| 671 | raise |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 672 | self._started.wait() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 673 | |
| 674 | def run(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 675 | try: |
| 676 | if self._target: |
| 677 | self._target(*self._args, **self._kwargs) |
| 678 | finally: |
| 679 | # Avoid a refcycle if the thread is running a function with |
| 680 | # an argument that has a member that points to the thread. |
| 681 | del self._target, self._args, self._kwargs |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 682 | |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 683 | def _bootstrap(self): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 684 | # Wrapper around the real bootstrap code that ignores |
| 685 | # exceptions during interpreter cleanup. Those typically |
| 686 | # happen when a daemon thread wakes up at an unfortunate |
| 687 | # moment, finds the world around it destroyed, and raises some |
| 688 | # random exception *** while trying to report the exception in |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 689 | # _bootstrap_inner() below ***. Those random exceptions |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 690 | # don't help anybody, and they confuse users, so we suppress |
| 691 | # them. We suppress them only when it appears that the world |
| 692 | # indeed has already been destroyed, so that exceptions in |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 693 | # _bootstrap_inner() during normal business hours are properly |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 694 | # reported. Also, we only suppress them for daemonic threads; |
| 695 | # if a non-daemonic encounters this, something else is wrong. |
| 696 | try: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 697 | self._bootstrap_inner() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 698 | except: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 699 | if self._daemonic and _sys is None: |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 700 | return |
| 701 | raise |
| 702 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 703 | def _set_ident(self): |
| 704 | self._ident = _get_ident() |
| 705 | |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 706 | def _bootstrap_inner(self): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 707 | try: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 708 | self._set_ident() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 709 | self._started.set() |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 710 | with _active_limbo_lock: |
| 711 | _active[self._ident] = self |
| 712 | del _limbo[self] |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 713 | if __debug__: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 714 | self._note("%s._bootstrap(): thread started", self) |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 715 | |
| 716 | if _trace_hook: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 717 | self._note("%s._bootstrap(): registering trace hook", self) |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 718 | _sys.settrace(_trace_hook) |
| 719 | if _profile_hook: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 720 | self._note("%s._bootstrap(): registering profile hook", self) |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 721 | _sys.setprofile(_profile_hook) |
Tim Peters | d1b108b | 2003-06-29 17:24:17 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 723 | try: |
| 724 | self.run() |
| 725 | except SystemExit: |
| 726 | if __debug__: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 727 | self._note("%s._bootstrap(): raised SystemExit", self) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 728 | except: |
| 729 | if __debug__: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 730 | self._note("%s._bootstrap(): unhandled exception", self) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 731 | # If sys.stderr is no more (most likely from interpreter |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 732 | # shutdown) use self._stderr. Otherwise still use sys (as in |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 733 | # _sys) in case sys.stderr was redefined since the creation of |
| 734 | # self. |
| 735 | if _sys: |
| 736 | _sys.stderr.write("Exception in thread %s:\n%s\n" % |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 737 | (self.name, _format_exc())) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 738 | else: |
| 739 | # Do the best job possible w/o a huge amt. of code to |
| 740 | # approximate a traceback (code ideas from |
| 741 | # Lib/traceback.py) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 742 | exc_type, exc_value, exc_tb = self._exc_info() |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 743 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 744 | print(( |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 745 | "Exception in thread " + self.name + |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 746 | " (most likely raised during interpreter shutdown):"), file=self._stderr) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 747 | print(( |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 748 | "Traceback (most recent call last):"), file=self._stderr) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 749 | while exc_tb: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 750 | print(( |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 751 | ' File "%s", line %s, in %s' % |
| 752 | (exc_tb.tb_frame.f_code.co_filename, |
| 753 | exc_tb.tb_lineno, |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 754 | exc_tb.tb_frame.f_code.co_name)), file=self._stderr) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 755 | exc_tb = exc_tb.tb_next |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 756 | print(("%s: %s" % (exc_type, exc_value)), file=self._stderr) |
Brett Cannon | cc4e935 | 2004-07-03 03:52:35 +0000 | [diff] [blame] | 757 | # Make sure that exc_tb gets deleted since it is a memory |
| 758 | # hog; deleting everything else is just for thoroughness |
| 759 | finally: |
| 760 | del exc_type, exc_value, exc_tb |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 761 | else: |
| 762 | if __debug__: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 763 | self._note("%s._bootstrap(): normal return", self) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 764 | finally: |
| 765 | # Prevent a race in |
| 766 | # test_threading.test_no_refcycle_through_target when |
| 767 | # the exception keeps the target alive past when we |
| 768 | # assert that it's dead. |
| 769 | #XXX self.__exc_clear() |
| 770 | pass |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 771 | finally: |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 772 | with _active_limbo_lock: |
| 773 | self._stop() |
| 774 | try: |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 775 | # We don't call self._delete() because it also |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 776 | # grabs _active_limbo_lock. |
| 777 | del _active[_get_ident()] |
| 778 | except: |
| 779 | pass |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 780 | |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 781 | def _stop(self): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 782 | self._block.acquire() |
| 783 | self._stopped = True |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 784 | self._block.notify_all() |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 785 | self._block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 787 | def _delete(self): |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 788 | "Remove current thread from the dict of currently running threads." |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 789 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 790 | # Notes about running with _dummy_thread: |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 791 | # |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 792 | # Must take care to not raise an exception if _dummy_thread is being |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 793 | # used (and thus this module is being used as an instance of |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 794 | # dummy_threading). _dummy_thread.get_ident() always returns -1 since |
| 795 | # there is only one thread if _dummy_thread is being used. Thus |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 796 | # len(_active) is always <= 1 here, and any Thread instance created |
| 797 | # overwrites the (if any) thread currently registered in _active. |
| 798 | # |
| 799 | # An instance of _MainThread is always created by 'threading'. This |
| 800 | # gets overwritten the instant an instance of Thread is created; both |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 801 | # threads return -1 from _dummy_thread.get_ident() and thus have the |
Tim Peters | 2142993 | 2004-07-21 03:36:52 +0000 | [diff] [blame] | 802 | # same key in the dict. So when the _MainThread instance created by |
| 803 | # 'threading' tries to clean itself up when atexit calls this method |
| 804 | # it gets a KeyError if another Thread instance was created. |
| 805 | # |
| 806 | # This all means that KeyError from trying to delete something from |
| 807 | # _active if dummy_threading is being used is a red herring. But |
| 808 | # since it isn't if dummy_threading is *not* being used then don't |
| 809 | # hide the exception. |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 810 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 811 | try: |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 812 | with _active_limbo_lock: |
Brett Cannon | 8b3d92a | 2004-07-21 02:21:58 +0000 | [diff] [blame] | 813 | del _active[_get_ident()] |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 814 | # There must not be any python code between the previous line |
| 815 | # and after the lock is released. Otherwise a tracing function |
| 816 | # could try to acquire the lock again in the same thread, (in |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 817 | # current_thread()), and would block. |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 818 | except KeyError: |
| 819 | if 'dummy_threading' not in _sys.modules: |
| 820 | raise |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 821 | |
| 822 | def join(self, timeout=None): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 823 | if not self._initialized: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 824 | raise RuntimeError("Thread.__init__() not called") |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 825 | if not self._started.is_set(): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 826 | raise RuntimeError("cannot join thread before it is started") |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 827 | if self is current_thread(): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 828 | raise RuntimeError("cannot join current thread") |
| 829 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 830 | if __debug__: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 831 | if not self._stopped: |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 832 | self._note("%s.join(): waiting until thread stops", self) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 833 | |
| 834 | self._block.acquire() |
| 835 | try: |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 836 | if timeout is None: |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 837 | while not self._stopped: |
| 838 | self._block.wait() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 839 | if __debug__: |
| 840 | self._note("%s.join(): thread stopped", self) |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 841 | else: |
| 842 | deadline = _time() + timeout |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 843 | while not self._stopped: |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 844 | delay = deadline - _time() |
| 845 | if delay <= 0: |
| 846 | if __debug__: |
| 847 | self._note("%s.join(): timed out", self) |
| 848 | break |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 849 | self._block.wait(delay) |
Brett Cannon | ad07ff2 | 2005-11-23 02:15:50 +0000 | [diff] [blame] | 850 | else: |
| 851 | if __debug__: |
| 852 | self._note("%s.join(): thread stopped", self) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 853 | finally: |
| 854 | self._block.release() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 855 | |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 856 | @property |
| 857 | def name(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 858 | assert self._initialized, "Thread.__init__() not called" |
| 859 | return self._name |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 860 | |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 861 | @name.setter |
| 862 | def name(self, name): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 863 | assert self._initialized, "Thread.__init__() not called" |
| 864 | self._name = str(name) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 865 | |
Benjamin Peterson | 773c17b | 2008-08-18 16:45:31 +0000 | [diff] [blame] | 866 | @property |
| 867 | def ident(self): |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 868 | assert self._initialized, "Thread.__init__() not called" |
| 869 | return self._ident |
| 870 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 871 | def is_alive(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 872 | assert self._initialized, "Thread.__init__() not called" |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 873 | return self._started.is_set() and not self._stopped |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 874 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 875 | isAlive = is_alive |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 876 | |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 877 | @property |
| 878 | def daemon(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 879 | assert self._initialized, "Thread.__init__() not called" |
| 880 | return self._daemonic |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 881 | |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 882 | @daemon.setter |
| 883 | def daemon(self, daemonic): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 884 | if not self._initialized: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 885 | raise RuntimeError("Thread.__init__() not called") |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 886 | if self._started.is_set(): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 887 | raise RuntimeError("cannot set daemon status of active thread"); |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 888 | self._daemonic = daemonic |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 889 | |
Benjamin Peterson | 6640d72 | 2008-08-18 18:16:46 +0000 | [diff] [blame] | 890 | def isDaemon(self): |
| 891 | return self.daemon |
| 892 | |
| 893 | def setDaemon(self, daemonic): |
| 894 | self.daemon = daemonic |
| 895 | |
| 896 | def getName(self): |
| 897 | return self.name |
| 898 | |
| 899 | def setName(self, name): |
| 900 | self.name = name |
| 901 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 902 | # The timer class was contributed by Itamar Shtull-Trauring |
| 903 | |
| 904 | def Timer(*args, **kwargs): |
| 905 | return _Timer(*args, **kwargs) |
| 906 | |
| 907 | class _Timer(Thread): |
| 908 | """Call a function after a specified number of seconds: |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 909 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 910 | t = Timer(30.0, f, args=[], kwargs={}) |
| 911 | t.start() |
| 912 | t.cancel() # stop the timer's action if it's still waiting |
| 913 | """ |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 914 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 915 | def __init__(self, interval, function, args=[], kwargs={}): |
| 916 | Thread.__init__(self) |
| 917 | self.interval = interval |
| 918 | self.function = function |
| 919 | self.args = args |
| 920 | self.kwargs = kwargs |
| 921 | self.finished = Event() |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 922 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 923 | def cancel(self): |
| 924 | """Stop the timer if it hasn't finished yet""" |
| 925 | self.finished.set() |
Tim Peters | b64bec3 | 2001-09-18 02:26:39 +0000 | [diff] [blame] | 926 | |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 927 | def run(self): |
| 928 | self.finished.wait(self.interval) |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 929 | if not self.finished.is_set(): |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 930 | self.function(*self.args, **self.kwargs) |
| 931 | self.finished.set() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 932 | |
| 933 | # Special thread class to represent the main thread |
| 934 | # This is garbage collected through an exit handler |
| 935 | |
| 936 | class _MainThread(Thread): |
| 937 | |
| 938 | def __init__(self): |
| 939 | Thread.__init__(self, name="MainThread") |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 940 | self._started.set() |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 941 | self._set_ident() |
| 942 | with _active_limbo_lock: |
| 943 | _active[self._ident] = self |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 944 | |
| 945 | def _set_daemon(self): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 946 | return False |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 947 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 948 | def _exitfunc(self): |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 949 | self._stop() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 950 | t = _pickSomeNonDaemonThread() |
| 951 | if t: |
| 952 | if __debug__: |
| 953 | self._note("%s: waiting for other threads", self) |
| 954 | while t: |
| 955 | t.join() |
| 956 | t = _pickSomeNonDaemonThread() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 957 | if __debug__: |
| 958 | self._note("%s: exiting", self) |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 959 | self._delete() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 960 | |
| 961 | def _pickSomeNonDaemonThread(): |
| 962 | for t in enumerate(): |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 963 | if not t.daemon and t.is_alive(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 964 | return t |
| 965 | return None |
| 966 | |
| 967 | |
| 968 | # Dummy thread class to represent threads not started here. |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 969 | # These aren't garbage collected when they die, nor can they be waited for. |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 970 | # If they invoke anything in threading.py that calls current_thread(), they |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 971 | # leave an entry in the _active dict forever after. |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 972 | # Their purpose is to return *something* from current_thread(). |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 973 | # They are marked as daemon threads so we won't wait for them |
| 974 | # when we exit (conform previous semantics). |
| 975 | |
| 976 | class _DummyThread(Thread): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 977 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 978 | def __init__(self): |
| 979 | Thread.__init__(self, name=_newname("Dummy-%d")) |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 980 | |
| 981 | # Thread.__block consumes an OS-level locking primitive, which |
| 982 | # can never be used by a _DummyThread. Since a _DummyThread |
| 983 | # instance is immortal, that's bad, so release this resource. |
Guido van Rossum | d064899 | 2007-08-20 19:25:41 +0000 | [diff] [blame] | 984 | del self._block |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 985 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 986 | |
| 987 | self._started.set() |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 988 | self._set_ident() |
| 989 | with _active_limbo_lock: |
| 990 | _active[self._ident] = self |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 991 | |
| 992 | def _set_daemon(self): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 993 | return True |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 994 | |
Neal Norwitz | 45bec8c | 2002-02-19 03:01:36 +0000 | [diff] [blame] | 995 | def join(self, timeout=None): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 996 | assert False, "cannot join a dummy thread" |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 997 | |
| 998 | |
| 999 | # Global API functions |
| 1000 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 1001 | def current_thread(): |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1002 | try: |
| 1003 | return _active[_get_ident()] |
| 1004 | except KeyError: |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 1005 | ##print "current_thread(): no current thread for", _get_ident() |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1006 | return _DummyThread() |
| 1007 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 1008 | currentThread = current_thread |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 1009 | |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 1010 | def active_count(): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1011 | with _active_limbo_lock: |
| 1012 | return len(_active) + len(_limbo) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1013 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 1014 | activeCount = active_count |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | bdec11f | 2009-11-05 13:49:14 +0000 | [diff] [blame] | 1016 | def _enumerate(): |
| 1017 | # Same as enumerate(), but without the lock. Internal use only. |
| 1018 | return list(_active.values()) + list(_limbo.values()) |
| 1019 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1020 | def enumerate(): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1021 | with _active_limbo_lock: |
| 1022 | return list(_active.values()) + list(_limbo.values()) |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1023 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 1024 | from _thread import stack_size |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1025 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1026 | # Create the main thread object, |
| 1027 | # and make it available for the interpreter |
| 1028 | # (Py_Main) as threading._shutdown. |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1029 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1030 | _shutdown = _MainThread()._exitfunc |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1031 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 1032 | # get thread-local implementation, either from the thread |
| 1033 | # module, or from the python fallback |
| 1034 | |
| 1035 | try: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 1036 | from _thread import _local as local |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 1037 | except ImportError: |
| 1038 | from _threading_local import local |
| 1039 | |
Guido van Rossum | 7f5013a | 1998-04-09 22:01:42 +0000 | [diff] [blame] | 1040 | |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 1041 | def _after_fork(): |
| 1042 | # This function is called by Python/ceval.c:PyEval_ReInitThreads which |
| 1043 | # is called from PyOS_AfterFork. Here we cleanup threading module state |
| 1044 | # that should not exist after a fork. |
| 1045 | |
| 1046 | # Reset _active_limbo_lock, in case we forked while the lock was held |
| 1047 | # by another (non-forked) thread. http://bugs.python.org/issue874900 |
| 1048 | global _active_limbo_lock |
| 1049 | _active_limbo_lock = _allocate_lock() |
| 1050 | |
| 1051 | # fork() only copied the current thread; clear references to others. |
| 1052 | new_active = {} |
| 1053 | current = current_thread() |
| 1054 | with _active_limbo_lock: |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 1055 | for thread in _active.values(): |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 1056 | if thread is current: |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 1057 | # There is only one active thread. We reset the ident to |
| 1058 | # its new value since it can have changed. |
| 1059 | ident = _get_ident() |
| 1060 | thread._ident = ident |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 1061 | new_active[ident] = thread |
| 1062 | else: |
| 1063 | # All the others are already stopped. |
| 1064 | # We don't call _Thread__stop() because it tries to acquire |
| 1065 | # thread._Thread__block which could also have been held while |
| 1066 | # we forked. |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 1067 | thread._stopped = True |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 1068 | |
| 1069 | _limbo.clear() |
| 1070 | _active.clear() |
| 1071 | _active.update(new_active) |
| 1072 | assert len(_active) == 1 |