blob: 4bb0182e66f14730caa666825b33056c01b27edb [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
Fred Drakea8725952002-12-30 23:32:50 +00003import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00004import _thread
Fred Drakea8725952002-12-30 23:32:50 +00005
Fred Drakea8725952002-12-30 23:32:50 +00006from time import time as _time, sleep as _sleep
Neil Schemenauerf607fc52003-11-05 23:03:00 +00007from traceback import format_exc as _format_exc
Raymond Hettinger756b3f32004-01-29 06:37:52 +00008from collections import deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +00009
Benjamin Petersonb3085c92008-09-01 23:09:31 +000010# 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 Rossum7f5013a1998-04-09 22:01:42 +000021# Rename some stuff so "from threading import *" is safe
Benjamin Peterson672b8032008-06-11 19:14:14 +000022__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Tim Peters685e6972003-06-29 16:50:06 +000023 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000025
Georg Brandl2067bfd2008-05-25 13:05:15 +000026_start_new_thread = _thread.start_new_thread
27_allocate_lock = _thread.allocate_lock
28_get_ident = _thread.get_ident
29ThreadError = _thread.error
30del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000031
Guido van Rossum7f5013a1998-04-09 22:01:42 +000032
Tim Peters59aba122003-07-01 20:01:55 +000033# Debug support (adapted from ihooks.py).
34# All the major classes here derive from _Verbose. We force that to
35# be a new-style class so that all the major classes here are new-style.
36# This helps debugging (type(instance) is more revealing for instances
37# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000038
Tim Peters0939fac2003-07-01 19:28:44 +000039_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
41if __debug__:
42
Tim Peters59aba122003-07-01 20:01:55 +000043 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
45 def __init__(self, verbose=None):
46 if verbose is None:
47 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000048 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000049
50 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000051 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052 format = format % args
53 format = "%s: %s\n" % (
Benjamin Petersonfdbea962008-08-18 17:33:47 +000054 current_thread().name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000055 _sys.stderr.write(format)
56
57else:
58 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000059 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000060 def __init__(self, verbose=None):
61 pass
62 def _note(self, *args):
63 pass
64
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000065# Support for profile and trace hooks
66
67_profile_hook = None
68_trace_hook = None
69
70def setprofile(func):
71 global _profile_hook
72 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000073
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000074def settrace(func):
75 global _trace_hook
76 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000077
78# Synchronization classes
79
80Lock = _allocate_lock
81
82def RLock(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +000083 return _RLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000084
85class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000086
Guido van Rossum7f5013a1998-04-09 22:01:42 +000087 def __init__(self, verbose=None):
88 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +000089 self._block = _allocate_lock()
90 self._owner = None
91 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000092
93 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000094 owner = self._owner
Antoine Pitrou959f3e52009-11-09 16:52:46 +000095 try:
96 owner = _active[owner].name
97 except KeyError:
98 pass
99 return "<%s owner=%r count=%d>" % (
100 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000101
Georg Brandlb044b2a2009-09-16 16:05:59 +0000102 def acquire(self, blocking=True):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000103 me = _get_ident()
104 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000105 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000106 if __debug__:
107 self._note("%s.acquire(%s): recursive success", self, blocking)
108 return 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000109 rc = self._block.acquire(blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000110 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000111 self._owner = me
112 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000113 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000114 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000115 else:
116 if __debug__:
117 self._note("%s.acquire(%s): failure", self, blocking)
118 return rc
119
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000120 __enter__ = acquire
121
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000122 def release(self):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000123 if self._owner != _get_ident():
Georg Brandl628e6f92009-10-27 20:24:45 +0000124 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000125 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000126 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000127 self._owner = None
128 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000129 if __debug__:
130 self._note("%s.release(): final release", self)
131 else:
132 if __debug__:
133 self._note("%s.release(): non-final release", self)
134
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000135 def __exit__(self, t, v, tb):
136 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000137
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000138 # Internal methods used by condition variables
139
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000140 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000141 self._block.acquire()
142 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000143 if __debug__:
144 self._note("%s._acquire_restore()", self)
145
146 def _release_save(self):
147 if __debug__:
148 self._note("%s._release_save()", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000149 count = self._count
150 self._count = 0
151 owner = self._owner
152 self._owner = None
153 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000154 return (count, owner)
155
156 def _is_owned(self):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000157 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000158
159
160def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000161 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162
163class _Condition(_Verbose):
164
165 def __init__(self, lock=None, verbose=None):
166 _Verbose.__init__(self, verbose)
167 if lock is None:
168 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000169 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000170 # Export the lock's acquire() and release() methods
171 self.acquire = lock.acquire
172 self.release = lock.release
173 # If the lock defines _release_save() and/or _acquire_restore(),
174 # these override the default implementations (which just call
175 # release() and acquire() on the lock). Ditto for _is_owned().
176 try:
177 self._release_save = lock._release_save
178 except AttributeError:
179 pass
180 try:
181 self._acquire_restore = lock._acquire_restore
182 except AttributeError:
183 pass
184 try:
185 self._is_owned = lock._is_owned
186 except AttributeError:
187 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000188 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000189
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000191 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000192
Thomas Wouters477c8d52006-05-27 19:21:47 +0000193 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000194 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000195
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000196 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000197 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000198
199 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000200 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000201
202 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000203 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
205 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000206 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000207 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000208 if self._lock.acquire(0):
209 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000210 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000211 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000212 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000213
214 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000215 if not self._is_owned():
Georg Brandl628e6f92009-10-27 20:24:45 +0000216 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217 waiter = _allocate_lock()
218 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000221 try: # restore state no matter what (e.g., KeyboardInterrupt)
222 if timeout is None:
223 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000224 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000225 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000226 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000227 # Balancing act: We can't afford a pure busy loop, so we
228 # have to sleep; but if we sleep the whole timeout time,
229 # we'll be unresponsive. The scheme here sleeps very
230 # little at first, longer as time goes on, but never longer
231 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000232 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000233 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000234 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000235 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000236 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000237 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000238 remaining = endtime - _time()
239 if remaining <= 0:
240 break
241 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000242 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000243 if not gotit:
244 if __debug__:
245 self._note("%s.wait(%s): timed out", self, timeout)
246 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000248 except ValueError:
249 pass
250 else:
251 if __debug__:
252 self._note("%s.wait(%s): got it", self, timeout)
253 finally:
254 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000255
256 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000257 if not self._is_owned():
Georg Brandl628e6f92009-10-27 20:24:45 +0000258 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000259 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000260 waiters = __waiters[:n]
261 if not waiters:
262 if __debug__:
263 self._note("%s.notify(): no waiters", self)
264 return
265 self._note("%s.notify(): notifying %d waiter%s", self, n,
266 n!=1 and "s" or "")
267 for waiter in waiters:
268 waiter.release()
269 try:
270 __waiters.remove(waiter)
271 except ValueError:
272 pass
273
Benjamin Peterson672b8032008-06-11 19:14:14 +0000274 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000275 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000277 notifyAll = notify_all
278
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000279
280def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000281 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000282
283class _Semaphore(_Verbose):
284
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000285 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286
287 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000288 if value < 0:
289 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000290 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000291 self._cond = Condition(Lock())
292 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000293
Georg Brandlb044b2a2009-09-16 16:05:59 +0000294 def acquire(self, blocking=True):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000295 rc = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000296 self._cond.acquire()
297 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000298 if not blocking:
299 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000300 if __debug__:
301 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000302 self, blocking, self._value)
303 self._cond.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000304 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000305 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000306 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000307 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000308 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000309 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000310 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000311 return rc
312
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000313 __enter__ = acquire
314
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000316 self._cond.acquire()
317 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000318 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000319 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000320 self, self._value)
321 self._cond.notify()
322 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000323
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000324 def __exit__(self, t, v, tb):
325 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000326
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000327
Skip Montanaroe428bb72001-08-20 20:27:58 +0000328def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000329 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000330
331class _BoundedSemaphore(_Semaphore):
332 """Semaphore that checks that # releases is <= # acquires"""
333 def __init__(self, value=1, verbose=None):
334 _Semaphore.__init__(self, value, verbose)
335 self._initial_value = value
336
337 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000338 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000339 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000340 return _Semaphore.release(self)
341
342
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000343def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000344 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000345
346class _Event(_Verbose):
347
348 # After Tim Peters' event class (without is_posted())
349
350 def __init__(self, verbose=None):
351 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000352 self._cond = Condition(Lock())
353 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000354
Benjamin Peterson672b8032008-06-11 19:14:14 +0000355 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000356 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000357
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000358 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000359
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000360 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000361 self._cond.acquire()
362 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000363 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000364 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000365 finally:
366 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000367
368 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000369 self._cond.acquire()
370 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000371 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000372 finally:
373 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000374
375 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000376 self._cond.acquire()
377 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000378 if not self._flag:
379 self._cond.wait(timeout)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000380 return self._flag
Christian Heimes969fe572008-01-25 11:23:10 +0000381 finally:
382 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000383
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000384# Helper to generate new thread names
385_counter = 0
386def _newname(template="Thread-%d"):
387 global _counter
388 _counter = _counter + 1
389 return template % _counter
390
391# Active thread administration
392_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000393_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000394_limbo = {}
395
396
397# Main class for threads
398
399class Thread(_Verbose):
400
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000401 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000402 # Need to store a reference to sys.exc_info for printing
403 # out exceptions when a thread tries to use a global var. during interp.
404 # shutdown and thus raises an exception about trying to perform some
405 # operation on/with a NoneType
406 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000407 # Keep sys.exc_clear too to clear the exception just before
408 # allowing .join() to return.
409 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000410
411 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000412 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000413 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000414 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000415 if kwargs is None:
416 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000417 self._target = target
418 self._name = str(name or _newname())
419 self._args = args
420 self._kwargs = kwargs
421 self._daemonic = self._set_daemon()
Georg Brandl0c77a822008-06-10 16:37:50 +0000422 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000423 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000424 self._stopped = False
425 self._block = Condition(Lock())
426 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000427 # sys.stderr is not stored in the class like
428 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000429 self._stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000430
431 def _set_daemon(self):
432 # Overridden in _MainThread and _DummyThread
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000433 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000434
435 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000436 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000437 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000438 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000439 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000440 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000441 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000442 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000443 status += " daemon"
444 if self._ident is not None:
445 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000446 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000447
448 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000449 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000450 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000451
Benjamin Peterson672b8032008-06-11 19:14:14 +0000452 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000453 raise RuntimeError("thread already started")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000454 if __debug__:
455 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000456 with _active_limbo_lock:
457 _limbo[self] = self
Guido van Rossumd0648992007-08-20 19:25:41 +0000458 _start_new_thread(self._bootstrap, ())
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000459 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000460
461 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000462 try:
463 if self._target:
464 self._target(*self._args, **self._kwargs)
465 finally:
466 # Avoid a refcycle if the thread is running a function with
467 # an argument that has a member that points to the thread.
468 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000469
Guido van Rossumd0648992007-08-20 19:25:41 +0000470 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000471 # Wrapper around the real bootstrap code that ignores
472 # exceptions during interpreter cleanup. Those typically
473 # happen when a daemon thread wakes up at an unfortunate
474 # moment, finds the world around it destroyed, and raises some
475 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000476 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000477 # don't help anybody, and they confuse users, so we suppress
478 # them. We suppress them only when it appears that the world
479 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000480 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000481 # reported. Also, we only suppress them for daemonic threads;
482 # if a non-daemonic encounters this, something else is wrong.
483 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000484 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000485 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000486 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000487 return
488 raise
489
Benjamin Petersond23f8222009-04-05 19:13:16 +0000490 def _set_ident(self):
491 self._ident = _get_ident()
492
Guido van Rossumd0648992007-08-20 19:25:41 +0000493 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000494 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000495 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000496 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000497 with _active_limbo_lock:
498 _active[self._ident] = self
499 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000500 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000501 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000502
503 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000504 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000505 _sys.settrace(_trace_hook)
506 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000507 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000508 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000509
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510 try:
511 self.run()
512 except SystemExit:
513 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000514 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000515 except:
516 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000517 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000518 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000519 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000520 # _sys) in case sys.stderr was redefined since the creation of
521 # self.
522 if _sys:
523 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000524 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000525 else:
526 # Do the best job possible w/o a huge amt. of code to
527 # approximate a traceback (code ideas from
528 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000529 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000530 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000531 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000532 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000533 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000534 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000535 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000536 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000537 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000538 ' File "%s", line %s, in %s' %
539 (exc_tb.tb_frame.f_code.co_filename,
540 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000541 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000542 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000543 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000544 # Make sure that exc_tb gets deleted since it is a memory
545 # hog; deleting everything else is just for thoroughness
546 finally:
547 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000548 else:
549 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000550 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000551 finally:
552 # Prevent a race in
553 # test_threading.test_no_refcycle_through_target when
554 # the exception keeps the target alive past when we
555 # assert that it's dead.
556 #XXX self.__exc_clear()
557 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000558 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000559 with _active_limbo_lock:
560 self._stop()
561 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000562 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000563 # grabs _active_limbo_lock.
564 del _active[_get_ident()]
565 except:
566 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000567
Guido van Rossumd0648992007-08-20 19:25:41 +0000568 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000569 self._block.acquire()
570 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000571 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000572 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000573
Guido van Rossumd0648992007-08-20 19:25:41 +0000574 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000575 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000576
Georg Brandl2067bfd2008-05-25 13:05:15 +0000577 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000578 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000579 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000580 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000581 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
582 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000583 # len(_active) is always <= 1 here, and any Thread instance created
584 # overwrites the (if any) thread currently registered in _active.
585 #
586 # An instance of _MainThread is always created by 'threading'. This
587 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000588 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000589 # same key in the dict. So when the _MainThread instance created by
590 # 'threading' tries to clean itself up when atexit calls this method
591 # it gets a KeyError if another Thread instance was created.
592 #
593 # This all means that KeyError from trying to delete something from
594 # _active if dummy_threading is being used is a red herring. But
595 # since it isn't if dummy_threading is *not* being used then don't
596 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000597
Christian Heimes969fe572008-01-25 11:23:10 +0000598 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000599 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000600 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000601 # There must not be any python code between the previous line
602 # and after the lock is released. Otherwise a tracing function
603 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000604 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000605 except KeyError:
606 if 'dummy_threading' not in _sys.modules:
607 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000608
609 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000610 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000611 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000612 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000613 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000614 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000615 raise RuntimeError("cannot join current thread")
616
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000617 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000618 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000619 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000620
621 self._block.acquire()
622 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000623 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000624 while not self._stopped:
625 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000626 if __debug__:
627 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000628 else:
629 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000630 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000631 delay = deadline - _time()
632 if delay <= 0:
633 if __debug__:
634 self._note("%s.join(): timed out", self)
635 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000636 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000637 else:
638 if __debug__:
639 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000640 finally:
641 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000642
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000643 @property
644 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000645 assert self._initialized, "Thread.__init__() not called"
646 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000647
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000648 @name.setter
649 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000650 assert self._initialized, "Thread.__init__() not called"
651 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000652
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000653 @property
654 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000655 assert self._initialized, "Thread.__init__() not called"
656 return self._ident
657
Benjamin Peterson672b8032008-06-11 19:14:14 +0000658 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000659 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000660 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000661
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000662 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000663
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000664 @property
665 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000666 assert self._initialized, "Thread.__init__() not called"
667 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000668
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000669 @daemon.setter
670 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000671 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000672 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000673 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000674 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000675 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000676
Benjamin Peterson6640d722008-08-18 18:16:46 +0000677 def isDaemon(self):
678 return self.daemon
679
680 def setDaemon(self, daemonic):
681 self.daemon = daemonic
682
683 def getName(self):
684 return self.name
685
686 def setName(self, name):
687 self.name = name
688
Martin v. Löwis44f86962001-09-05 13:44:54 +0000689# The timer class was contributed by Itamar Shtull-Trauring
690
691def Timer(*args, **kwargs):
692 return _Timer(*args, **kwargs)
693
694class _Timer(Thread):
695 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000696
Martin v. Löwis44f86962001-09-05 13:44:54 +0000697 t = Timer(30.0, f, args=[], kwargs={})
698 t.start()
699 t.cancel() # stop the timer's action if it's still waiting
700 """
Tim Petersb64bec32001-09-18 02:26:39 +0000701
Martin v. Löwis44f86962001-09-05 13:44:54 +0000702 def __init__(self, interval, function, args=[], kwargs={}):
703 Thread.__init__(self)
704 self.interval = interval
705 self.function = function
706 self.args = args
707 self.kwargs = kwargs
708 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000709
Martin v. Löwis44f86962001-09-05 13:44:54 +0000710 def cancel(self):
711 """Stop the timer if it hasn't finished yet"""
712 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000713
Martin v. Löwis44f86962001-09-05 13:44:54 +0000714 def run(self):
715 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000716 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000717 self.function(*self.args, **self.kwargs)
718 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000719
720# Special thread class to represent the main thread
721# This is garbage collected through an exit handler
722
723class _MainThread(Thread):
724
725 def __init__(self):
726 Thread.__init__(self, name="MainThread")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000727 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000728 self._set_ident()
729 with _active_limbo_lock:
730 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731
732 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000733 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000734
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000735 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000736 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000737 t = _pickSomeNonDaemonThread()
738 if t:
739 if __debug__:
740 self._note("%s: waiting for other threads", self)
741 while t:
742 t.join()
743 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000744 if __debug__:
745 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000746 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000747
748def _pickSomeNonDaemonThread():
749 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000750 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000751 return t
752 return None
753
754
755# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000756# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000757# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000758# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000759# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000760# They are marked as daemon threads so we won't wait for them
761# when we exit (conform previous semantics).
762
763class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000764
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000765 def __init__(self):
766 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000767
768 # Thread.__block consumes an OS-level locking primitive, which
769 # can never be used by a _DummyThread. Since a _DummyThread
770 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000771 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000772
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000773
774 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000775 self._set_ident()
776 with _active_limbo_lock:
777 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000778
779 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000780 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000781
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000782 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000783 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000784
785
786# Global API functions
787
Benjamin Peterson672b8032008-06-11 19:14:14 +0000788def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000789 try:
790 return _active[_get_ident()]
791 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000792 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000793 return _DummyThread()
794
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000795currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000796
Benjamin Peterson672b8032008-06-11 19:14:14 +0000797def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000798 with _active_limbo_lock:
799 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000800
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000801activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000802
Antoine Pitroua9546072009-11-05 13:51:19 +0000803def _enumerate():
804 # Same as enumerate(), but without the lock. Internal use only.
805 return list(_active.values()) + list(_limbo.values())
806
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000807def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000808 with _active_limbo_lock:
809 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000810
Georg Brandl2067bfd2008-05-25 13:05:15 +0000811from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000812
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000813# Create the main thread object,
814# and make it available for the interpreter
815# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000816
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000817_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000818
Jim Fultond15dc062004-07-14 19:11:50 +0000819# get thread-local implementation, either from the thread
820# module, or from the python fallback
821
822try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000823 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000824except ImportError:
825 from _threading_local import local
826
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000827
Jesse Nollera8513972008-07-17 16:49:17 +0000828def _after_fork():
829 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
830 # is called from PyOS_AfterFork. Here we cleanup threading module state
831 # that should not exist after a fork.
832
833 # Reset _active_limbo_lock, in case we forked while the lock was held
834 # by another (non-forked) thread. http://bugs.python.org/issue874900
835 global _active_limbo_lock
836 _active_limbo_lock = _allocate_lock()
837
838 # fork() only copied the current thread; clear references to others.
839 new_active = {}
840 current = current_thread()
841 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000842 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +0000843 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000844 # There is only one active thread. We reset the ident to
845 # its new value since it can have changed.
846 ident = _get_ident()
847 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000848 new_active[ident] = thread
849 else:
850 # All the others are already stopped.
851 # We don't call _Thread__stop() because it tries to acquire
852 # thread._Thread__block which could also have been held while
853 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000854 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +0000855
856 _limbo.clear()
857 _active.clear()
858 _active.update(new_active)
859 assert len(_active) == 1
860
861
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000862# Self-test code
863
864def _test():
865
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000866 class BoundedQueue(_Verbose):
867
868 def __init__(self, limit):
869 _Verbose.__init__(self)
870 self.mon = RLock()
871 self.rc = Condition(self.mon)
872 self.wc = Condition(self.mon)
873 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000874 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000875
876 def put(self, item):
877 self.mon.acquire()
878 while len(self.queue) >= self.limit:
879 self._note("put(%s): queue full", item)
880 self.wc.wait()
881 self.queue.append(item)
882 self._note("put(%s): appended, length now %d",
883 item, len(self.queue))
884 self.rc.notify()
885 self.mon.release()
886
887 def get(self):
888 self.mon.acquire()
889 while not self.queue:
890 self._note("get(): queue empty")
891 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000892 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000893 self._note("get(): got %s, %d left", item, len(self.queue))
894 self.wc.notify()
895 self.mon.release()
896 return item
897
898 class ProducerThread(Thread):
899
900 def __init__(self, queue, quota):
901 Thread.__init__(self, name="Producer")
902 self.queue = queue
903 self.quota = quota
904
905 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000906 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000907 counter = 0
908 while counter < self.quota:
909 counter = counter + 1
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000910 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000911 _sleep(random() * 0.00001)
912
913
914 class ConsumerThread(Thread):
915
916 def __init__(self, queue, count):
917 Thread.__init__(self, name="Consumer")
918 self.queue = queue
919 self.count = count
920
921 def run(self):
922 while self.count > 0:
923 item = self.queue.get()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000924 print(item)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000925 self.count = self.count - 1
926
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000927 NP = 3
928 QL = 4
929 NI = 5
930
931 Q = BoundedQueue(QL)
932 P = []
933 for i in range(NP):
934 t = ProducerThread(Q, NI)
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000935 t.name = "Producer-%d" % (i+1)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000936 P.append(t)
937 C = ConsumerThread(Q, NI*NP)
938 for t in P:
939 t.start()
940 _sleep(0.000001)
941 C.start()
942 for t in P:
943 t.join()
944 C.join()
945
946if __name__ == '__main__':
947 _test()