blob: 7e513b8efe7eadda4520cc79f824d2c5d10418a6 [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
Antoine Pitrou434736a2009-11-10 18:46:01 +000030try:
31 _CRLock = _thread.RLock
32except AttributeError:
33 _CRLock = None
Georg Brandl2067bfd2008-05-25 13:05:15 +000034del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000035
Guido van Rossum7f5013a1998-04-09 22:01:42 +000036
Tim Peters59aba122003-07-01 20:01:55 +000037# Debug support (adapted from ihooks.py).
38# All the major classes here derive from _Verbose. We force that to
39# be a new-style class so that all the major classes here are new-style.
40# This helps debugging (type(instance) is more revealing for instances
41# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000042
Tim Peters0939fac2003-07-01 19:28:44 +000043_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
45if __debug__:
46
Tim Peters59aba122003-07-01 20:01:55 +000047 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000048
49 def __init__(self, verbose=None):
50 if verbose is None:
51 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000052 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
54 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000055 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000056 format = format % args
57 format = "%s: %s\n" % (
Benjamin Petersonfdbea962008-08-18 17:33:47 +000058 current_thread().name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000059 _sys.stderr.write(format)
60
61else:
62 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000063 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000064 def __init__(self, verbose=None):
65 pass
66 def _note(self, *args):
67 pass
68
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000069# Support for profile and trace hooks
70
71_profile_hook = None
72_trace_hook = None
73
74def setprofile(func):
75 global _profile_hook
76 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000077
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000078def settrace(func):
79 global _trace_hook
80 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000081
82# Synchronization classes
83
84Lock = _allocate_lock
85
Antoine Pitrou434736a2009-11-10 18:46:01 +000086def RLock(verbose=None, *args, **kwargs):
87 if verbose is None:
88 verbose = _VERBOSE
89 if (__debug__ and verbose) or _CRLock is None:
90 return _PyRLock(verbose, *args, **kwargs)
91 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000092
93class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000094
Guido van Rossum7f5013a1998-04-09 22:01:42 +000095 def __init__(self, verbose=None):
96 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +000097 self._block = _allocate_lock()
98 self._owner = None
99 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000100
101 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000102 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000103 try:
104 owner = _active[owner].name
105 except KeyError:
106 pass
107 return "<%s owner=%r count=%d>" % (
108 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000109
Georg Brandl7b1c4142009-09-16 14:23:20 +0000110 def acquire(self, blocking=True):
Antoine Pitroub0872682009-11-09 16:08:16 +0000111 me = _get_ident()
112 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000113 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000114 if __debug__:
115 self._note("%s.acquire(%s): recursive success", self, blocking)
116 return 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000117 rc = self._block.acquire(blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000118 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000119 self._owner = me
120 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000122 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000123 else:
124 if __debug__:
125 self._note("%s.acquire(%s): failure", self, blocking)
126 return rc
127
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000128 __enter__ = acquire
129
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000130 def release(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000131 if self._owner != _get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000132 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000133 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000134 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000135 self._owner = None
136 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000137 if __debug__:
138 self._note("%s.release(): final release", self)
139 else:
140 if __debug__:
141 self._note("%s.release(): non-final release", self)
142
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000143 def __exit__(self, t, v, tb):
144 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000145
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000146 # Internal methods used by condition variables
147
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000148 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000149 self._block.acquire()
150 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000151 if __debug__:
152 self._note("%s._acquire_restore()", self)
153
154 def _release_save(self):
155 if __debug__:
156 self._note("%s._release_save()", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000157 count = self._count
158 self._count = 0
159 owner = self._owner
160 self._owner = None
161 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162 return (count, owner)
163
164 def _is_owned(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000165 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000166
Antoine Pitrou434736a2009-11-10 18:46:01 +0000167_PyRLock = _RLock
168
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000169
170def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000171 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172
173class _Condition(_Verbose):
174
175 def __init__(self, lock=None, verbose=None):
176 _Verbose.__init__(self, verbose)
177 if lock is None:
178 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000179 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000180 # Export the lock's acquire() and release() methods
181 self.acquire = lock.acquire
182 self.release = lock.release
183 # If the lock defines _release_save() and/or _acquire_restore(),
184 # these override the default implementations (which just call
185 # release() and acquire() on the lock). Ditto for _is_owned().
186 try:
187 self._release_save = lock._release_save
188 except AttributeError:
189 pass
190 try:
191 self._acquire_restore = lock._acquire_restore
192 except AttributeError:
193 pass
194 try:
195 self._is_owned = lock._is_owned
196 except AttributeError:
197 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000198 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000199
Thomas Wouters477c8d52006-05-27 19:21:47 +0000200 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000201 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000202
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000204 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000205
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000206 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000207 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000208
209 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000210 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000211
212 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000213 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000214
215 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000216 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000217 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000218 if self._lock.acquire(0):
219 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000220 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000221 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000222 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223
224 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000225 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000226 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000227 waiter = _allocate_lock()
228 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000229 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000230 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000231 try: # restore state no matter what (e.g., KeyboardInterrupt)
232 if timeout is None:
233 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000234 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000235 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000236 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000237 # Balancing act: We can't afford a pure busy loop, so we
238 # have to sleep; but if we sleep the whole timeout time,
239 # we'll be unresponsive. The scheme here sleeps very
240 # little at first, longer as time goes on, but never longer
241 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000242 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000243 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000244 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000245 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000246 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000247 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000248 remaining = endtime - _time()
249 if remaining <= 0:
250 break
251 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000252 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000253 if not gotit:
254 if __debug__:
255 self._note("%s.wait(%s): timed out", self, timeout)
256 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000257 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000258 except ValueError:
259 pass
260 else:
261 if __debug__:
262 self._note("%s.wait(%s): got it", self, timeout)
263 finally:
264 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000265
266 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000267 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000268 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000269 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000270 waiters = __waiters[:n]
271 if not waiters:
272 if __debug__:
273 self._note("%s.notify(): no waiters", self)
274 return
275 self._note("%s.notify(): notifying %d waiter%s", self, n,
276 n!=1 and "s" or "")
277 for waiter in waiters:
278 waiter.release()
279 try:
280 __waiters.remove(waiter)
281 except ValueError:
282 pass
283
Benjamin Peterson672b8032008-06-11 19:14:14 +0000284 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000285 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000287 notifyAll = notify_all
288
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000289
290def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000291 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292
293class _Semaphore(_Verbose):
294
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000295 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000296
297 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000298 if value < 0:
299 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000300 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000301 self._cond = Condition(Lock())
302 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000303
Georg Brandl7b1c4142009-09-16 14:23:20 +0000304 def acquire(self, blocking=True):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000305 rc = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000306 self._cond.acquire()
307 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308 if not blocking:
309 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000310 if __debug__:
311 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000312 self, blocking, self._value)
313 self._cond.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000314 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000315 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000316 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000317 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000318 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000319 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000320 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000321 return rc
322
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000323 __enter__ = acquire
324
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000325 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000326 self._cond.acquire()
327 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000328 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000329 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000330 self, self._value)
331 self._cond.notify()
332 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000333
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000334 def __exit__(self, t, v, tb):
335 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000336
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000337
Skip Montanaroe428bb72001-08-20 20:27:58 +0000338def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000339 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000340
341class _BoundedSemaphore(_Semaphore):
342 """Semaphore that checks that # releases is <= # acquires"""
343 def __init__(self, value=1, verbose=None):
344 _Semaphore.__init__(self, value, verbose)
345 self._initial_value = value
346
347 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000348 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000349 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000350 return _Semaphore.release(self)
351
352
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000353def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000354 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000355
356class _Event(_Verbose):
357
358 # After Tim Peters' event class (without is_posted())
359
360 def __init__(self, verbose=None):
361 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000362 self._cond = Condition(Lock())
363 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000364
Benjamin Peterson672b8032008-06-11 19:14:14 +0000365 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000366 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000367
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000368 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000369
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000370 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000371 self._cond.acquire()
372 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000373 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000374 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000375 finally:
376 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000377
378 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000379 self._cond.acquire()
380 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000381 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000382 finally:
383 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000384
385 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000386 self._cond.acquire()
387 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000388 if not self._flag:
389 self._cond.wait(timeout)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000390 return self._flag
Christian Heimes969fe572008-01-25 11:23:10 +0000391 finally:
392 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000393
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000394# Helper to generate new thread names
395_counter = 0
396def _newname(template="Thread-%d"):
397 global _counter
398 _counter = _counter + 1
399 return template % _counter
400
401# Active thread administration
402_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000403_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000404_limbo = {}
405
406
407# Main class for threads
408
409class Thread(_Verbose):
410
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000411 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000412 # Need to store a reference to sys.exc_info for printing
413 # out exceptions when a thread tries to use a global var. during interp.
414 # shutdown and thus raises an exception about trying to perform some
415 # operation on/with a NoneType
416 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000417 # Keep sys.exc_clear too to clear the exception just before
418 # allowing .join() to return.
419 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000420
421 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000422 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000423 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000424 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000425 if kwargs is None:
426 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000427 self._target = target
428 self._name = str(name or _newname())
429 self._args = args
430 self._kwargs = kwargs
431 self._daemonic = self._set_daemon()
Georg Brandl0c77a822008-06-10 16:37:50 +0000432 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000433 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000434 self._stopped = False
435 self._block = Condition(Lock())
436 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000437 # sys.stderr is not stored in the class like
438 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000439 self._stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000440
441 def _set_daemon(self):
442 # Overridden in _MainThread and _DummyThread
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000443 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000444
445 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000446 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000447 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000448 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000449 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000450 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000451 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000452 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000453 status += " daemon"
454 if self._ident is not None:
455 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000456 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000457
458 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000459 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000460 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000461
Benjamin Peterson672b8032008-06-11 19:14:14 +0000462 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000463 raise RuntimeError("thread already started")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000464 if __debug__:
465 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000466 with _active_limbo_lock:
467 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000468 try:
469 _start_new_thread(self._bootstrap, ())
470 except Exception:
471 with _active_limbo_lock:
472 del _limbo[self]
473 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000474 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000475
476 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000477 try:
478 if self._target:
479 self._target(*self._args, **self._kwargs)
480 finally:
481 # Avoid a refcycle if the thread is running a function with
482 # an argument that has a member that points to the thread.
483 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000484
Guido van Rossumd0648992007-08-20 19:25:41 +0000485 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000486 # Wrapper around the real bootstrap code that ignores
487 # exceptions during interpreter cleanup. Those typically
488 # happen when a daemon thread wakes up at an unfortunate
489 # moment, finds the world around it destroyed, and raises some
490 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000491 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000492 # don't help anybody, and they confuse users, so we suppress
493 # them. We suppress them only when it appears that the world
494 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000495 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000496 # reported. Also, we only suppress them for daemonic threads;
497 # if a non-daemonic encounters this, something else is wrong.
498 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000499 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000500 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000501 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000502 return
503 raise
504
Benjamin Petersond23f8222009-04-05 19:13:16 +0000505 def _set_ident(self):
506 self._ident = _get_ident()
507
Guido van Rossumd0648992007-08-20 19:25:41 +0000508 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000509 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000510 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000511 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000512 with _active_limbo_lock:
513 _active[self._ident] = self
514 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000515 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000516 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000517
518 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000519 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000520 _sys.settrace(_trace_hook)
521 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000522 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000523 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000524
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000525 try:
526 self.run()
527 except SystemExit:
528 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000529 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000530 except:
531 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000532 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000533 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000534 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000535 # _sys) in case sys.stderr was redefined since the creation of
536 # self.
537 if _sys:
538 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000539 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000540 else:
541 # Do the best job possible w/o a huge amt. of code to
542 # approximate a traceback (code ideas from
543 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000544 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000545 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000546 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000547 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000548 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000549 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000550 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000551 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000552 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000553 ' File "%s", line %s, in %s' %
554 (exc_tb.tb_frame.f_code.co_filename,
555 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000556 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000557 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000558 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000559 # Make sure that exc_tb gets deleted since it is a memory
560 # hog; deleting everything else is just for thoroughness
561 finally:
562 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000563 else:
564 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000565 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000566 finally:
567 # Prevent a race in
568 # test_threading.test_no_refcycle_through_target when
569 # the exception keeps the target alive past when we
570 # assert that it's dead.
571 #XXX self.__exc_clear()
572 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000573 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000574 with _active_limbo_lock:
575 self._stop()
576 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000577 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000578 # grabs _active_limbo_lock.
579 del _active[_get_ident()]
580 except:
581 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000582
Guido van Rossumd0648992007-08-20 19:25:41 +0000583 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000584 self._block.acquire()
585 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000586 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000587 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000588
Guido van Rossumd0648992007-08-20 19:25:41 +0000589 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000590 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000591
Georg Brandl2067bfd2008-05-25 13:05:15 +0000592 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000593 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000594 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000595 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000596 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
597 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000598 # len(_active) is always <= 1 here, and any Thread instance created
599 # overwrites the (if any) thread currently registered in _active.
600 #
601 # An instance of _MainThread is always created by 'threading'. This
602 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000603 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000604 # same key in the dict. So when the _MainThread instance created by
605 # 'threading' tries to clean itself up when atexit calls this method
606 # it gets a KeyError if another Thread instance was created.
607 #
608 # This all means that KeyError from trying to delete something from
609 # _active if dummy_threading is being used is a red herring. But
610 # since it isn't if dummy_threading is *not* being used then don't
611 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000612
Christian Heimes969fe572008-01-25 11:23:10 +0000613 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000614 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000615 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000616 # There must not be any python code between the previous line
617 # and after the lock is released. Otherwise a tracing function
618 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000619 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000620 except KeyError:
621 if 'dummy_threading' not in _sys.modules:
622 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000623
624 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000625 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000626 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000627 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000628 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000629 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000630 raise RuntimeError("cannot join current thread")
631
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000632 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000633 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000634 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000635
636 self._block.acquire()
637 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000638 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000639 while not self._stopped:
640 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000641 if __debug__:
642 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000643 else:
644 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000645 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000646 delay = deadline - _time()
647 if delay <= 0:
648 if __debug__:
649 self._note("%s.join(): timed out", self)
650 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000651 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000652 else:
653 if __debug__:
654 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000655 finally:
656 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000657
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000658 @property
659 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000660 assert self._initialized, "Thread.__init__() not called"
661 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000662
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000663 @name.setter
664 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000665 assert self._initialized, "Thread.__init__() not called"
666 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000667
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000668 @property
669 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000670 assert self._initialized, "Thread.__init__() not called"
671 return self._ident
672
Benjamin Peterson672b8032008-06-11 19:14:14 +0000673 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000674 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000675 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000676
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000677 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000678
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000679 @property
680 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000681 assert self._initialized, "Thread.__init__() not called"
682 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000683
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000684 @daemon.setter
685 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000686 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000687 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000688 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000689 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000690 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000691
Benjamin Peterson6640d722008-08-18 18:16:46 +0000692 def isDaemon(self):
693 return self.daemon
694
695 def setDaemon(self, daemonic):
696 self.daemon = daemonic
697
698 def getName(self):
699 return self.name
700
701 def setName(self, name):
702 self.name = name
703
Martin v. Löwis44f86962001-09-05 13:44:54 +0000704# The timer class was contributed by Itamar Shtull-Trauring
705
706def Timer(*args, **kwargs):
707 return _Timer(*args, **kwargs)
708
709class _Timer(Thread):
710 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000711
Martin v. Löwis44f86962001-09-05 13:44:54 +0000712 t = Timer(30.0, f, args=[], kwargs={})
713 t.start()
714 t.cancel() # stop the timer's action if it's still waiting
715 """
Tim Petersb64bec32001-09-18 02:26:39 +0000716
Martin v. Löwis44f86962001-09-05 13:44:54 +0000717 def __init__(self, interval, function, args=[], kwargs={}):
718 Thread.__init__(self)
719 self.interval = interval
720 self.function = function
721 self.args = args
722 self.kwargs = kwargs
723 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000724
Martin v. Löwis44f86962001-09-05 13:44:54 +0000725 def cancel(self):
726 """Stop the timer if it hasn't finished yet"""
727 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000728
Martin v. Löwis44f86962001-09-05 13:44:54 +0000729 def run(self):
730 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000731 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000732 self.function(*self.args, **self.kwargs)
733 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000734
735# Special thread class to represent the main thread
736# This is garbage collected through an exit handler
737
738class _MainThread(Thread):
739
740 def __init__(self):
741 Thread.__init__(self, name="MainThread")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000742 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000743 self._set_ident()
744 with _active_limbo_lock:
745 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000746
747 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000748 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000749
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000750 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000751 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000752 t = _pickSomeNonDaemonThread()
753 if t:
754 if __debug__:
755 self._note("%s: waiting for other threads", self)
756 while t:
757 t.join()
758 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759 if __debug__:
760 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000761 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000762
763def _pickSomeNonDaemonThread():
764 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000765 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000766 return t
767 return None
768
769
770# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000771# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000772# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000773# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000774# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775# They are marked as daemon threads so we won't wait for them
776# when we exit (conform previous semantics).
777
778class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000779
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000780 def __init__(self):
781 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000782
783 # Thread.__block consumes an OS-level locking primitive, which
784 # can never be used by a _DummyThread. Since a _DummyThread
785 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000786 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000787
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000788
789 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000790 self._set_ident()
791 with _active_limbo_lock:
792 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000793
794 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000795 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000796
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000797 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000798 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000799
800
801# Global API functions
802
Benjamin Peterson672b8032008-06-11 19:14:14 +0000803def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000804 try:
805 return _active[_get_ident()]
806 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000807 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000808 return _DummyThread()
809
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000810currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000811
Benjamin Peterson672b8032008-06-11 19:14:14 +0000812def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000813 with _active_limbo_lock:
814 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000815
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000816activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000817
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000818def _enumerate():
819 # Same as enumerate(), but without the lock. Internal use only.
820 return list(_active.values()) + list(_limbo.values())
821
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000823 with _active_limbo_lock:
824 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000825
Georg Brandl2067bfd2008-05-25 13:05:15 +0000826from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000827
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000828# Create the main thread object,
829# and make it available for the interpreter
830# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000831
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000832_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000833
Jim Fultond15dc062004-07-14 19:11:50 +0000834# get thread-local implementation, either from the thread
835# module, or from the python fallback
836
837try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000838 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000839except ImportError:
840 from _threading_local import local
841
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000842
Jesse Nollera8513972008-07-17 16:49:17 +0000843def _after_fork():
844 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
845 # is called from PyOS_AfterFork. Here we cleanup threading module state
846 # that should not exist after a fork.
847
848 # Reset _active_limbo_lock, in case we forked while the lock was held
849 # by another (non-forked) thread. http://bugs.python.org/issue874900
850 global _active_limbo_lock
851 _active_limbo_lock = _allocate_lock()
852
853 # fork() only copied the current thread; clear references to others.
854 new_active = {}
855 current = current_thread()
856 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000857 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +0000858 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000859 # There is only one active thread. We reset the ident to
860 # its new value since it can have changed.
861 ident = _get_ident()
862 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000863 new_active[ident] = thread
864 else:
865 # All the others are already stopped.
866 # We don't call _Thread__stop() because it tries to acquire
867 # thread._Thread__block which could also have been held while
868 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000869 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +0000870
871 _limbo.clear()
872 _active.clear()
873 _active.update(new_active)
874 assert len(_active) == 1
875
876
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000877# Self-test code
878
879def _test():
880
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881 class BoundedQueue(_Verbose):
882
883 def __init__(self, limit):
884 _Verbose.__init__(self)
885 self.mon = RLock()
886 self.rc = Condition(self.mon)
887 self.wc = Condition(self.mon)
888 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000889 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000890
891 def put(self, item):
892 self.mon.acquire()
893 while len(self.queue) >= self.limit:
894 self._note("put(%s): queue full", item)
895 self.wc.wait()
896 self.queue.append(item)
897 self._note("put(%s): appended, length now %d",
898 item, len(self.queue))
899 self.rc.notify()
900 self.mon.release()
901
902 def get(self):
903 self.mon.acquire()
904 while not self.queue:
905 self._note("get(): queue empty")
906 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000907 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000908 self._note("get(): got %s, %d left", item, len(self.queue))
909 self.wc.notify()
910 self.mon.release()
911 return item
912
913 class ProducerThread(Thread):
914
915 def __init__(self, queue, quota):
916 Thread.__init__(self, name="Producer")
917 self.queue = queue
918 self.quota = quota
919
920 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000921 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000922 counter = 0
923 while counter < self.quota:
924 counter = counter + 1
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000925 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000926 _sleep(random() * 0.00001)
927
928
929 class ConsumerThread(Thread):
930
931 def __init__(self, queue, count):
932 Thread.__init__(self, name="Consumer")
933 self.queue = queue
934 self.count = count
935
936 def run(self):
937 while self.count > 0:
938 item = self.queue.get()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000939 print(item)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000940 self.count = self.count - 1
941
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000942 NP = 3
943 QL = 4
944 NI = 5
945
946 Q = BoundedQueue(QL)
947 P = []
948 for i in range(NP):
949 t = ProducerThread(Q, NI)
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000950 t.name = "Producer-%d" % (i+1)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000951 P.append(t)
952 C = ConsumerThread(Q, NI*NP)
953 for t in P:
954 t.start()
955 _sleep(0.000001)
956 C.start()
957 for t in P:
958 t.join()
959 C.join()
960
961if __name__ == '__main__':
962 _test()