blob: 527f20acc60cb893917802bac02580d06c843a7e [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
4
5try:
6 import thread
7except ImportError:
8 del _sys.modules[__name__]
9 raise
10
Jeffrey Yasskin105f3d42008-03-31 00:35:53 +000011import warnings
Benjamin Petersonf4395602008-06-11 17:50:00 +000012
Brian Curtina9391052012-08-09 23:04:42 -050013from collections import deque as _deque
R David Murrayd999c342014-10-04 17:40:43 -040014from itertools import count as _count
Fred Drakea8725952002-12-30 23:32:50 +000015from time import time as _time, sleep as _sleep
Neil Schemenauerf607fc52003-11-05 23:03:00 +000016from traceback import format_exc as _format_exc
Guido van Rossum7f5013a1998-04-09 22:01:42 +000017
Benjamin Peterson973e6c22008-09-01 23:12:58 +000018# Note regarding PEP 8 compliant aliases
19# This threading model was originally inspired by Java, and inherited
20# the convention of camelCase function and method names from that
21# language. While those names are not in any imminent danger of being
22# deprecated, starting with Python 2.6, the module now provides a
23# PEP 8 compliant alias for any such method name.
24# Using the new PEP 8 compliant names also facilitates substitution
25# with the multiprocessing module, which doesn't provide the old
26# Java inspired names.
27
28
Guido van Rossum7f5013a1998-04-09 22:01:42 +000029# Rename some stuff so "from threading import *" is safe
Benjamin Peterson13f73822008-06-11 18:02:31 +000030__all__ = ['activeCount', 'active_count', 'Condition', 'currentThread',
31 'current_thread', 'enumerate', 'Event',
Tim Peters685e6972003-06-29 16:50:06 +000032 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
Andrew MacIntyre92913322006-06-13 15:04:24 +000033 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000034
Guido van Rossum7f5013a1998-04-09 22:01:42 +000035_start_new_thread = thread.start_new_thread
36_allocate_lock = thread.allocate_lock
37_get_ident = thread.get_ident
Jeremy Hyltonb5fc7492000-06-01 01:17:17 +000038ThreadError = thread.error
Guido van Rossum7f5013a1998-04-09 22:01:42 +000039del thread
40
Guido van Rossum7f5013a1998-04-09 22:01:42 +000041
Jeffrey Yasskin105f3d42008-03-31 00:35:53 +000042# sys.exc_clear is used to work around the fact that except blocks
43# don't fully clear the exception until 3.0.
44warnings.filterwarnings('ignore', category=DeprecationWarning,
45 module='threading', message='sys.exc_clear')
46
Tim Peters59aba122003-07-01 20:01:55 +000047# Debug support (adapted from ihooks.py).
48# All the major classes here derive from _Verbose. We force that to
49# be a new-style class so that all the major classes here are new-style.
50# This helps debugging (type(instance) is more revealing for instances
51# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052
Tim Peters0939fac2003-07-01 19:28:44 +000053_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000054
55if __debug__:
56
Tim Peters59aba122003-07-01 20:01:55 +000057 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000058
59 def __init__(self, verbose=None):
60 if verbose is None:
61 verbose = _VERBOSE
62 self.__verbose = verbose
63
64 def _note(self, format, *args):
65 if self.__verbose:
66 format = format % args
Antoine Pitrou47900cf2010-12-17 17:45:12 +000067 # Issue #4188: calling current_thread() can incur an infinite
68 # recursion if it has to create a DummyThread on the fly.
69 ident = _get_ident()
70 try:
71 name = _active[ident].name
72 except KeyError:
73 name = "<OS thread %d>" % ident
74 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000075 _sys.stderr.write(format)
76
77else:
78 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000079 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000080 def __init__(self, verbose=None):
81 pass
82 def _note(self, *args):
83 pass
84
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000085# Support for profile and trace hooks
86
87_profile_hook = None
88_trace_hook = None
89
90def setprofile(func):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -070091 """Set a profile function for all threads started from the threading module.
92
93 The func will be passed to sys.setprofile() for each thread, before its
94 run() method is called.
95
96 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000097 global _profile_hook
98 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000099
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000100def settrace(func):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700101 """Set a trace function for all threads started from the threading module.
102
103 The func will be passed to sys.settrace() for each thread, before its run()
104 method is called.
105
106 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000107 global _trace_hook
108 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000109
110# Synchronization classes
111
112Lock = _allocate_lock
113
114def RLock(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700115 """Factory function that returns a new reentrant lock.
116
117 A reentrant lock must be released by the thread that acquired it. Once a
118 thread has acquired a reentrant lock, the same thread may acquire it again
119 without blocking; the thread must release it once for each time it has
120 acquired it.
121
122 """
Guido van Rossum68468eb2003-02-27 20:14:51 +0000123 return _RLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124
125class _RLock(_Verbose):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700126 """A reentrant lock must be released by the thread that acquired it. Once a
127 thread has acquired a reentrant lock, the same thread may acquire it
128 again without blocking; the thread must release it once for each time it
129 has acquired it.
130 """
Tim Petersb90f89a2001-01-15 03:26:36 +0000131
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000132 def __init__(self, verbose=None):
133 _Verbose.__init__(self, verbose)
134 self.__block = _allocate_lock()
135 self.__owner = None
136 self.__count = 0
137
138 def __repr__(self):
Nick Coghlanf8bbaa92007-07-31 13:38:01 +0000139 owner = self.__owner
Antoine Pitroud7158d42009-11-09 16:00:11 +0000140 try:
141 owner = _active[owner].name
142 except KeyError:
143 pass
144 return "<%s owner=%r count=%d>" % (
145 self.__class__.__name__, owner, self.__count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000146
147 def acquire(self, blocking=1):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700148 """Acquire a lock, blocking or non-blocking.
149
150 When invoked without arguments: if this thread already owns the lock,
151 increment the recursion level by one, and return immediately. Otherwise,
152 if another thread owns the lock, block until the lock is unlocked. Once
153 the lock is unlocked (not owned by any thread), then grab ownership, set
154 the recursion level to one, and return. If more than one thread is
155 blocked waiting until the lock is unlocked, only one at a time will be
156 able to grab ownership of the lock. There is no return value in this
157 case.
158
159 When invoked with the blocking argument set to true, do the same thing
160 as when called without arguments, and return true.
161
162 When invoked with the blocking argument set to false, do not block. If a
163 call without an argument would block, return false immediately;
164 otherwise, do the same thing as when called without arguments, and
165 return true.
166
167 """
Antoine Pitroud7158d42009-11-09 16:00:11 +0000168 me = _get_ident()
169 if self.__owner == me:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000170 self.__count = self.__count + 1
171 if __debug__:
172 self._note("%s.acquire(%s): recursive success", self, blocking)
173 return 1
174 rc = self.__block.acquire(blocking)
175 if rc:
176 self.__owner = me
177 self.__count = 1
178 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000179 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000180 else:
181 if __debug__:
182 self._note("%s.acquire(%s): failure", self, blocking)
183 return rc
184
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000185 __enter__ = acquire
186
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000187 def release(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700188 """Release a lock, decrementing the recursion level.
189
190 If after the decrement it is zero, reset the lock to unlocked (not owned
191 by any thread), and if any other threads are blocked waiting for the
192 lock to become unlocked, allow exactly one of them to proceed. If after
193 the decrement the recursion level is still nonzero, the lock remains
194 locked and owned by the calling thread.
195
196 Only call this method when the calling thread owns the lock. A
197 RuntimeError is raised if this method is called when the lock is
198 unlocked.
199
200 There is no return value.
201
202 """
Antoine Pitroud7158d42009-11-09 16:00:11 +0000203 if self.__owner != _get_ident():
Georg Brandle1254d72009-10-14 15:51:48 +0000204 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000205 self.__count = count = self.__count - 1
206 if not count:
207 self.__owner = None
208 self.__block.release()
209 if __debug__:
210 self._note("%s.release(): final release", self)
211 else:
212 if __debug__:
213 self._note("%s.release(): non-final release", self)
214
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000215 def __exit__(self, t, v, tb):
216 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000217
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000218 # Internal methods used by condition variables
219
Brett Cannon20050502008-08-02 03:13:46 +0000220 def _acquire_restore(self, count_owner):
221 count, owner = count_owner
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000222 self.__block.acquire()
223 self.__count = count
224 self.__owner = owner
225 if __debug__:
226 self._note("%s._acquire_restore()", self)
227
228 def _release_save(self):
229 if __debug__:
230 self._note("%s._release_save()", self)
231 count = self.__count
232 self.__count = 0
233 owner = self.__owner
234 self.__owner = None
235 self.__block.release()
236 return (count, owner)
237
238 def _is_owned(self):
Antoine Pitroud7158d42009-11-09 16:00:11 +0000239 return self.__owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240
241
242def Condition(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700243 """Factory function that returns a new condition variable object.
244
245 A condition variable allows one or more threads to wait until they are
246 notified by another thread.
247
248 If the lock argument is given and not None, it must be a Lock or RLock
249 object, and it is used as the underlying lock. Otherwise, a new RLock object
250 is created and used as the underlying lock.
251
252 """
Guido van Rossum68468eb2003-02-27 20:14:51 +0000253 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000254
255class _Condition(_Verbose):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700256 """Condition variables allow one or more threads to wait until they are
257 notified by another thread.
258 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000259
260 def __init__(self, lock=None, verbose=None):
261 _Verbose.__init__(self, verbose)
262 if lock is None:
263 lock = RLock()
264 self.__lock = lock
265 # Export the lock's acquire() and release() methods
266 self.acquire = lock.acquire
267 self.release = lock.release
268 # If the lock defines _release_save() and/or _acquire_restore(),
269 # these override the default implementations (which just call
270 # release() and acquire() on the lock). Ditto for _is_owned().
271 try:
272 self._release_save = lock._release_save
273 except AttributeError:
274 pass
275 try:
276 self._acquire_restore = lock._acquire_restore
277 except AttributeError:
278 pass
279 try:
280 self._is_owned = lock._is_owned
281 except AttributeError:
282 pass
283 self.__waiters = []
284
Guido van Rossumda5b7012006-05-02 19:47:52 +0000285 def __enter__(self):
286 return self.__lock.__enter__()
287
288 def __exit__(self, *args):
289 return self.__lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000290
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000291 def __repr__(self):
292 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
293
294 def _release_save(self):
295 self.__lock.release() # No state to save
296
297 def _acquire_restore(self, x):
298 self.__lock.acquire() # Ignore saved state
299
300 def _is_owned(self):
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000301 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000302 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000303 if self.__lock.acquire(0):
304 self.__lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000305 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000306 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000307 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308
309 def wait(self, timeout=None):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700310 """Wait until notified or until a timeout occurs.
311
312 If the calling thread has not acquired the lock when this method is
313 called, a RuntimeError is raised.
314
315 This method releases the underlying lock, and then blocks until it is
316 awakened by a notify() or notifyAll() call for the same condition
317 variable in another thread, or until the optional timeout occurs. Once
318 awakened or timed out, it re-acquires the lock and returns.
319
320 When the timeout argument is present and not None, it should be a
321 floating point number specifying a timeout for the operation in seconds
322 (or fractions thereof).
323
324 When the underlying lock is an RLock, it is not released using its
325 release() method, since this may not actually unlock the lock when it
326 was acquired multiple times recursively. Instead, an internal interface
327 of the RLock class is used, which really unlocks it even when it has
328 been recursively acquired several times. Another internal interface is
329 then used to restore the recursion level when the lock is reacquired.
330
331 """
Collin Winter50b79ce2007-06-06 00:17:35 +0000332 if not self._is_owned():
Georg Brandle1254d72009-10-14 15:51:48 +0000333 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000334 waiter = _allocate_lock()
335 waiter.acquire()
336 self.__waiters.append(waiter)
337 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000338 try: # restore state no matter what (e.g., KeyboardInterrupt)
339 if timeout is None:
340 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000341 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000342 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000343 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000344 # Balancing act: We can't afford a pure busy loop, so we
345 # have to sleep; but if we sleep the whole timeout time,
346 # we'll be unresponsive. The scheme here sleeps very
347 # little at first, longer as time goes on, but never longer
348 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000349 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000350 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000351 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000352 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000353 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000354 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000355 remaining = endtime - _time()
356 if remaining <= 0:
357 break
358 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000359 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000360 if not gotit:
361 if __debug__:
362 self._note("%s.wait(%s): timed out", self, timeout)
363 try:
364 self.__waiters.remove(waiter)
365 except ValueError:
366 pass
367 else:
368 if __debug__:
369 self._note("%s.wait(%s): got it", self, timeout)
370 finally:
371 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000372
373 def notify(self, n=1):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700374 """Wake up one or more threads waiting on this condition, if any.
375
376 If the calling thread has not acquired the lock when this method is
377 called, a RuntimeError is raised.
378
379 This method wakes up at most n of the threads waiting for the condition
380 variable; it is a no-op if no threads are waiting.
381
382 """
Collin Winter50b79ce2007-06-06 00:17:35 +0000383 if not self._is_owned():
Georg Brandle1254d72009-10-14 15:51:48 +0000384 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000385 __waiters = self.__waiters
386 waiters = __waiters[:n]
387 if not waiters:
388 if __debug__:
389 self._note("%s.notify(): no waiters", self)
390 return
391 self._note("%s.notify(): notifying %d waiter%s", self, n,
392 n!=1 and "s" or "")
393 for waiter in waiters:
394 waiter.release()
395 try:
396 __waiters.remove(waiter)
397 except ValueError:
398 pass
399
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000400 def notifyAll(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700401 """Wake up all threads waiting on this condition.
402
403 If the calling thread has not acquired the lock when this method
404 is called, a RuntimeError is raised.
405
406 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000407 self.notify(len(self.__waiters))
408
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000409 notify_all = notifyAll
Benjamin Petersonf4395602008-06-11 17:50:00 +0000410
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000411
412def Semaphore(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700413 """A factory function that returns a new semaphore.
414
415 Semaphores manage a counter representing the number of release() calls minus
416 the number of acquire() calls, plus an initial value. The acquire() method
417 blocks if necessary until it can return without making the counter
418 negative. If not given, value defaults to 1.
419
420 """
Guido van Rossum68468eb2003-02-27 20:14:51 +0000421 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000422
423class _Semaphore(_Verbose):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700424 """Semaphores manage a counter representing the number of release() calls
425 minus the number of acquire() calls, plus an initial value. The acquire()
426 method blocks if necessary until it can return without making the counter
427 negative. If not given, value defaults to 1.
428
429 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000430
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000431 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000432
433 def __init__(self, value=1, verbose=None):
Collin Winter50b79ce2007-06-06 00:17:35 +0000434 if value < 0:
435 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000436 _Verbose.__init__(self, verbose)
437 self.__cond = Condition(Lock())
438 self.__value = value
439
440 def acquire(self, blocking=1):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700441 """Acquire a semaphore, decrementing the internal counter by one.
442
443 When invoked without arguments: if the internal counter is larger than
444 zero on entry, decrement it by one and return immediately. If it is zero
445 on entry, block, waiting until some other thread has called release() to
446 make it larger than zero. This is done with proper interlocking so that
447 if multiple acquire() calls are blocked, release() will wake exactly one
448 of them up. The implementation may pick one at random, so the order in
449 which blocked threads are awakened should not be relied on. There is no
450 return value in this case.
451
452 When invoked with blocking set to true, do the same thing as when called
453 without arguments, and return true.
454
455 When invoked with blocking set to false, do not block. If a call without
456 an argument would block, return false immediately; otherwise, do the
457 same thing as when called without arguments, and return true.
458
459 """
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000460 rc = False
Serhiy Storchakad194b302013-04-22 22:51:00 +0300461 with self.__cond:
462 while self.__value == 0:
463 if not blocking:
464 break
465 if __debug__:
466 self._note("%s.acquire(%s): blocked waiting, value=%s",
467 self, blocking, self.__value)
468 self.__cond.wait()
469 else:
470 self.__value = self.__value - 1
471 if __debug__:
472 self._note("%s.acquire: success, value=%s",
473 self, self.__value)
474 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000475 return rc
476
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000477 __enter__ = acquire
478
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000479 def release(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700480 """Release a semaphore, incrementing the internal counter by one.
481
482 When the counter is zero on entry and another thread is waiting for it
483 to become larger than zero again, wake up that thread.
484
485 """
Serhiy Storchakad194b302013-04-22 22:51:00 +0300486 with self.__cond:
487 self.__value = self.__value + 1
488 if __debug__:
489 self._note("%s.release: success, value=%s",
490 self, self.__value)
491 self.__cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000492
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000493 def __exit__(self, t, v, tb):
494 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000495
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000496
Skip Montanaroe428bb72001-08-20 20:27:58 +0000497def BoundedSemaphore(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700498 """A factory function that returns a new bounded semaphore.
499
500 A bounded semaphore checks to make sure its current value doesn't exceed its
501 initial value. If it does, ValueError is raised. In most situations
502 semaphores are used to guard resources with limited capacity.
503
504 If the semaphore is released too many times it's a sign of a bug. If not
505 given, value defaults to 1.
506
507 Like regular semaphores, bounded semaphores manage a counter representing
508 the number of release() calls minus the number of acquire() calls, plus an
509 initial value. The acquire() method blocks if necessary until it can return
510 without making the counter negative. If not given, value defaults to 1.
511
512 """
Guido van Rossum68468eb2003-02-27 20:14:51 +0000513 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000514
515class _BoundedSemaphore(_Semaphore):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700516 """A bounded semaphore checks to make sure its current value doesn't exceed
517 its initial value. If it does, ValueError is raised. In most situations
518 semaphores are used to guard resources with limited capacity.
519 """
520
Skip Montanaroe428bb72001-08-20 20:27:58 +0000521 def __init__(self, value=1, verbose=None):
522 _Semaphore.__init__(self, value, verbose)
523 self._initial_value = value
524
525 def release(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700526 """Release a semaphore, incrementing the internal counter by one.
527
528 When the counter is zero on entry and another thread is waiting for it
529 to become larger than zero again, wake up that thread.
530
531 If the number of releases exceeds the number of acquires,
532 raise a ValueError.
533
534 """
Tim Petersb563d4b2013-10-08 21:51:06 -0500535 with self._Semaphore__cond:
536 if self._Semaphore__value >= self._initial_value:
Tim Peters641d6212013-10-08 20:55:51 -0500537 raise ValueError("Semaphore released too many times")
Tim Petersb563d4b2013-10-08 21:51:06 -0500538 self._Semaphore__value += 1
539 self._Semaphore__cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000540
541
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000542def Event(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700543 """A factory function that returns a new event.
544
545 Events manage a flag that can be set to true with the set() method and reset
546 to false with the clear() method. The wait() method blocks until the flag is
547 true.
548
549 """
Guido van Rossum68468eb2003-02-27 20:14:51 +0000550 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000551
552class _Event(_Verbose):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700553 """A factory function that returns a new event object. An event manages a
554 flag that can be set to true with the set() method and reset to false
555 with the clear() method. The wait() method blocks until the flag is true.
556
557 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000558
559 # After Tim Peters' event class (without is_posted())
560
561 def __init__(self, verbose=None):
562 _Verbose.__init__(self, verbose)
563 self.__cond = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000564 self.__flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000565
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000566 def _reset_internal_locks(self):
567 # private! called by Thread._reset_internal_locks by _after_fork()
Benjamin Peterson51cd53e2015-10-05 21:56:22 -0700568 self.__cond.__init__(Lock())
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000569
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000570 def isSet(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700571 'Return true if and only if the internal flag is true.'
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000572 return self.__flag
573
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000574 is_set = isSet
Benjamin Petersonf4395602008-06-11 17:50:00 +0000575
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000576 def set(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700577 """Set the internal flag to true.
578
579 All threads waiting for the flag to become true are awakened. Threads
580 that call wait() once the flag is true will not block at all.
581
582 """
Benjamin Peterson5f32b232015-10-10 19:34:46 -0700583 with self.__cond:
Guido van Rossum21b60142002-11-21 21:08:39 +0000584 self.__flag = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000585 self.__cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000586
587 def clear(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700588 """Reset the internal flag to false.
589
590 Subsequently, threads calling wait() will block until set() is called to
591 set the internal flag to true again.
592
593 """
Benjamin Peterson5f32b232015-10-10 19:34:46 -0700594 with self.__cond:
Guido van Rossum21b60142002-11-21 21:08:39 +0000595 self.__flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000596
597 def wait(self, timeout=None):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700598 """Block until the internal flag is true.
599
600 If the internal flag is true on entry, return immediately. Otherwise,
601 block until another thread calls set() to set the flag to true, or until
602 the optional timeout occurs.
603
604 When the timeout argument is present and not None, it should be a
605 floating point number specifying a timeout for the operation in seconds
606 (or fractions thereof).
607
608 This method returns the internal flag on exit, so it will always return
609 True except if a timeout is given and the operation times out.
610
611 """
Benjamin Peterson5f32b232015-10-10 19:34:46 -0700612 with self.__cond:
Guido van Rossum21b60142002-11-21 21:08:39 +0000613 if not self.__flag:
614 self.__cond.wait(timeout)
Georg Brandlef660e82009-03-31 20:41:08 +0000615 return self.__flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000616
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000617# Helper to generate new thread names
R David Murrayd999c342014-10-04 17:40:43 -0400618_counter = _count().next
619_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000620def _newname(template="Thread-%d"):
R David Murrayd999c342014-10-04 17:40:43 -0400621 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000622
623# Active thread administration
624_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000625_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000626_limbo = {}
627
628
629# Main class for threads
630
631class Thread(_Verbose):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700632 """A class that represents a thread of control.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000633
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700634 This class can be safely subclassed in a limited fashion.
635
636 """
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000637 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000638 # Need to store a reference to sys.exc_info for printing
639 # out exceptions when a thread tries to use a global var. during interp.
640 # shutdown and thus raises an exception about trying to perform some
641 # operation on/with a NoneType
642 __exc_info = _sys.exc_info
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000643 # Keep sys.exc_clear too to clear the exception just before
644 # allowing .join() to return.
645 __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000646
647 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000648 args=(), kwargs=None, verbose=None):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700649 """This constructor should always be called with keyword arguments. Arguments are:
650
651 *group* should be None; reserved for future extension when a ThreadGroup
652 class is implemented.
653
654 *target* is the callable object to be invoked by the run()
655 method. Defaults to None, meaning nothing is called.
656
657 *name* is the thread name. By default, a unique name is constructed of
658 the form "Thread-N" where N is a small decimal number.
659
660 *args* is the argument tuple for the target invocation. Defaults to ().
661
662 *kwargs* is a dictionary of keyword arguments for the target
663 invocation. Defaults to {}.
664
665 If a subclass overrides the constructor, it must make sure to invoke
666 the base class constructor (Thread.__init__()) before doing anything
667 else to the thread.
668
669"""
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000670 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000671 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000672 if kwargs is None:
673 kwargs = {}
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000674 self.__target = target
675 self.__name = str(name or _newname())
676 self.__args = args
677 self.__kwargs = kwargs
678 self.__daemonic = self._set_daemon()
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000679 self.__ident = None
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000680 self.__started = Event()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000681 self.__stopped = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000682 self.__block = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000683 self.__initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000684 # sys.stderr is not stored in the class like
685 # sys.exc_info since it can be changed between instances
686 self.__stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000687
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000688 def _reset_internal_locks(self):
689 # private! Called by _after_fork() to reset our internal locks as
690 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smithc8762022011-01-04 18:43:54 +0000691 if hasattr(self, '_Thread__block'): # DummyThread deletes self.__block
692 self.__block.__init__()
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000693 self.__started._reset_internal_locks()
694
695 @property
696 def _block(self):
697 # used by a unittest
698 return self.__block
699
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000700 def _set_daemon(self):
701 # Overridden in _MainThread and _DummyThread
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000702 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000703
704 def __repr__(self):
705 assert self.__initialized, "Thread.__init__() was not called"
706 status = "initial"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000707 if self.__started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000708 status = "started"
709 if self.__stopped:
710 status = "stopped"
711 if self.__daemonic:
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000712 status += " daemon"
713 if self.__ident is not None:
714 status += " %s" % self.__ident
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000715 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
716
717 def start(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700718 """Start the thread's activity.
719
720 It must be called at most once per thread object. It arranges for the
721 object's run() method to be invoked in a separate thread of control.
722
723 This method will raise a RuntimeError if called more than once on the
724 same thread object.
725
726 """
Collin Winter50b79ce2007-06-06 00:17:35 +0000727 if not self.__initialized:
728 raise RuntimeError("thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000729 if self.__started.is_set():
Senthil Kumaranb02b3112010-04-06 03:23:33 +0000730 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731 if __debug__:
732 self._note("%s.start(): starting thread", self)
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000733 with _active_limbo_lock:
734 _limbo[self] = self
Gregory P. Smith613c7a52010-02-28 18:36:09 +0000735 try:
736 _start_new_thread(self.__bootstrap, ())
737 except Exception:
738 with _active_limbo_lock:
739 del _limbo[self]
740 raise
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000741 self.__started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000742
743 def run(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700744 """Method representing the thread's activity.
745
746 You may override this method in a subclass. The standard run() method
747 invokes the callable object passed to the object's constructor as the
748 target argument, if any, with sequential and keyword arguments taken
749 from the args and kwargs arguments, respectively.
750
751 """
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000752 try:
753 if self.__target:
754 self.__target(*self.__args, **self.__kwargs)
755 finally:
756 # Avoid a refcycle if the thread is running a function with
757 # an argument that has a member that points to the thread.
758 del self.__target, self.__args, self.__kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759
760 def __bootstrap(self):
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000761 # Wrapper around the real bootstrap code that ignores
762 # exceptions during interpreter cleanup. Those typically
763 # happen when a daemon thread wakes up at an unfortunate
764 # moment, finds the world around it destroyed, and raises some
765 # random exception *** while trying to report the exception in
766 # __bootstrap_inner() below ***. Those random exceptions
767 # don't help anybody, and they confuse users, so we suppress
768 # them. We suppress them only when it appears that the world
769 # indeed has already been destroyed, so that exceptions in
770 # __bootstrap_inner() during normal business hours are properly
771 # reported. Also, we only suppress them for daemonic threads;
772 # if a non-daemonic encounters this, something else is wrong.
773 try:
774 self.__bootstrap_inner()
775 except:
776 if self.__daemonic and _sys is None:
777 return
778 raise
779
Benjamin Petersond906ea62009-03-31 21:34:42 +0000780 def _set_ident(self):
781 self.__ident = _get_ident()
782
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000783 def __bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000784 try:
Benjamin Petersond906ea62009-03-31 21:34:42 +0000785 self._set_ident()
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000786 self.__started.set()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000787 with _active_limbo_lock:
788 _active[self.__ident] = self
789 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000790 if __debug__:
791 self._note("%s.__bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000792
793 if _trace_hook:
794 self._note("%s.__bootstrap(): registering trace hook", self)
795 _sys.settrace(_trace_hook)
796 if _profile_hook:
797 self._note("%s.__bootstrap(): registering profile hook", self)
798 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000799
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000800 try:
801 self.run()
802 except SystemExit:
803 if __debug__:
804 self._note("%s.__bootstrap(): raised SystemExit", self)
805 except:
806 if __debug__:
807 self._note("%s.__bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000808 # If sys.stderr is no more (most likely from interpreter
809 # shutdown) use self.__stderr. Otherwise still use sys (as in
810 # _sys) in case sys.stderr was redefined since the creation of
811 # self.
Serhiy Storchaka91943462014-09-21 22:08:00 +0300812 if _sys and _sys.stderr is not None:
813 print>>_sys.stderr, ("Exception in thread %s:\n%s" %
814 (self.name, _format_exc()))
815 elif self.__stderr is not None:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000816 # Do the best job possible w/o a huge amt. of code to
817 # approximate a traceback (code ideas from
818 # Lib/traceback.py)
819 exc_type, exc_value, exc_tb = self.__exc_info()
820 try:
821 print>>self.__stderr, (
Benjamin Petersonb6a95562008-08-22 20:43:48 +0000822 "Exception in thread " + self.name +
Brett Cannoncc4e9352004-07-03 03:52:35 +0000823 " (most likely raised during interpreter shutdown):")
824 print>>self.__stderr, (
825 "Traceback (most recent call last):")
826 while exc_tb:
827 print>>self.__stderr, (
828 ' File "%s", line %s, in %s' %
829 (exc_tb.tb_frame.f_code.co_filename,
830 exc_tb.tb_lineno,
831 exc_tb.tb_frame.f_code.co_name))
832 exc_tb = exc_tb.tb_next
833 print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
834 # Make sure that exc_tb gets deleted since it is a memory
835 # hog; deleting everything else is just for thoroughness
836 finally:
837 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838 else:
839 if __debug__:
840 self._note("%s.__bootstrap(): normal return", self)
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000841 finally:
842 # Prevent a race in
843 # test_threading.test_no_refcycle_through_target when
844 # the exception keeps the target alive past when we
845 # assert that it's dead.
Amaury Forgeot d'Arc504a48f2008-03-29 01:41:08 +0000846 self.__exc_clear()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847 finally:
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000848 with _active_limbo_lock:
849 self.__stop()
850 try:
851 # We don't call self.__delete() because it also
852 # grabs _active_limbo_lock.
853 del _active[_get_ident()]
854 except:
855 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856
857 def __stop(self):
Antoine Pitrou52849bf2012-04-19 23:55:01 +0200858 # DummyThreads delete self.__block, but they have no waiters to
859 # notify anyway (join() is forbidden on them).
860 if not hasattr(self, '_Thread__block'):
861 return
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000862 self.__block.acquire()
863 self.__stopped = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000864 self.__block.notify_all()
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000865 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000866
867 def __delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000868 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000869
Tim Peters21429932004-07-21 03:36:52 +0000870 # Notes about running with dummy_thread:
871 #
872 # Must take care to not raise an exception if dummy_thread is being
873 # used (and thus this module is being used as an instance of
874 # dummy_threading). dummy_thread.get_ident() always returns -1 since
875 # there is only one thread if dummy_thread is being used. Thus
876 # len(_active) is always <= 1 here, and any Thread instance created
877 # overwrites the (if any) thread currently registered in _active.
878 #
879 # An instance of _MainThread is always created by 'threading'. This
880 # gets overwritten the instant an instance of Thread is created; both
881 # threads return -1 from dummy_thread.get_ident() and thus have the
882 # same key in the dict. So when the _MainThread instance created by
883 # 'threading' tries to clean itself up when atexit calls this method
884 # it gets a KeyError if another Thread instance was created.
885 #
886 # This all means that KeyError from trying to delete something from
887 # _active if dummy_threading is being used is a red herring. But
888 # since it isn't if dummy_threading is *not* being used then don't
889 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000890
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000891 try:
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000892 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000893 del _active[_get_ident()]
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000894 # There must not be any python code between the previous line
895 # and after the lock is released. Otherwise a tracing function
896 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000897 # current_thread()), and would block.
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000898 except KeyError:
899 if 'dummy_threading' not in _sys.modules:
900 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000901
902 def join(self, timeout=None):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700903 """Wait until the thread terminates.
904
905 This blocks the calling thread until the thread whose join() method is
906 called terminates -- either normally or through an unhandled exception
907 or until the optional timeout occurs.
908
909 When the timeout argument is present and not None, it should be a
910 floating point number specifying a timeout for the operation in seconds
911 (or fractions thereof). As join() always returns None, you must call
912 isAlive() after join() to decide whether a timeout happened -- if the
913 thread is still alive, the join() call timed out.
914
915 When the timeout argument is not present or None, the operation will
916 block until the thread terminates.
917
918 A thread can be join()ed many times.
919
920 join() raises a RuntimeError if an attempt is made to join the current
921 thread as that would cause a deadlock. It is also an error to join() a
922 thread before it has been started and attempts to do so raises the same
923 exception.
924
925 """
Collin Winter50b79ce2007-06-06 00:17:35 +0000926 if not self.__initialized:
927 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000928 if not self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +0000929 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000930 if self is current_thread():
Collin Winter50b79ce2007-06-06 00:17:35 +0000931 raise RuntimeError("cannot join current thread")
932
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000933 if __debug__:
934 if not self.__stopped:
935 self._note("%s.join(): waiting until thread stops", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000936 self.__block.acquire()
937 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000938 if timeout is None:
939 while not self.__stopped:
940 self.__block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000941 if __debug__:
942 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000943 else:
944 deadline = _time() + timeout
945 while not self.__stopped:
946 delay = deadline - _time()
947 if delay <= 0:
948 if __debug__:
949 self._note("%s.join(): timed out", self)
950 break
951 self.__block.wait(delay)
952 else:
953 if __debug__:
954 self._note("%s.join(): thread stopped", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000955 finally:
956 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000957
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000958 @property
959 def name(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700960 """A string used for identification purposes only.
961
962 It has no semantics. Multiple threads may be given the same name. The
963 initial name is set by the constructor.
964
965 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000966 assert self.__initialized, "Thread.__init__() not called"
967 return self.__name
968
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000969 @name.setter
970 def name(self, name):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000971 assert self.__initialized, "Thread.__init__() not called"
972 self.__name = str(name)
973
Benjamin Petersond8a89722008-08-18 16:40:03 +0000974 @property
975 def ident(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700976 """Thread identifier of this thread or None if it has not been started.
977
978 This is a nonzero integer. See the thread.get_ident() function. Thread
979 identifiers may be recycled when a thread exits and another thread is
980 created. The identifier is available even after the thread has exited.
981
982 """
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000983 assert self.__initialized, "Thread.__init__() not called"
984 return self.__ident
985
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000986 def isAlive(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -0700987 """Return whether the thread is alive.
988
989 This method returns True just before the run() method starts until just
990 after the run() method terminates. The module function enumerate()
991 returns a list of all alive threads.
992
993 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000994 assert self.__initialized, "Thread.__init__() not called"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000995 return self.__started.is_set() and not self.__stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000996
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000997 is_alive = isAlive
Benjamin Peterson6ee1a312008-08-18 21:53:29 +0000998
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000999 @property
1000 def daemon(self):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001001 """A boolean value indicating whether this thread is a daemon thread (True) or not (False).
1002
1003 This must be set before start() is called, otherwise RuntimeError is
1004 raised. Its initial value is inherited from the creating thread; the
1005 main thread is not a daemon thread and therefore all threads created in
1006 the main thread default to daemon = False.
1007
1008 The entire Python program exits when no alive non-daemon threads are
1009 left.
1010
1011 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001012 assert self.__initialized, "Thread.__init__() not called"
1013 return self.__daemonic
1014
Benjamin Petersoncbae8692008-08-18 17:45:09 +00001015 @daemon.setter
1016 def daemon(self, daemonic):
Collin Winter50b79ce2007-06-06 00:17:35 +00001017 if not self.__initialized:
1018 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +00001019 if self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +00001020 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001021 self.__daemonic = daemonic
1022
Benjamin Petersond8106262008-08-18 18:13:17 +00001023 def isDaemon(self):
1024 return self.daemon
1025
1026 def setDaemon(self, daemonic):
1027 self.daemon = daemonic
1028
1029 def getName(self):
1030 return self.name
1031
1032 def setName(self, name):
1033 self.name = name
1034
Martin v. Löwis44f86962001-09-05 13:44:54 +00001035# The timer class was contributed by Itamar Shtull-Trauring
1036
1037def Timer(*args, **kwargs):
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001038 """Factory function to create a Timer object.
1039
1040 Timers call a function after a specified number of seconds:
1041
1042 t = Timer(30.0, f, args=[], kwargs={})
1043 t.start()
1044 t.cancel() # stop the timer's action if it's still waiting
1045
1046 """
Martin v. Löwis44f86962001-09-05 13:44:54 +00001047 return _Timer(*args, **kwargs)
1048
1049class _Timer(Thread):
1050 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001051
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001052 t = Timer(30.0, f, args=[], kwargs={})
1053 t.start()
1054 t.cancel() # stop the timer's action if it's still waiting
1055
Martin v. Löwis44f86962001-09-05 13:44:54 +00001056 """
Tim Petersb64bec32001-09-18 02:26:39 +00001057
Martin v. Löwis44f86962001-09-05 13:44:54 +00001058 def __init__(self, interval, function, args=[], kwargs={}):
1059 Thread.__init__(self)
1060 self.interval = interval
1061 self.function = function
1062 self.args = args
1063 self.kwargs = kwargs
1064 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001065
Martin v. Löwis44f86962001-09-05 13:44:54 +00001066 def cancel(self):
1067 """Stop the timer if it hasn't finished yet"""
1068 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001069
Martin v. Löwis44f86962001-09-05 13:44:54 +00001070 def run(self):
1071 self.finished.wait(self.interval)
Benjamin Peterson0fbcf692008-06-11 17:27:50 +00001072 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001073 self.function(*self.args, **self.kwargs)
1074 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001075
1076# Special thread class to represent the main thread
1077# This is garbage collected through an exit handler
1078
1079class _MainThread(Thread):
1080
1081 def __init__(self):
1082 Thread.__init__(self, name="MainThread")
Jeffrey Yasskin69e13092008-02-28 06:09:19 +00001083 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +00001084 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +00001085 with _active_limbo_lock:
1086 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001087
1088 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001089 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001090
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +00001091 def _exitfunc(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001092 self._Thread__stop()
1093 t = _pickSomeNonDaemonThread()
1094 if t:
1095 if __debug__:
1096 self._note("%s: waiting for other threads", self)
1097 while t:
1098 t.join()
1099 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001100 if __debug__:
1101 self._note("%s: exiting", self)
1102 self._Thread__delete()
1103
1104def _pickSomeNonDaemonThread():
1105 for t in enumerate():
Benjamin Petersoncbae8692008-08-18 17:45:09 +00001106 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001107 return t
1108 return None
1109
1110
1111# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001112# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +00001113# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001114# leave an entry in the _active dict forever after.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +00001115# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001116# They are marked as daemon threads so we won't wait for them
1117# when we exit (conform previous semantics).
1118
1119class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001120
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001121 def __init__(self):
1122 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +00001123
1124 # Thread.__block consumes an OS-level locking primitive, which
1125 # can never be used by a _DummyThread. Since a _DummyThread
1126 # instance is immortal, that's bad, so release this resource.
Brett Cannone6539c42005-01-08 02:43:53 +00001127 del self._Thread__block
Tim Peters711906e2005-01-08 07:30:42 +00001128
Jeffrey Yasskin69e13092008-02-28 06:09:19 +00001129 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +00001130 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +00001131 with _active_limbo_lock:
1132 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001133
1134 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001135 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001136
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001137 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001138 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001139
1140
1141# Global API functions
1142
Benjamin Peterson973e6c22008-09-01 23:12:58 +00001143def currentThread():
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001144 """Return the current Thread object, corresponding to the caller's thread of control.
1145
1146 If the caller's thread of control was not created through the threading
1147 module, a dummy thread object with limited functionality is returned.
1148
1149 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001150 try:
1151 return _active[_get_ident()]
1152 except KeyError:
Benjamin Peterson0fbcf692008-06-11 17:27:50 +00001153 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001154 return _DummyThread()
1155
Benjamin Peterson973e6c22008-09-01 23:12:58 +00001156current_thread = currentThread
Benjamin Petersonf4395602008-06-11 17:50:00 +00001157
Benjamin Peterson973e6c22008-09-01 23:12:58 +00001158def activeCount():
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001159 """Return the number of Thread objects currently alive.
1160
1161 The returned count is equal to the length of the list returned by
1162 enumerate().
1163
1164 """
Benjamin Petersonbd9dd312009-03-31 21:06:30 +00001165 with _active_limbo_lock:
1166 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001167
Benjamin Peterson973e6c22008-09-01 23:12:58 +00001168active_count = activeCount
Benjamin Petersonf4395602008-06-11 17:50:00 +00001169
Antoine Pitrou99c160b2009-11-05 13:42:29 +00001170def _enumerate():
1171 # Same as enumerate(), but without the lock. Internal use only.
1172 return _active.values() + _limbo.values()
1173
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001174def enumerate():
Raymond Hettinger9f7e2472013-03-08 21:02:13 -07001175 """Return a list of all Thread objects currently alive.
1176
1177 The list includes daemonic threads, dummy thread objects created by
1178 current_thread(), and the main thread. It excludes terminated threads and
1179 threads that have not yet been started.
1180
1181 """
Benjamin Petersonbd9dd312009-03-31 21:06:30 +00001182 with _active_limbo_lock:
1183 return _active.values() + _limbo.values()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001184
Andrew MacIntyre92913322006-06-13 15:04:24 +00001185from thread import stack_size
1186
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +00001187# Create the main thread object,
1188# and make it available for the interpreter
1189# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001190
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +00001191_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001192
Jim Fultond15dc062004-07-14 19:11:50 +00001193# get thread-local implementation, either from the thread
1194# module, or from the python fallback
1195
1196try:
1197 from thread import _local as local
1198except ImportError:
1199 from _threading_local import local
1200
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001201
Jesse Noller5e62ca42008-07-16 20:03:47 +00001202def _after_fork():
1203 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1204 # is called from PyOS_AfterFork. Here we cleanup threading module state
1205 # that should not exist after a fork.
1206
1207 # Reset _active_limbo_lock, in case we forked while the lock was held
1208 # by another (non-forked) thread. http://bugs.python.org/issue874900
1209 global _active_limbo_lock
1210 _active_limbo_lock = _allocate_lock()
1211
1212 # fork() only copied the current thread; clear references to others.
1213 new_active = {}
1214 current = current_thread()
1215 with _active_limbo_lock:
Charles-François Natali30a54452013-08-30 23:30:50 +02001216 for thread in _enumerate():
Charles-François Natali41616302011-12-18 18:22:24 +01001217 # Any lock/condition variable may be currently locked or in an
1218 # invalid state, so we reinitialize them.
1219 if hasattr(thread, '_reset_internal_locks'):
1220 thread._reset_internal_locks()
Jesse Noller5e62ca42008-07-16 20:03:47 +00001221 if thread is current:
Antoine Pitrou9fb1aca2008-09-06 23:04:32 +00001222 # There is only one active thread. We reset the ident to
1223 # its new value since it can have changed.
1224 ident = _get_ident()
1225 thread._Thread__ident = ident
Jesse Noller5e62ca42008-07-16 20:03:47 +00001226 new_active[ident] = thread
1227 else:
1228 # All the others are already stopped.
Charles-François Natali41616302011-12-18 18:22:24 +01001229 thread._Thread__stop()
Jesse Noller5e62ca42008-07-16 20:03:47 +00001230
1231 _limbo.clear()
1232 _active.clear()
1233 _active.update(new_active)
1234 assert len(_active) == 1
1235
1236
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001237# Self-test code
1238
1239def _test():
1240
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001241 class BoundedQueue(_Verbose):
1242
1243 def __init__(self, limit):
1244 _Verbose.__init__(self)
1245 self.mon = RLock()
1246 self.rc = Condition(self.mon)
1247 self.wc = Condition(self.mon)
1248 self.limit = limit
Brian Curtina9391052012-08-09 23:04:42 -05001249 self.queue = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001250
1251 def put(self, item):
1252 self.mon.acquire()
1253 while len(self.queue) >= self.limit:
1254 self._note("put(%s): queue full", item)
1255 self.wc.wait()
1256 self.queue.append(item)
1257 self._note("put(%s): appended, length now %d",
1258 item, len(self.queue))
1259 self.rc.notify()
1260 self.mon.release()
1261
1262 def get(self):
1263 self.mon.acquire()
1264 while not self.queue:
1265 self._note("get(): queue empty")
1266 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001267 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001268 self._note("get(): got %s, %d left", item, len(self.queue))
1269 self.wc.notify()
1270 self.mon.release()
1271 return item
1272
1273 class ProducerThread(Thread):
1274
1275 def __init__(self, queue, quota):
1276 Thread.__init__(self, name="Producer")
1277 self.queue = queue
1278 self.quota = quota
1279
1280 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +00001281 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001282 counter = 0
1283 while counter < self.quota:
1284 counter = counter + 1
Benjamin Petersoncbae8692008-08-18 17:45:09 +00001285 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001286 _sleep(random() * 0.00001)
1287
1288
1289 class ConsumerThread(Thread):
1290
1291 def __init__(self, queue, count):
1292 Thread.__init__(self, name="Consumer")
1293 self.queue = queue
1294 self.count = count
1295
1296 def run(self):
1297 while self.count > 0:
1298 item = self.queue.get()
1299 print item
1300 self.count = self.count - 1
1301
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001302 NP = 3
1303 QL = 4
1304 NI = 5
1305
1306 Q = BoundedQueue(QL)
1307 P = []
1308 for i in range(NP):
1309 t = ProducerThread(Q, NI)
Benjamin Petersoncbae8692008-08-18 17:45:09 +00001310 t.name = ("Producer-%d" % (i+1))
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001311 P.append(t)
1312 C = ConsumerThread(Q, NI*NP)
1313 for t in P:
1314 t.start()
1315 _sleep(0.000001)
1316 C.start()
1317 for t in P:
1318 t.join()
1319 C.join()
1320
1321if __name__ == '__main__':
1322 _test()