blob: c965c6f0f5a056dd22d66f37268ede82f7a4a816 [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
Antoine Pitroubc7f20c2010-12-17 17:44:45 +000053 # Issue #4188: calling current_thread() can incur an infinite
54 # recursion if it has to create a DummyThread on the fly.
55 ident = _get_ident()
56 try:
57 name = _active[ident].name
58 except KeyError:
59 name = "<OS thread %d>" % ident
60 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000061 _sys.stderr.write(format)
62
63else:
64 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000065 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000066 def __init__(self, verbose=None):
67 pass
68 def _note(self, *args):
69 pass
70
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000071# Support for profile and trace hooks
72
73_profile_hook = None
74_trace_hook = None
75
76def setprofile(func):
77 global _profile_hook
78 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000079
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000080def settrace(func):
81 global _trace_hook
82 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000083
84# Synchronization classes
85
86Lock = _allocate_lock
87
88def RLock(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +000089 return _RLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000090
91class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000092
Guido van Rossum7f5013a1998-04-09 22:01:42 +000093 def __init__(self, verbose=None):
94 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +000095 self._block = _allocate_lock()
96 self._owner = None
97 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000098
99 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000100 owner = self._owner
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000101 try:
102 owner = _active[owner].name
103 except KeyError:
104 pass
105 return "<%s owner=%r count=%d>" % (
106 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000107
Georg Brandlb044b2a2009-09-16 16:05:59 +0000108 def acquire(self, blocking=True):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000109 me = _get_ident()
110 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000111 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000112 if __debug__:
113 self._note("%s.acquire(%s): recursive success", self, blocking)
114 return 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000115 rc = self._block.acquire(blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000116 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000117 self._owner = me
118 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000119 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000120 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121 else:
122 if __debug__:
123 self._note("%s.acquire(%s): failure", self, blocking)
124 return rc
125
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000126 __enter__ = acquire
127
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000128 def release(self):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000129 if self._owner != _get_ident():
Georg Brandl628e6f92009-10-27 20:24:45 +0000130 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000131 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000132 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000133 self._owner = None
134 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000135 if __debug__:
136 self._note("%s.release(): final release", self)
137 else:
138 if __debug__:
139 self._note("%s.release(): non-final release", self)
140
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000141 def __exit__(self, t, v, tb):
142 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000143
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000144 # Internal methods used by condition variables
145
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000146 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000147 self._block.acquire()
148 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000149 if __debug__:
150 self._note("%s._acquire_restore()", self)
151
152 def _release_save(self):
153 if __debug__:
154 self._note("%s._release_save()", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000155 count = self._count
156 self._count = 0
157 owner = self._owner
158 self._owner = None
159 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000160 return (count, owner)
161
162 def _is_owned(self):
Antoine Pitrou959f3e52009-11-09 16:52:46 +0000163 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000164
165
166def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000167 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000168
169class _Condition(_Verbose):
170
171 def __init__(self, lock=None, verbose=None):
172 _Verbose.__init__(self, verbose)
173 if lock is None:
174 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000175 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176 # Export the lock's acquire() and release() methods
177 self.acquire = lock.acquire
178 self.release = lock.release
179 # If the lock defines _release_save() and/or _acquire_restore(),
180 # these override the default implementations (which just call
181 # release() and acquire() on the lock). Ditto for _is_owned().
182 try:
183 self._release_save = lock._release_save
184 except AttributeError:
185 pass
186 try:
187 self._acquire_restore = lock._acquire_restore
188 except AttributeError:
189 pass
190 try:
191 self._is_owned = lock._is_owned
192 except AttributeError:
193 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000194 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000195
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000197 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000198
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000200 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000201
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000202 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000203 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
205 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000206 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000207
208 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000209 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000210
211 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000212 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000213 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000214 if self._lock.acquire(0):
215 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000216 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000218 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000219
220 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000221 if not self._is_owned():
Georg Brandl628e6f92009-10-27 20:24:45 +0000222 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223 waiter = _allocate_lock()
224 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000225 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000226 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000227 try: # restore state no matter what (e.g., KeyboardInterrupt)
228 if timeout is None:
229 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000230 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000231 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000233 # Balancing act: We can't afford a pure busy loop, so we
234 # have to sleep; but if we sleep the whole timeout time,
235 # we'll be unresponsive. The scheme here sleeps very
236 # little at first, longer as time goes on, but never longer
237 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000238 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000239 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000240 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000241 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000242 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000243 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000244 remaining = endtime - _time()
245 if remaining <= 0:
246 break
247 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000248 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000249 if not gotit:
250 if __debug__:
251 self._note("%s.wait(%s): timed out", self, timeout)
252 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000253 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000254 except ValueError:
255 pass
256 else:
257 if __debug__:
258 self._note("%s.wait(%s): got it", self, timeout)
259 finally:
260 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000261
262 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000263 if not self._is_owned():
Georg Brandl628e6f92009-10-27 20:24:45 +0000264 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000265 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000266 waiters = __waiters[:n]
267 if not waiters:
268 if __debug__:
269 self._note("%s.notify(): no waiters", self)
270 return
271 self._note("%s.notify(): notifying %d waiter%s", self, n,
272 n!=1 and "s" or "")
273 for waiter in waiters:
274 waiter.release()
275 try:
276 __waiters.remove(waiter)
277 except ValueError:
278 pass
279
Benjamin Peterson672b8032008-06-11 19:14:14 +0000280 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000281 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000282
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000283 notifyAll = notify_all
284
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000285
286def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000287 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000288
289class _Semaphore(_Verbose):
290
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000291 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292
293 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000294 if value < 0:
295 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000296 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000297 self._cond = Condition(Lock())
298 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000299
Georg Brandlb044b2a2009-09-16 16:05:59 +0000300 def acquire(self, blocking=True):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000301 rc = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000302 self._cond.acquire()
303 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000304 if not blocking:
305 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000306 if __debug__:
307 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000308 self, blocking, self._value)
309 self._cond.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000310 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000311 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000312 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000313 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000314 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000315 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000316 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317 return rc
318
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000319 __enter__ = acquire
320
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000321 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000322 self._cond.acquire()
323 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000324 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000325 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000326 self, self._value)
327 self._cond.notify()
328 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000329
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000330 def __exit__(self, t, v, tb):
331 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000332
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000333
Skip Montanaroe428bb72001-08-20 20:27:58 +0000334def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000335 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000336
337class _BoundedSemaphore(_Semaphore):
338 """Semaphore that checks that # releases is <= # acquires"""
339 def __init__(self, value=1, verbose=None):
340 _Semaphore.__init__(self, value, verbose)
341 self._initial_value = value
342
343 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000344 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000345 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000346 return _Semaphore.release(self)
347
348
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000349def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000350 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000351
352class _Event(_Verbose):
353
354 # After Tim Peters' event class (without is_posted())
355
356 def __init__(self, verbose=None):
357 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000358 self._cond = Condition(Lock())
359 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000360
Gregory P. Smithdb0ef2b2011-01-04 18:42:29 +0000361 def _reset_internal_locks(self):
362 # private! called by Thread._reset_internal_locks by _after_fork()
363 self._cond.__init__()
364
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
Gregory P. Smithdb0ef2b2011-01-04 18:42:29 +0000441 def _reset_internal_locks(self):
442 # private! Called by _after_fork() to reset our internal locks as
443 # they may be in an invalid state leading to a deadlock or crash.
444 if hasattr(self, '_block'): # DummyThread deletes _block
445 self._block.__init__()
446 self._started._reset_internal_locks()
447
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000448 def _set_daemon(self):
449 # Overridden in _MainThread and _DummyThread
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000450 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000451
452 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000453 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000454 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000455 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000456 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000457 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000458 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000459 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000460 status += " daemon"
461 if self._ident is not None:
462 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000463 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000464
465 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000466 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000467 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000468
Benjamin Peterson672b8032008-06-11 19:14:14 +0000469 if self._started.is_set():
Senthil Kumaranf58d8e72010-04-06 03:36:00 +0000470 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000471 if __debug__:
472 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000473 with _active_limbo_lock:
474 _limbo[self] = self
Gregory P. Smith31d12ca2010-02-28 19:21:42 +0000475 try:
476 _start_new_thread(self._bootstrap, ())
477 except Exception:
478 with _active_limbo_lock:
479 del _limbo[self]
480 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000481 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000482
483 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000484 try:
485 if self._target:
486 self._target(*self._args, **self._kwargs)
487 finally:
488 # Avoid a refcycle if the thread is running a function with
489 # an argument that has a member that points to the thread.
490 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000491
Guido van Rossumd0648992007-08-20 19:25:41 +0000492 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000493 # Wrapper around the real bootstrap code that ignores
494 # exceptions during interpreter cleanup. Those typically
495 # happen when a daemon thread wakes up at an unfortunate
496 # moment, finds the world around it destroyed, and raises some
497 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000498 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000499 # don't help anybody, and they confuse users, so we suppress
500 # them. We suppress them only when it appears that the world
501 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000502 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000503 # reported. Also, we only suppress them for daemonic threads;
504 # if a non-daemonic encounters this, something else is wrong.
505 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000506 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000507 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000508 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000509 return
510 raise
511
Benjamin Petersond23f8222009-04-05 19:13:16 +0000512 def _set_ident(self):
513 self._ident = _get_ident()
514
Guido van Rossumd0648992007-08-20 19:25:41 +0000515 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000516 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000517 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000518 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000519 with _active_limbo_lock:
520 _active[self._ident] = self
521 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000522 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000523 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000524
525 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000526 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000527 _sys.settrace(_trace_hook)
528 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000529 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000530 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000531
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000532 try:
533 self.run()
534 except SystemExit:
535 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000536 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000537 except:
538 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000539 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000540 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000541 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000542 # _sys) in case sys.stderr was redefined since the creation of
543 # self.
544 if _sys:
545 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000546 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000547 else:
548 # Do the best job possible w/o a huge amt. of code to
549 # approximate a traceback (code ideas from
550 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000551 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000552 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000553 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000554 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000555 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000556 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000557 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000558 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000559 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000560 ' File "%s", line %s, in %s' %
561 (exc_tb.tb_frame.f_code.co_filename,
562 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000563 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000564 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000565 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000566 # Make sure that exc_tb gets deleted since it is a memory
567 # hog; deleting everything else is just for thoroughness
568 finally:
569 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000570 else:
571 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000572 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000573 finally:
574 # Prevent a race in
575 # test_threading.test_no_refcycle_through_target when
576 # the exception keeps the target alive past when we
577 # assert that it's dead.
578 #XXX self.__exc_clear()
579 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000580 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000581 with _active_limbo_lock:
582 self._stop()
583 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000584 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000585 # grabs _active_limbo_lock.
586 del _active[_get_ident()]
587 except:
588 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000589
Guido van Rossumd0648992007-08-20 19:25:41 +0000590 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000591 self._block.acquire()
592 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000593 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000594 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000595
Guido van Rossumd0648992007-08-20 19:25:41 +0000596 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000597 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000598
Georg Brandl2067bfd2008-05-25 13:05:15 +0000599 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000600 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000601 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000602 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000603 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
604 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000605 # len(_active) is always <= 1 here, and any Thread instance created
606 # overwrites the (if any) thread currently registered in _active.
607 #
608 # An instance of _MainThread is always created by 'threading'. This
609 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000610 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000611 # same key in the dict. So when the _MainThread instance created by
612 # 'threading' tries to clean itself up when atexit calls this method
613 # it gets a KeyError if another Thread instance was created.
614 #
615 # This all means that KeyError from trying to delete something from
616 # _active if dummy_threading is being used is a red herring. But
617 # since it isn't if dummy_threading is *not* being used then don't
618 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000619
Christian Heimes969fe572008-01-25 11:23:10 +0000620 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000621 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000622 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000623 # There must not be any python code between the previous line
624 # and after the lock is released. Otherwise a tracing function
625 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000626 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000627 except KeyError:
628 if 'dummy_threading' not in _sys.modules:
629 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000630
631 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000632 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000633 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000634 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000635 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000636 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000637 raise RuntimeError("cannot join current thread")
638
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000639 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000640 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000641 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000642
643 self._block.acquire()
644 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000645 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000646 while not self._stopped:
647 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000648 if __debug__:
649 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000650 else:
651 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000652 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000653 delay = deadline - _time()
654 if delay <= 0:
655 if __debug__:
656 self._note("%s.join(): timed out", self)
657 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000658 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000659 else:
660 if __debug__:
661 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000662 finally:
663 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000664
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000665 @property
666 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000667 assert self._initialized, "Thread.__init__() not called"
668 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000669
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000670 @name.setter
671 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000672 assert self._initialized, "Thread.__init__() not called"
673 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000674
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000675 @property
676 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000677 assert self._initialized, "Thread.__init__() not called"
678 return self._ident
679
Benjamin Peterson672b8032008-06-11 19:14:14 +0000680 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000681 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000682 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000683
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000684 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000685
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000686 @property
687 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000688 assert self._initialized, "Thread.__init__() not called"
689 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000690
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000691 @daemon.setter
692 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000693 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000694 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000695 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000696 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000697 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000698
Benjamin Peterson6640d722008-08-18 18:16:46 +0000699 def isDaemon(self):
700 return self.daemon
701
702 def setDaemon(self, daemonic):
703 self.daemon = daemonic
704
705 def getName(self):
706 return self.name
707
708 def setName(self, name):
709 self.name = name
710
Martin v. Löwis44f86962001-09-05 13:44:54 +0000711# The timer class was contributed by Itamar Shtull-Trauring
712
713def Timer(*args, **kwargs):
714 return _Timer(*args, **kwargs)
715
716class _Timer(Thread):
717 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000718
Martin v. Löwis44f86962001-09-05 13:44:54 +0000719 t = Timer(30.0, f, args=[], kwargs={})
720 t.start()
721 t.cancel() # stop the timer's action if it's still waiting
722 """
Tim Petersb64bec32001-09-18 02:26:39 +0000723
Martin v. Löwis44f86962001-09-05 13:44:54 +0000724 def __init__(self, interval, function, args=[], kwargs={}):
725 Thread.__init__(self)
726 self.interval = interval
727 self.function = function
728 self.args = args
729 self.kwargs = kwargs
730 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000731
Martin v. Löwis44f86962001-09-05 13:44:54 +0000732 def cancel(self):
733 """Stop the timer if it hasn't finished yet"""
734 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000735
Martin v. Löwis44f86962001-09-05 13:44:54 +0000736 def run(self):
737 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000738 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000739 self.function(*self.args, **self.kwargs)
740 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000741
742# Special thread class to represent the main thread
743# This is garbage collected through an exit handler
744
745class _MainThread(Thread):
746
747 def __init__(self):
748 Thread.__init__(self, name="MainThread")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000749 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000750 self._set_ident()
751 with _active_limbo_lock:
752 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000753
754 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000755 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000756
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000757 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000758 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759 t = _pickSomeNonDaemonThread()
760 if t:
761 if __debug__:
762 self._note("%s: waiting for other threads", self)
763 while t:
764 t.join()
765 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000766 if __debug__:
767 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000768 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000769
770def _pickSomeNonDaemonThread():
771 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000772 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000773 return t
774 return None
775
776
777# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000778# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000779# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000780# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000781# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000782# They are marked as daemon threads so we won't wait for them
783# when we exit (conform previous semantics).
784
785class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000786
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000787 def __init__(self):
788 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000789
Gregory P. Smithdb0ef2b2011-01-04 18:42:29 +0000790 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000791 # can never be used by a _DummyThread. Since a _DummyThread
792 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000793 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000794
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000795 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000796 self._set_ident()
797 with _active_limbo_lock:
798 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000799
800 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000801 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000802
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000803 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000804 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000805
806
807# Global API functions
808
Benjamin Peterson672b8032008-06-11 19:14:14 +0000809def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000810 try:
811 return _active[_get_ident()]
812 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000813 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000814 return _DummyThread()
815
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000816currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000817
Benjamin Peterson672b8032008-06-11 19:14:14 +0000818def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000819 with _active_limbo_lock:
820 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000821
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000822activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000823
Antoine Pitroua9546072009-11-05 13:51:19 +0000824def _enumerate():
825 # Same as enumerate(), but without the lock. Internal use only.
826 return list(_active.values()) + list(_limbo.values())
827
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000828def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000829 with _active_limbo_lock:
830 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000831
Georg Brandl2067bfd2008-05-25 13:05:15 +0000832from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000833
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000834# Create the main thread object,
835# and make it available for the interpreter
836# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000837
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000838_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000839
Jim Fultond15dc062004-07-14 19:11:50 +0000840# get thread-local implementation, either from the thread
841# module, or from the python fallback
842
843try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000844 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000845except ImportError:
846 from _threading_local import local
847
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000848
Jesse Nollera8513972008-07-17 16:49:17 +0000849def _after_fork():
850 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
851 # is called from PyOS_AfterFork. Here we cleanup threading module state
852 # that should not exist after a fork.
853
854 # Reset _active_limbo_lock, in case we forked while the lock was held
855 # by another (non-forked) thread. http://bugs.python.org/issue874900
856 global _active_limbo_lock
857 _active_limbo_lock = _allocate_lock()
858
859 # fork() only copied the current thread; clear references to others.
860 new_active = {}
861 current = current_thread()
862 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000863 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +0000864 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000865 # There is only one active thread. We reset the ident to
866 # its new value since it can have changed.
867 ident = _get_ident()
868 thread._ident = ident
Gregory P. Smith4b129d22011-01-04 00:51:50 +0000869 # Any condition variables hanging off of the active thread may
870 # be in an invalid state, so we reinitialize them.
Gregory P. Smithdb0ef2b2011-01-04 18:42:29 +0000871 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000872 new_active[ident] = thread
873 else:
874 # All the others are already stopped.
875 # We don't call _Thread__stop() because it tries to acquire
876 # thread._Thread__block which could also have been held while
877 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000878 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +0000879
880 _limbo.clear()
881 _active.clear()
882 _active.update(new_active)
883 assert len(_active) == 1
884
885
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000886# Self-test code
887
888def _test():
889
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000890 class BoundedQueue(_Verbose):
891
892 def __init__(self, limit):
893 _Verbose.__init__(self)
894 self.mon = RLock()
895 self.rc = Condition(self.mon)
896 self.wc = Condition(self.mon)
897 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000898 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000899
900 def put(self, item):
901 self.mon.acquire()
902 while len(self.queue) >= self.limit:
903 self._note("put(%s): queue full", item)
904 self.wc.wait()
905 self.queue.append(item)
906 self._note("put(%s): appended, length now %d",
907 item, len(self.queue))
908 self.rc.notify()
909 self.mon.release()
910
911 def get(self):
912 self.mon.acquire()
913 while not self.queue:
914 self._note("get(): queue empty")
915 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000916 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000917 self._note("get(): got %s, %d left", item, len(self.queue))
918 self.wc.notify()
919 self.mon.release()
920 return item
921
922 class ProducerThread(Thread):
923
924 def __init__(self, queue, quota):
925 Thread.__init__(self, name="Producer")
926 self.queue = queue
927 self.quota = quota
928
929 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000930 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000931 counter = 0
932 while counter < self.quota:
933 counter = counter + 1
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000934 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000935 _sleep(random() * 0.00001)
936
937
938 class ConsumerThread(Thread):
939
940 def __init__(self, queue, count):
941 Thread.__init__(self, name="Consumer")
942 self.queue = queue
943 self.count = count
944
945 def run(self):
946 while self.count > 0:
947 item = self.queue.get()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000948 print(item)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000949 self.count = self.count - 1
950
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000951 NP = 3
952 QL = 4
953 NI = 5
954
955 Q = BoundedQueue(QL)
956 P = []
957 for i in range(NP):
958 t = ProducerThread(Q, NI)
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000959 t.name = "Producer-%d" % (i+1)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000960 P.append(t)
961 C = ConsumerThread(Q, NI*NP)
962 for t in P:
963 t.start()
964 _sleep(0.000001)
965 C.start()
966 for t in P:
967 t.join()
968 C.join()
969
970if __name__ == '__main__':
971 _test()