blob: 625c9b9d7b7590898635c770f7c779782573b975 [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
Victor Stinnerec895392012-04-29 02:41:27 +02006from time import sleep as _sleep
7try:
8 from time import monotonic as _time
9except ImportError:
10 from time import time as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +000011from traceback import format_exc as _format_exc
Antoine Pitrouc081c0c2011-07-15 22:12:24 +020012from _weakrefset import WeakSet
Guido van Rossum7f5013a1998-04-09 22:01:42 +000013
Benjamin Petersonb3085c92008-09-01 23:09:31 +000014# Note regarding PEP 8 compliant names
15# This threading model was originally inspired by Java, and inherited
16# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030017# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000018# being deprecated (even for Py3k),so this module provides them as an
19# alias for the PEP 8 compliant names
20# Note that using the new PEP 8 compliant names facilitates substitution
21# with the multiprocessing module, which doesn't provide the old
22# Java inspired names.
23
Benjamin Peterson672b8032008-06-11 19:14:14 +000024__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000025 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050026 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000027
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000028# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000029_start_new_thread = _thread.start_new_thread
30_allocate_lock = _thread.allocate_lock
Victor Stinner2a129742011-05-30 23:02:52 +020031get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000032ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000033try:
34 _CRLock = _thread.RLock
35except AttributeError:
36 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000037TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000038del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000039
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000041# Support for profile and trace hooks
42
43_profile_hook = None
44_trace_hook = None
45
46def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020047 """Set a profile function for all threads started from the threading module.
48
49 The func will be passed to sys.setprofile() for each thread, before its
50 run() method is called.
51
52 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000053 global _profile_hook
54 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000055
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000056def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020057 """Set a trace function for all threads started from the threading module.
58
59 The func will be passed to sys.settrace() for each thread, before its run()
60 method is called.
61
62 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000063 global _trace_hook
64 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000065
66# Synchronization classes
67
68Lock = _allocate_lock
69
Victor Stinner135b6d82012-03-03 01:32:57 +010070def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020071 """Factory function that returns a new reentrant lock.
72
73 A reentrant lock must be released by the thread that acquired it. Once a
74 thread has acquired a reentrant lock, the same thread may acquire it again
75 without blocking; the thread must release it once for each time it has
76 acquired it.
77
78 """
Victor Stinner135b6d82012-03-03 01:32:57 +010079 if _CRLock is None:
80 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000081 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000082
Victor Stinner135b6d82012-03-03 01:32:57 +010083class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020084 """This class implements reentrant lock objects.
85
86 A reentrant lock must be released by the thread that acquired it. Once a
87 thread has acquired a reentrant lock, the same thread may acquire it
88 again without blocking; the thread must release it once for each time it
89 has acquired it.
90
91 """
Tim Petersb90f89a2001-01-15 03:26:36 +000092
Victor Stinner135b6d82012-03-03 01:32:57 +010093 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000094 self._block = _allocate_lock()
95 self._owner = None
96 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000097
98 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000099 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000100 try:
101 owner = _active[owner].name
102 except KeyError:
103 pass
104 return "<%s owner=%r count=%d>" % (
105 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000106
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000107 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200108 """Acquire a lock, blocking or non-blocking.
109
110 When invoked without arguments: if this thread already owns the lock,
111 increment the recursion level by one, and return immediately. Otherwise,
112 if another thread owns the lock, block until the lock is unlocked. Once
113 the lock is unlocked (not owned by any thread), then grab ownership, set
114 the recursion level to one, and return. If more than one thread is
115 blocked waiting until the lock is unlocked, only one at a time will be
116 able to grab ownership of the lock. There is no return value in this
117 case.
118
119 When invoked with the blocking argument set to true, do the same thing
120 as when called without arguments, and return true.
121
122 When invoked with the blocking argument set to false, do not block. If a
123 call without an argument would block, return false immediately;
124 otherwise, do the same thing as when called without arguments, and
125 return true.
126
127 When invoked with the floating-point timeout argument set to a positive
128 value, block for at most the number of seconds specified by timeout
129 and as long as the lock cannot be acquired. Return true if the lock has
130 been acquired, false if the timeout has elapsed.
131
132 """
Victor Stinner2a129742011-05-30 23:02:52 +0200133 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000134 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000135 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000136 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000137 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000138 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000139 self._owner = me
140 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000141 return rc
142
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000143 __enter__ = acquire
144
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000145 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200146 """Release a lock, decrementing the recursion level.
147
148 If after the decrement it is zero, reset the lock to unlocked (not owned
149 by any thread), and if any other threads are blocked waiting for the
150 lock to become unlocked, allow exactly one of them to proceed. If after
151 the decrement the recursion level is still nonzero, the lock remains
152 locked and owned by the calling thread.
153
154 Only call this method when the calling thread owns the lock. A
155 RuntimeError is raised if this method is called when the lock is
156 unlocked.
157
158 There is no return value.
159
160 """
Victor Stinner2a129742011-05-30 23:02:52 +0200161 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000162 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000163 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000164 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000165 self._owner = None
166 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000167
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000168 def __exit__(self, t, v, tb):
169 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000170
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000171 # Internal methods used by condition variables
172
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000173 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000174 self._block.acquire()
175 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176
177 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200178 if self._count == 0:
179 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000180 count = self._count
181 self._count = 0
182 owner = self._owner
183 self._owner = None
184 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000185 return (count, owner)
186
187 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200188 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000189
Antoine Pitrou434736a2009-11-10 18:46:01 +0000190_PyRLock = _RLock
191
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000192
Victor Stinner135b6d82012-03-03 01:32:57 +0100193class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200194 """Class that implements a condition variable.
195
196 A condition variable allows one or more threads to wait until they are
197 notified by another thread.
198
199 If the lock argument is given and not None, it must be a Lock or RLock
200 object, and it is used as the underlying lock. Otherwise, a new RLock object
201 is created and used as the underlying lock.
202
203 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
Victor Stinner135b6d82012-03-03 01:32:57 +0100205 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000206 if lock is None:
207 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000208 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000209 # Export the lock's acquire() and release() methods
210 self.acquire = lock.acquire
211 self.release = lock.release
212 # If the lock defines _release_save() and/or _acquire_restore(),
213 # these override the default implementations (which just call
214 # release() and acquire() on the lock). Ditto for _is_owned().
215 try:
216 self._release_save = lock._release_save
217 except AttributeError:
218 pass
219 try:
220 self._acquire_restore = lock._acquire_restore
221 except AttributeError:
222 pass
223 try:
224 self._is_owned = lock._is_owned
225 except AttributeError:
226 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000227 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228
Thomas Wouters477c8d52006-05-27 19:21:47 +0000229 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000230 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000231
Thomas Wouters477c8d52006-05-27 19:21:47 +0000232 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000233 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000234
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000236 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237
238 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000239 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240
241 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000242 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000243
244 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000245 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000246 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 if self._lock.acquire(0):
248 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000249 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000250 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000251 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000252
253 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200254 """Wait until notified or until a timeout occurs.
255
256 If the calling thread has not acquired the lock when this method is
257 called, a RuntimeError is raised.
258
259 This method releases the underlying lock, and then blocks until it is
260 awakened by a notify() or notify_all() call for the same condition
261 variable in another thread, or until the optional timeout occurs. Once
262 awakened or timed out, it re-acquires the lock and returns.
263
264 When the timeout argument is present and not None, it should be a
265 floating point number specifying a timeout for the operation in seconds
266 (or fractions thereof).
267
268 When the underlying lock is an RLock, it is not released using its
269 release() method, since this may not actually unlock the lock when it
270 was acquired multiple times recursively. Instead, an internal interface
271 of the RLock class is used, which really unlocks it even when it has
272 been recursively acquired several times. Another internal interface is
273 then used to restore the recursion level when the lock is reacquired.
274
275 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000276 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000277 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000278 waiter = _allocate_lock()
279 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000280 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000281 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000282 try: # restore state no matter what (e.g., KeyboardInterrupt)
283 if timeout is None:
284 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000285 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000287 if timeout > 0:
288 gotit = waiter.acquire(True, timeout)
289 else:
290 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000291 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000292 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000293 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000294 except ValueError:
295 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000296 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000297 finally:
298 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000299
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000300 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200301 """Wait until a condition evaluates to True.
302
303 predicate should be a callable which result will be interpreted as a
304 boolean value. A timeout may be provided giving the maximum time to
305 wait.
306
307 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000308 endtime = None
309 waittime = timeout
310 result = predicate()
311 while not result:
312 if waittime is not None:
313 if endtime is None:
314 endtime = _time() + waittime
315 else:
316 waittime = endtime - _time()
317 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000318 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000319 self.wait(waittime)
320 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000321 return result
322
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000323 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200324 """Wake up one or more threads waiting on this condition, if any.
325
326 If the calling thread has not acquired the lock when this method is
327 called, a RuntimeError is raised.
328
329 This method wakes up at most n of the threads waiting for the condition
330 variable; it is a no-op if no threads are waiting.
331
332 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000333 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000334 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000335 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000336 waiters = __waiters[:n]
337 if not waiters:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000338 return
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000339 for waiter in waiters:
340 waiter.release()
341 try:
342 __waiters.remove(waiter)
343 except ValueError:
344 pass
345
Benjamin Peterson672b8032008-06-11 19:14:14 +0000346 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200347 """Wake up all threads waiting on this condition.
348
349 If the calling thread has not acquired the lock when this method
350 is called, a RuntimeError is raised.
351
352 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000353 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000354
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000355 notifyAll = notify_all
356
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000357
Victor Stinner135b6d82012-03-03 01:32:57 +0100358class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200359 """This class implements semaphore objects.
360
361 Semaphores manage a counter representing the number of release() calls minus
362 the number of acquire() calls, plus an initial value. The acquire() method
363 blocks if necessary until it can return without making the counter
364 negative. If not given, value defaults to 1.
365
366 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000367
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000368 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000369
Victor Stinner135b6d82012-03-03 01:32:57 +0100370 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000371 if value < 0:
372 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000373 self._cond = Condition(Lock())
374 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
Antoine Pitrou0454af92010-04-17 23:51:58 +0000376 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200377 """Acquire a semaphore, decrementing the internal counter by one.
378
379 When invoked without arguments: if the internal counter is larger than
380 zero on entry, decrement it by one and return immediately. If it is zero
381 on entry, block, waiting until some other thread has called release() to
382 make it larger than zero. This is done with proper interlocking so that
383 if multiple acquire() calls are blocked, release() will wake exactly one
384 of them up. The implementation may pick one at random, so the order in
385 which blocked threads are awakened should not be relied on. There is no
386 return value in this case.
387
388 When invoked with blocking set to true, do the same thing as when called
389 without arguments, and return true.
390
391 When invoked with blocking set to false, do not block. If a call without
392 an argument would block, return false immediately; otherwise, do the
393 same thing as when called without arguments, and return true.
394
395 When invoked with a timeout other than None, it will block for at
396 most timeout seconds. If acquire does not complete successfully in
397 that interval, return false. Return true otherwise.
398
399 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000400 if not blocking and timeout is not None:
401 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000402 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000403 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300404 with self._cond:
405 while self._value == 0:
406 if not blocking:
407 break
408 if timeout is not None:
409 if endtime is None:
410 endtime = _time() + timeout
411 else:
412 timeout = endtime - _time()
413 if timeout <= 0:
414 break
415 self._cond.wait(timeout)
416 else:
417 self._value = self._value - 1
418 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000419 return rc
420
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000421 __enter__ = acquire
422
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000423 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200424 """Release a semaphore, incrementing the internal counter by one.
425
426 When the counter is zero on entry and another thread is waiting for it
427 to become larger than zero again, wake up that thread.
428
429 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300430 with self._cond:
431 self._value = self._value + 1
432 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000433
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000434 def __exit__(self, t, v, tb):
435 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000436
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000437
Éric Araujo0cdd4452011-07-28 00:28:28 +0200438class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200439 """Implements a bounded semaphore.
440
441 A bounded semaphore checks to make sure its current value doesn't exceed its
442 initial value. If it does, ValueError is raised. In most situations
443 semaphores are used to guard resources with limited capacity.
444
445 If the semaphore is released too many times it's a sign of a bug. If not
446 given, value defaults to 1.
447
448 Like regular semaphores, bounded semaphores manage a counter representing
449 the number of release() calls minus the number of acquire() calls, plus an
450 initial value. The acquire() method blocks if necessary until it can return
451 without making the counter negative. If not given, value defaults to 1.
452
453 """
454
Victor Stinner135b6d82012-03-03 01:32:57 +0100455 def __init__(self, value=1):
456 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000457 self._initial_value = value
458
459 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200460 """Release a semaphore, incrementing the internal counter by one.
461
462 When the counter is zero on entry and another thread is waiting for it
463 to become larger than zero again, wake up that thread.
464
465 If the number of releases exceeds the number of acquires,
466 raise a ValueError.
467
468 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500469 with self._cond:
470 if self._value >= self._initial_value:
471 raise ValueError("Semaphore released too many times")
472 self._value += 1
473 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000474
475
Victor Stinner135b6d82012-03-03 01:32:57 +0100476class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200477 """Class implementing event objects.
478
479 Events manage a flag that can be set to true with the set() method and reset
480 to false with the clear() method. The wait() method blocks until the flag is
481 true. The flag is initially false.
482
483 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000484
485 # After Tim Peters' event class (without is_posted())
486
Victor Stinner135b6d82012-03-03 01:32:57 +0100487 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000488 self._cond = Condition(Lock())
489 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000490
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000491 def _reset_internal_locks(self):
492 # private! called by Thread._reset_internal_locks by _after_fork()
493 self._cond.__init__()
494
Benjamin Peterson672b8032008-06-11 19:14:14 +0000495 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200496 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000497 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000498
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000499 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000500
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000501 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200502 """Set the internal flag to true.
503
504 All threads waiting for it to become true are awakened. Threads
505 that call wait() once the flag is true will not block at all.
506
507 """
Christian Heimes969fe572008-01-25 11:23:10 +0000508 self._cond.acquire()
509 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000510 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000511 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000512 finally:
513 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000514
515 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200516 """Reset the internal flag to false.
517
518 Subsequently, threads calling wait() will block until set() is called to
519 set the internal flag to true again.
520
521 """
Christian Heimes969fe572008-01-25 11:23:10 +0000522 self._cond.acquire()
523 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000524 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000525 finally:
526 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000527
528 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200529 """Block until the internal flag is true.
530
531 If the internal flag is true on entry, return immediately. Otherwise,
532 block until another thread calls set() to set the flag to true, or until
533 the optional timeout occurs.
534
535 When the timeout argument is present and not None, it should be a
536 floating point number specifying a timeout for the operation in seconds
537 (or fractions thereof).
538
539 This method returns the internal flag on exit, so it will always return
540 True except if a timeout is given and the operation times out.
541
542 """
Christian Heimes969fe572008-01-25 11:23:10 +0000543 self._cond.acquire()
544 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100545 signaled = self._flag
546 if not signaled:
547 signaled = self._cond.wait(timeout)
548 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000549 finally:
550 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000551
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000552
553# A barrier class. Inspired in part by the pthread_barrier_* api and
554# the CyclicBarrier class from Java. See
555# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
556# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
557# CyclicBarrier.html
558# for information.
559# We maintain two main states, 'filling' and 'draining' enabling the barrier
560# to be cyclic. Threads are not allowed into it until it has fully drained
561# since the previous cycle. In addition, a 'resetting' state exists which is
562# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300563# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100564class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200565 """Implements a Barrier.
566
567 Useful for synchronizing a fixed number of threads at known synchronization
568 points. Threads block on 'wait()' and are simultaneously once they have all
569 made that call.
570
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000571 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200572
Victor Stinner135b6d82012-03-03 01:32:57 +0100573 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200574 """Create a barrier, initialised to 'parties' threads.
575
576 'action' is a callable which, when supplied, will be called by one of
577 the threads after they have all entered the barrier and just prior to
578 releasing them all. If a 'timeout' is provided, it is uses as the
579 default for all subsequent 'wait()' calls.
580
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000581 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000582 self._cond = Condition(Lock())
583 self._action = action
584 self._timeout = timeout
585 self._parties = parties
586 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
587 self._count = 0
588
589 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200590 """Wait for the barrier.
591
592 When the specified number of threads have started waiting, they are all
593 simultaneously awoken. If an 'action' was provided for the barrier, one
594 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000595 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200596
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000597 """
598 if timeout is None:
599 timeout = self._timeout
600 with self._cond:
601 self._enter() # Block while the barrier drains.
602 index = self._count
603 self._count += 1
604 try:
605 if index + 1 == self._parties:
606 # We release the barrier
607 self._release()
608 else:
609 # We wait until someone releases us
610 self._wait(timeout)
611 return index
612 finally:
613 self._count -= 1
614 # Wake up any threads waiting for barrier to drain.
615 self._exit()
616
617 # Block until the barrier is ready for us, or raise an exception
618 # if it is broken.
619 def _enter(self):
620 while self._state in (-1, 1):
621 # It is draining or resetting, wait until done
622 self._cond.wait()
623 #see if the barrier is in a broken state
624 if self._state < 0:
625 raise BrokenBarrierError
626 assert self._state == 0
627
628 # Optionally run the 'action' and release the threads waiting
629 # in the barrier.
630 def _release(self):
631 try:
632 if self._action:
633 self._action()
634 # enter draining state
635 self._state = 1
636 self._cond.notify_all()
637 except:
638 #an exception during the _action handler. Break and reraise
639 self._break()
640 raise
641
642 # Wait in the barrier until we are relased. Raise an exception
643 # if the barrier is reset or broken.
644 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000645 if not self._cond.wait_for(lambda : self._state != 0, timeout):
646 #timed out. Break the barrier
647 self._break()
648 raise BrokenBarrierError
649 if self._state < 0:
650 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000651 assert self._state == 1
652
653 # If we are the last thread to exit the barrier, signal any threads
654 # waiting for the barrier to drain.
655 def _exit(self):
656 if self._count == 0:
657 if self._state in (-1, 1):
658 #resetting or draining
659 self._state = 0
660 self._cond.notify_all()
661
662 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200663 """Reset the barrier to the initial state.
664
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000665 Any threads currently waiting will get the BrokenBarrier exception
666 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200667
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000668 """
669 with self._cond:
670 if self._count > 0:
671 if self._state == 0:
672 #reset the barrier, waking up threads
673 self._state = -1
674 elif self._state == -2:
675 #was broken, set it to reset state
676 #which clears when the last thread exits
677 self._state = -1
678 else:
679 self._state = 0
680 self._cond.notify_all()
681
682 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200683 """Place the barrier into a 'broken' state.
684
685 Useful in case of error. Any currently waiting threads and threads
686 attempting to 'wait()' will have BrokenBarrierError raised.
687
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000688 """
689 with self._cond:
690 self._break()
691
692 def _break(self):
693 # An internal error was detected. The barrier is set to
694 # a broken state all parties awakened.
695 self._state = -2
696 self._cond.notify_all()
697
698 @property
699 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200700 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000701 return self._parties
702
703 @property
704 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200705 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000706 # We don't need synchronization here since this is an ephemeral result
707 # anyway. It returns the correct value in the steady state.
708 if self._state == 0:
709 return self._count
710 return 0
711
712 @property
713 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200714 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000715 return self._state == -2
716
Georg Brandlc30b59f2013-10-13 10:43:59 +0200717# exception raised by the Barrier class
718class BrokenBarrierError(RuntimeError):
719 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000720
721
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000722# Helper to generate new thread names
723_counter = 0
724def _newname(template="Thread-%d"):
725 global _counter
726 _counter = _counter + 1
727 return template % _counter
728
729# Active thread administration
730_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000731_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000732_limbo = {}
733
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200734# For debug and leak testing
735_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000736
737# Main class for threads
738
Victor Stinner135b6d82012-03-03 01:32:57 +0100739class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200740 """A class that represents a thread of control.
741
742 This class can be safely subclassed in a limited fashion. There are two ways
743 to specify the activity: by passing a callable object to the constructor, or
744 by overriding the run() method in a subclass.
745
746 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000747
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000748 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000749 # Need to store a reference to sys.exc_info for printing
750 # out exceptions when a thread tries to use a global var. during interp.
751 # shutdown and thus raises an exception about trying to perform some
752 # operation on/with a NoneType
753 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000754 # Keep sys.exc_clear too to clear the exception just before
755 # allowing .join() to return.
756 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000757
758 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100759 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200760 """This constructor should always be called with keyword arguments. Arguments are:
761
762 *group* should be None; reserved for future extension when a ThreadGroup
763 class is implemented.
764
765 *target* is the callable object to be invoked by the run()
766 method. Defaults to None, meaning nothing is called.
767
768 *name* is the thread name. By default, a unique name is constructed of
769 the form "Thread-N" where N is a small decimal number.
770
771 *args* is the argument tuple for the target invocation. Defaults to ().
772
773 *kwargs* is a dictionary of keyword arguments for the target
774 invocation. Defaults to {}.
775
776 If a subclass overrides the constructor, it must make sure to invoke
777 the base class constructor (Thread.__init__()) before doing anything
778 else to the thread.
779
780 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000781 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000782 if kwargs is None:
783 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000784 self._target = target
785 self._name = str(name or _newname())
786 self._args = args
787 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000788 if daemon is not None:
789 self._daemonic = daemon
790 else:
791 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000792 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000793 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000794 self._stopped = False
795 self._block = Condition(Lock())
796 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000797 # sys.stderr is not stored in the class like
798 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000799 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200800 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000801
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000802 def _reset_internal_locks(self):
803 # private! Called by _after_fork() to reset our internal locks as
804 # they may be in an invalid state leading to a deadlock or crash.
805 if hasattr(self, '_block'): # DummyThread deletes _block
806 self._block.__init__()
807 self._started._reset_internal_locks()
808
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000809 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000810 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000811 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000812 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000813 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000814 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000815 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000816 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000817 status += " daemon"
818 if self._ident is not None:
819 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000820 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000821
822 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200823 """Start the thread's activity.
824
825 It must be called at most once per thread object. It arranges for the
826 object's run() method to be invoked in a separate thread of control.
827
828 This method will raise a RuntimeError if called more than once on the
829 same thread object.
830
831 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000832 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000833 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000834
Benjamin Peterson672b8032008-06-11 19:14:14 +0000835 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000836 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000837 with _active_limbo_lock:
838 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000839 try:
840 _start_new_thread(self._bootstrap, ())
841 except Exception:
842 with _active_limbo_lock:
843 del _limbo[self]
844 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000845 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000846
847 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200848 """Method representing the thread's activity.
849
850 You may override this method in a subclass. The standard run() method
851 invokes the callable object passed to the object's constructor as the
852 target argument, if any, with sequential and keyword arguments taken
853 from the args and kwargs arguments, respectively.
854
855 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000856 try:
857 if self._target:
858 self._target(*self._args, **self._kwargs)
859 finally:
860 # Avoid a refcycle if the thread is running a function with
861 # an argument that has a member that points to the thread.
862 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000863
Guido van Rossumd0648992007-08-20 19:25:41 +0000864 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000865 # Wrapper around the real bootstrap code that ignores
866 # exceptions during interpreter cleanup. Those typically
867 # happen when a daemon thread wakes up at an unfortunate
868 # moment, finds the world around it destroyed, and raises some
869 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000870 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000871 # don't help anybody, and they confuse users, so we suppress
872 # them. We suppress them only when it appears that the world
873 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000874 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000875 # reported. Also, we only suppress them for daemonic threads;
876 # if a non-daemonic encounters this, something else is wrong.
877 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000878 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000879 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000880 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000881 return
882 raise
883
Benjamin Petersond23f8222009-04-05 19:13:16 +0000884 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200885 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000886
Guido van Rossumd0648992007-08-20 19:25:41 +0000887 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000888 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000889 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000890 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000891 with _active_limbo_lock:
892 _active[self._ident] = self
893 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000894
895 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000896 _sys.settrace(_trace_hook)
897 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000898 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000899
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000900 try:
901 self.run()
902 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100903 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000904 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000905 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000906 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000907 # _sys) in case sys.stderr was redefined since the creation of
908 # self.
909 if _sys:
910 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000911 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000912 else:
913 # Do the best job possible w/o a huge amt. of code to
914 # approximate a traceback (code ideas from
915 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000916 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000917 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000918 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000919 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000920 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000921 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000922 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000923 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000924 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000925 ' File "%s", line %s, in %s' %
926 (exc_tb.tb_frame.f_code.co_filename,
927 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000928 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000929 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000930 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000931 # Make sure that exc_tb gets deleted since it is a memory
932 # hog; deleting everything else is just for thoroughness
933 finally:
934 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000935 finally:
936 # Prevent a race in
937 # test_threading.test_no_refcycle_through_target when
938 # the exception keeps the target alive past when we
939 # assert that it's dead.
940 #XXX self.__exc_clear()
941 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000942 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000943 with _active_limbo_lock:
944 self._stop()
945 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000946 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000947 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200948 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000949 except:
950 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000951
Guido van Rossumd0648992007-08-20 19:25:41 +0000952 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000953 self._block.acquire()
954 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000955 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000956 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000957
Guido van Rossumd0648992007-08-20 19:25:41 +0000958 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000959 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000960
Georg Brandl2067bfd2008-05-25 13:05:15 +0000961 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000962 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000963 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000964 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000965 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
966 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000967 # len(_active) is always <= 1 here, and any Thread instance created
968 # overwrites the (if any) thread currently registered in _active.
969 #
970 # An instance of _MainThread is always created by 'threading'. This
971 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000972 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000973 # same key in the dict. So when the _MainThread instance created by
974 # 'threading' tries to clean itself up when atexit calls this method
975 # it gets a KeyError if another Thread instance was created.
976 #
977 # This all means that KeyError from trying to delete something from
978 # _active if dummy_threading is being used is a red herring. But
979 # since it isn't if dummy_threading is *not* being used then don't
980 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000981
Christian Heimes969fe572008-01-25 11:23:10 +0000982 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000983 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200984 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000985 # There must not be any python code between the previous line
986 # and after the lock is released. Otherwise a tracing function
987 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000988 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000989 except KeyError:
990 if 'dummy_threading' not in _sys.modules:
991 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000992
993 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200994 """Wait until the thread terminates.
995
996 This blocks the calling thread until the thread whose join() method is
997 called terminates -- either normally or through an unhandled exception
998 or until the optional timeout occurs.
999
1000 When the timeout argument is present and not None, it should be a
1001 floating point number specifying a timeout for the operation in seconds
1002 (or fractions thereof). As join() always returns None, you must call
1003 isAlive() after join() to decide whether a timeout happened -- if the
1004 thread is still alive, the join() call timed out.
1005
1006 When the timeout argument is not present or None, the operation will
1007 block until the thread terminates.
1008
1009 A thread can be join()ed many times.
1010
1011 join() raises a RuntimeError if an attempt is made to join the current
1012 thread as that would cause a deadlock. It is also an error to join() a
1013 thread before it has been started and attempts to do so raises the same
1014 exception.
1015
1016 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001017 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001018 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001019 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001020 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001021 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001022 raise RuntimeError("cannot join current thread")
1023
Christian Heimes969fe572008-01-25 11:23:10 +00001024 self._block.acquire()
1025 try:
Brett Cannonad07ff22005-11-23 02:15:50 +00001026 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +00001027 while not self._stopped:
1028 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +00001029 else:
1030 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +00001031 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +00001032 delay = deadline - _time()
1033 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +00001034 break
Guido van Rossumd0648992007-08-20 19:25:41 +00001035 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +00001036 finally:
1037 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001038
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001039 @property
1040 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001041 """A string used for identification purposes only.
1042
1043 It has no semantics. Multiple threads may be given the same name. The
1044 initial name is set by the constructor.
1045
1046 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001047 assert self._initialized, "Thread.__init__() not called"
1048 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001049
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001050 @name.setter
1051 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001052 assert self._initialized, "Thread.__init__() not called"
1053 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001054
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001055 @property
1056 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001057 """Thread identifier of this thread or None if it has not been started.
1058
1059 This is a nonzero integer. See the thread.get_ident() function. Thread
1060 identifiers may be recycled when a thread exits and another thread is
1061 created. The identifier is available even after the thread has exited.
1062
1063 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001064 assert self._initialized, "Thread.__init__() not called"
1065 return self._ident
1066
Benjamin Peterson672b8032008-06-11 19:14:14 +00001067 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001068 """Return whether the thread is alive.
1069
1070 This method returns True just before the run() method starts until just
1071 after the run() method terminates. The module function enumerate()
1072 returns a list of all alive threads.
1073
1074 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001075 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +00001076 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001077
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001078 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001079
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001080 @property
1081 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001082 """A boolean value indicating whether this thread is a daemon thread.
1083
1084 This must be set before start() is called, otherwise RuntimeError is
1085 raised. Its initial value is inherited from the creating thread; the
1086 main thread is not a daemon thread and therefore all threads created in
1087 the main thread default to daemon = False.
1088
1089 The entire Python program exits when no alive non-daemon threads are
1090 left.
1091
1092 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001093 assert self._initialized, "Thread.__init__() not called"
1094 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001095
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001096 @daemon.setter
1097 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001098 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001099 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001100 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001101 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +00001102 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001103
Benjamin Peterson6640d722008-08-18 18:16:46 +00001104 def isDaemon(self):
1105 return self.daemon
1106
1107 def setDaemon(self, daemonic):
1108 self.daemon = daemonic
1109
1110 def getName(self):
1111 return self.name
1112
1113 def setName(self, name):
1114 self.name = name
1115
Martin v. Löwis44f86962001-09-05 13:44:54 +00001116# The timer class was contributed by Itamar Shtull-Trauring
1117
Éric Araujo0cdd4452011-07-28 00:28:28 +02001118class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001119 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001120
Georg Brandlc30b59f2013-10-13 10:43:59 +02001121 t = Timer(30.0, f, args=None, kwargs=None)
1122 t.start()
1123 t.cancel() # stop the timer's action if it's still waiting
1124
Martin v. Löwis44f86962001-09-05 13:44:54 +00001125 """
Tim Petersb64bec32001-09-18 02:26:39 +00001126
R David Murray19aeb432013-03-30 17:19:38 -04001127 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001128 Thread.__init__(self)
1129 self.interval = interval
1130 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001131 self.args = args if args is not None else []
1132 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001133 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001134
Martin v. Löwis44f86962001-09-05 13:44:54 +00001135 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001136 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001137 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001138
Martin v. Löwis44f86962001-09-05 13:44:54 +00001139 def run(self):
1140 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001141 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001142 self.function(*self.args, **self.kwargs)
1143 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001144
1145# Special thread class to represent the main thread
1146# This is garbage collected through an exit handler
1147
1148class _MainThread(Thread):
1149
1150 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001151 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001152 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001153 self._set_ident()
1154 with _active_limbo_lock:
1155 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001156
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001157 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +00001158 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001159 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001160 while t:
1161 t.join()
1162 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +00001163 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001164
1165def _pickSomeNonDaemonThread():
1166 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001167 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001168 return t
1169 return None
1170
1171
1172# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001173# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001174# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001175# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001176# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001177# They are marked as daemon threads so we won't wait for them
1178# when we exit (conform previous semantics).
1179
1180class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001181
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001182 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001183 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001184
Gregory P. Smith9bd4a242011-01-04 18:33:38 +00001185 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +00001186 # can never be used by a _DummyThread. Since a _DummyThread
1187 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +00001188 del self._block
Tim Peters711906e2005-01-08 07:30:42 +00001189
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001190 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001191 self._set_ident()
1192 with _active_limbo_lock:
1193 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001194
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001195 def _stop(self):
1196 pass
1197
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001198 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001199 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001200
1201
1202# Global API functions
1203
Benjamin Peterson672b8032008-06-11 19:14:14 +00001204def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001205 """Return the current Thread object, corresponding to the caller's thread of control.
1206
1207 If the caller's thread of control was not created through the threading
1208 module, a dummy thread object with limited functionality is returned.
1209
1210 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001211 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001212 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001213 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001214 return _DummyThread()
1215
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001216currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001217
Benjamin Peterson672b8032008-06-11 19:14:14 +00001218def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001219 """Return the number of Thread objects currently alive.
1220
1221 The returned count is equal to the length of the list returned by
1222 enumerate().
1223
1224 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001225 with _active_limbo_lock:
1226 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001227
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001228activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001229
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001230def _enumerate():
1231 # Same as enumerate(), but without the lock. Internal use only.
1232 return list(_active.values()) + list(_limbo.values())
1233
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001234def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001235 """Return a list of all Thread objects currently alive.
1236
1237 The list includes daemonic threads, dummy thread objects created by
1238 current_thread(), and the main thread. It excludes terminated threads and
1239 threads that have not yet been started.
1240
1241 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001242 with _active_limbo_lock:
1243 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001244
Georg Brandl2067bfd2008-05-25 13:05:15 +00001245from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001246
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001247# Create the main thread object,
1248# and make it available for the interpreter
1249# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001250
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001251_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001252
Jim Fultond15dc062004-07-14 19:11:50 +00001253# get thread-local implementation, either from the thread
1254# module, or from the python fallback
1255
1256try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001257 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +00001258except ImportError:
1259 from _threading_local import local
1260
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001261
Jesse Nollera8513972008-07-17 16:49:17 +00001262def _after_fork():
1263 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1264 # is called from PyOS_AfterFork. Here we cleanup threading module state
1265 # that should not exist after a fork.
1266
1267 # Reset _active_limbo_lock, in case we forked while the lock was held
1268 # by another (non-forked) thread. http://bugs.python.org/issue874900
1269 global _active_limbo_lock
1270 _active_limbo_lock = _allocate_lock()
1271
1272 # fork() only copied the current thread; clear references to others.
1273 new_active = {}
1274 current = current_thread()
1275 with _active_limbo_lock:
Charles-François Natali9939cc82013-08-30 23:32:53 +02001276 for thread in _enumerate():
Charles-François Natalib055bf62011-12-18 18:45:16 +01001277 # Any lock/condition variable may be currently locked or in an
1278 # invalid state, so we reinitialize them.
1279 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +00001280 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001281 # There is only one active thread. We reset the ident to
1282 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +02001283 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001284 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001285 new_active[ident] = thread
1286 else:
1287 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +01001288 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001289
1290 _limbo.clear()
1291 _active.clear()
1292 _active.update(new_active)
1293 assert len(_active) == 1