blob: e7f7df7d7f80d5857641de4e1afc319e5ad8dd8c [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
Raymond Hettinger30307282013-03-20 19:28:19 -070013from itertools import islice as _islice
Raymond Hettingerec4b1742013-03-10 17:57:28 -070014try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070015 from _collections import deque as _deque
16except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070017 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000018
Benjamin Petersonb3085c92008-09-01 23:09:31 +000019# Note regarding PEP 8 compliant names
20# This threading model was originally inspired by Java, and inherited
21# the convention of camelCase function and method names from that
22# language. Those originaly names are not in any imminent danger of
23# being deprecated (even for Py3k),so this module provides them as an
24# alias for the PEP 8 compliant names
25# Note that using the new PEP 8 compliant names facilitates substitution
26# with the multiprocessing module, which doesn't provide the old
27# Java inspired names.
28
Benjamin Peterson672b8032008-06-11 19:14:14 +000029__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000030 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050031 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000032
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000033# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000034_start_new_thread = _thread.start_new_thread
35_allocate_lock = _thread.allocate_lock
Victor Stinner2a129742011-05-30 23:02:52 +020036get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000037ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000038try:
39 _CRLock = _thread.RLock
40except AttributeError:
41 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000042TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000043del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000046# Support for profile and trace hooks
47
48_profile_hook = None
49_trace_hook = None
50
51def setprofile(func):
52 global _profile_hook
53 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000054
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000055def settrace(func):
56 global _trace_hook
57 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000058
59# Synchronization classes
60
61Lock = _allocate_lock
62
Victor Stinner135b6d82012-03-03 01:32:57 +010063def RLock(*args, **kwargs):
64 if _CRLock is None:
65 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000066 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000067
Victor Stinner135b6d82012-03-03 01:32:57 +010068class _RLock:
Tim Petersb90f89a2001-01-15 03:26:36 +000069
Victor Stinner135b6d82012-03-03 01:32:57 +010070 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000071 self._block = _allocate_lock()
72 self._owner = None
73 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000074
75 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000076 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +000077 try:
78 owner = _active[owner].name
79 except KeyError:
80 pass
81 return "<%s owner=%r count=%d>" % (
82 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000083
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000084 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +020085 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +000086 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -070087 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000089 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000090 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +000091 self._owner = me
92 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000093 return rc
94
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000095 __enter__ = acquire
96
Guido van Rossum7f5013a1998-04-09 22:01:42 +000097 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +020098 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +000099 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000100 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000101 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000102 self._owner = None
103 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000104
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000105 def __exit__(self, t, v, tb):
106 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000107
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000108 # Internal methods used by condition variables
109
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000110 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000111 self._block.acquire()
112 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000113
114 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200115 if self._count == 0:
116 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000117 count = self._count
118 self._count = 0
119 owner = self._owner
120 self._owner = None
121 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000122 return (count, owner)
123
124 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200125 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000126
Antoine Pitrou434736a2009-11-10 18:46:01 +0000127_PyRLock = _RLock
128
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000129
Victor Stinner135b6d82012-03-03 01:32:57 +0100130class Condition:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000131
Victor Stinner135b6d82012-03-03 01:32:57 +0100132 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000133 if lock is None:
134 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000135 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000136 # Export the lock's acquire() and release() methods
137 self.acquire = lock.acquire
138 self.release = lock.release
139 # If the lock defines _release_save() and/or _acquire_restore(),
140 # these override the default implementations (which just call
141 # release() and acquire() on the lock). Ditto for _is_owned().
142 try:
143 self._release_save = lock._release_save
144 except AttributeError:
145 pass
146 try:
147 self._acquire_restore = lock._acquire_restore
148 except AttributeError:
149 pass
150 try:
151 self._is_owned = lock._is_owned
152 except AttributeError:
153 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700154 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000155
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000157 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000158
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000160 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000161
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000163 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000164
165 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000166 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000167
168 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000169 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000170
171 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000172 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000173 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000174 if self._lock.acquire(0):
175 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000176 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000177 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000178 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000179
180 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000181 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000182 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000183 waiter = _allocate_lock()
184 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000185 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000186 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000187 try: # restore state no matter what (e.g., KeyboardInterrupt)
188 if timeout is None:
189 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000190 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000191 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000192 if timeout > 0:
193 gotit = waiter.acquire(True, timeout)
194 else:
195 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000196 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000197 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000198 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000199 except ValueError:
200 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000201 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000202 finally:
203 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000205 def wait_for(self, predicate, timeout=None):
206 endtime = None
207 waittime = timeout
208 result = predicate()
209 while not result:
210 if waittime is not None:
211 if endtime is None:
212 endtime = _time() + waittime
213 else:
214 waittime = endtime - _time()
215 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000216 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000217 self.wait(waittime)
218 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000219 return result
220
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000221 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000222 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000223 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700224 all_waiters = self._waiters
225 waiters_to_notify = _deque(_islice(all_waiters, n))
226 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000227 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700228 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000229 waiter.release()
230 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700231 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232 except ValueError:
233 pass
234
Benjamin Peterson672b8032008-06-11 19:14:14 +0000235 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000236 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000238 notifyAll = notify_all
239
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240
Victor Stinner135b6d82012-03-03 01:32:57 +0100241class Semaphore:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000242
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000243 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000244
Victor Stinner135b6d82012-03-03 01:32:57 +0100245 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000246 if value < 0:
247 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000248 self._cond = Condition(Lock())
249 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000250
Antoine Pitrou0454af92010-04-17 23:51:58 +0000251 def acquire(self, blocking=True, timeout=None):
252 if not blocking and timeout is not None:
253 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000254 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000255 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000256 self._cond.acquire()
257 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000258 if not blocking:
259 break
Antoine Pitrou0454af92010-04-17 23:51:58 +0000260 if timeout is not None:
261 if endtime is None:
262 endtime = _time() + timeout
263 else:
264 timeout = endtime - _time()
265 if timeout <= 0:
266 break
267 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000268 else:
Raymond Hettinger720da572013-03-10 15:13:35 -0700269 self._value -= 1
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000270 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000271 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000272 return rc
273
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000274 __enter__ = acquire
275
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000277 self._cond.acquire()
Raymond Hettinger720da572013-03-10 15:13:35 -0700278 self._value += 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000279 self._cond.notify()
280 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000281
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000282 def __exit__(self, t, v, tb):
283 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000284
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000285
Éric Araujo0cdd4452011-07-28 00:28:28 +0200286class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000287 """Semaphore that checks that # releases is <= # acquires"""
Victor Stinner135b6d82012-03-03 01:32:57 +0100288 def __init__(self, value=1):
289 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000290 self._initial_value = value
291
292 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000293 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000294 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200295 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000296
297
Victor Stinner135b6d82012-03-03 01:32:57 +0100298class Event:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000299
300 # After Tim Peters' event class (without is_posted())
301
Victor Stinner135b6d82012-03-03 01:32:57 +0100302 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000303 self._cond = Condition(Lock())
304 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000305
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000306 def _reset_internal_locks(self):
307 # private! called by Thread._reset_internal_locks by _after_fork()
308 self._cond.__init__()
309
Benjamin Peterson672b8032008-06-11 19:14:14 +0000310 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000311 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000312
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000313 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000314
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000316 self._cond.acquire()
317 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000318 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000319 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000320 finally:
321 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000322
323 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000324 self._cond.acquire()
325 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000326 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000327 finally:
328 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000329
330 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000331 self._cond.acquire()
332 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100333 signaled = self._flag
334 if not signaled:
335 signaled = self._cond.wait(timeout)
336 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000337 finally:
338 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000339
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000340
341# A barrier class. Inspired in part by the pthread_barrier_* api and
342# the CyclicBarrier class from Java. See
343# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
344# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
345# CyclicBarrier.html
346# for information.
347# We maintain two main states, 'filling' and 'draining' enabling the barrier
348# to be cyclic. Threads are not allowed into it until it has fully drained
349# since the previous cycle. In addition, a 'resetting' state exists which is
350# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300351# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100352class Barrier:
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000353 """
354 Barrier. Useful for synchronizing a fixed number of threads
355 at known synchronization points. Threads block on 'wait()' and are
356 simultaneously once they have all made that call.
357 """
Victor Stinner135b6d82012-03-03 01:32:57 +0100358 def __init__(self, parties, action=None, timeout=None):
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000359 """
360 Create a barrier, initialised to 'parties' threads.
361 'action' is a callable which, when supplied, will be called
362 by one of the threads after they have all entered the
363 barrier and just prior to releasing them all.
364 If a 'timeout' is provided, it is uses as the default for
365 all subsequent 'wait()' calls.
366 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000367 self._cond = Condition(Lock())
368 self._action = action
369 self._timeout = timeout
370 self._parties = parties
371 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
372 self._count = 0
373
374 def wait(self, timeout=None):
375 """
376 Wait for the barrier. When the specified number of threads have
377 started waiting, they are all simultaneously awoken. If an 'action'
378 was provided for the barrier, one of the threads will have executed
379 that callback prior to returning.
380 Returns an individual index number from 0 to 'parties-1'.
381 """
382 if timeout is None:
383 timeout = self._timeout
384 with self._cond:
385 self._enter() # Block while the barrier drains.
386 index = self._count
387 self._count += 1
388 try:
389 if index + 1 == self._parties:
390 # We release the barrier
391 self._release()
392 else:
393 # We wait until someone releases us
394 self._wait(timeout)
395 return index
396 finally:
397 self._count -= 1
398 # Wake up any threads waiting for barrier to drain.
399 self._exit()
400
401 # Block until the barrier is ready for us, or raise an exception
402 # if it is broken.
403 def _enter(self):
404 while self._state in (-1, 1):
405 # It is draining or resetting, wait until done
406 self._cond.wait()
407 #see if the barrier is in a broken state
408 if self._state < 0:
409 raise BrokenBarrierError
410 assert self._state == 0
411
412 # Optionally run the 'action' and release the threads waiting
413 # in the barrier.
414 def _release(self):
415 try:
416 if self._action:
417 self._action()
418 # enter draining state
419 self._state = 1
420 self._cond.notify_all()
421 except:
422 #an exception during the _action handler. Break and reraise
423 self._break()
424 raise
425
426 # Wait in the barrier until we are relased. Raise an exception
427 # if the barrier is reset or broken.
428 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000429 if not self._cond.wait_for(lambda : self._state != 0, timeout):
430 #timed out. Break the barrier
431 self._break()
432 raise BrokenBarrierError
433 if self._state < 0:
434 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000435 assert self._state == 1
436
437 # If we are the last thread to exit the barrier, signal any threads
438 # waiting for the barrier to drain.
439 def _exit(self):
440 if self._count == 0:
441 if self._state in (-1, 1):
442 #resetting or draining
443 self._state = 0
444 self._cond.notify_all()
445
446 def reset(self):
447 """
448 Reset the barrier to the initial state.
449 Any threads currently waiting will get the BrokenBarrier exception
450 raised.
451 """
452 with self._cond:
453 if self._count > 0:
454 if self._state == 0:
455 #reset the barrier, waking up threads
456 self._state = -1
457 elif self._state == -2:
458 #was broken, set it to reset state
459 #which clears when the last thread exits
460 self._state = -1
461 else:
462 self._state = 0
463 self._cond.notify_all()
464
465 def abort(self):
466 """
467 Place the barrier into a 'broken' state.
468 Useful in case of error. Any currently waiting threads and
469 threads attempting to 'wait()' will have BrokenBarrierError
470 raised.
471 """
472 with self._cond:
473 self._break()
474
475 def _break(self):
476 # An internal error was detected. The barrier is set to
477 # a broken state all parties awakened.
478 self._state = -2
479 self._cond.notify_all()
480
481 @property
482 def parties(self):
483 """
484 Return the number of threads required to trip the barrier.
485 """
486 return self._parties
487
488 @property
489 def n_waiting(self):
490 """
491 Return the number of threads that are currently waiting at the barrier.
492 """
493 # We don't need synchronization here since this is an ephemeral result
494 # anyway. It returns the correct value in the steady state.
495 if self._state == 0:
496 return self._count
497 return 0
498
499 @property
500 def broken(self):
501 """
502 Return True if the barrier is in a broken state
503 """
504 return self._state == -2
505
506#exception raised by the Barrier class
507class BrokenBarrierError(RuntimeError): pass
508
509
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510# Helper to generate new thread names
511_counter = 0
512def _newname(template="Thread-%d"):
513 global _counter
Raymond Hettinger720da572013-03-10 15:13:35 -0700514 _counter += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000515 return template % _counter
516
517# Active thread administration
518_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000519_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000520_limbo = {}
521
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200522# For debug and leak testing
523_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000524
525# Main class for threads
526
Victor Stinner135b6d82012-03-03 01:32:57 +0100527class Thread:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000528
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000529 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000530 # Need to store a reference to sys.exc_info for printing
531 # out exceptions when a thread tries to use a global var. during interp.
532 # shutdown and thus raises an exception about trying to perform some
533 # operation on/with a NoneType
534 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000535 # Keep sys.exc_clear too to clear the exception just before
536 # allowing .join() to return.
537 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000538
539 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100540 args=(), kwargs=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000541 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000542 if kwargs is None:
543 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000544 self._target = target
545 self._name = str(name or _newname())
546 self._args = args
547 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000548 if daemon is not None:
549 self._daemonic = daemon
550 else:
551 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000552 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000553 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000554 self._stopped = False
555 self._block = Condition(Lock())
556 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000557 # sys.stderr is not stored in the class like
558 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000559 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200560 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000561
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000562 def _reset_internal_locks(self):
563 # private! Called by _after_fork() to reset our internal locks as
564 # they may be in an invalid state leading to a deadlock or crash.
565 if hasattr(self, '_block'): # DummyThread deletes _block
566 self._block.__init__()
567 self._started._reset_internal_locks()
568
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000569 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000570 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000571 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000572 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000573 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000574 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000575 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000576 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000577 status += " daemon"
578 if self._ident is not None:
579 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000580 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000581
582 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000583 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000584 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000585
Benjamin Peterson672b8032008-06-11 19:14:14 +0000586 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000587 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000588 with _active_limbo_lock:
589 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000590 try:
591 _start_new_thread(self._bootstrap, ())
592 except Exception:
593 with _active_limbo_lock:
594 del _limbo[self]
595 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000596 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000597
598 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000599 try:
600 if self._target:
601 self._target(*self._args, **self._kwargs)
602 finally:
603 # Avoid a refcycle if the thread is running a function with
604 # an argument that has a member that points to the thread.
605 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000606
Guido van Rossumd0648992007-08-20 19:25:41 +0000607 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000608 # Wrapper around the real bootstrap code that ignores
609 # exceptions during interpreter cleanup. Those typically
610 # happen when a daemon thread wakes up at an unfortunate
611 # moment, finds the world around it destroyed, and raises some
612 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000613 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000614 # don't help anybody, and they confuse users, so we suppress
615 # them. We suppress them only when it appears that the world
616 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000617 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000618 # reported. Also, we only suppress them for daemonic threads;
619 # if a non-daemonic encounters this, something else is wrong.
620 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000621 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000622 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000623 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000624 return
625 raise
626
Benjamin Petersond23f8222009-04-05 19:13:16 +0000627 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200628 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000629
Guido van Rossumd0648992007-08-20 19:25:41 +0000630 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000631 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000632 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000633 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000634 with _active_limbo_lock:
635 _active[self._ident] = self
636 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000637
638 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000639 _sys.settrace(_trace_hook)
640 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000641 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000642
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000643 try:
644 self.run()
645 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100646 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000647 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000648 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000649 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000650 # _sys) in case sys.stderr was redefined since the creation of
651 # self.
652 if _sys:
653 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000654 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000655 else:
656 # Do the best job possible w/o a huge amt. of code to
657 # approximate a traceback (code ideas from
658 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000659 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000660 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000661 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000662 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000663 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000664 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000665 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000666 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000667 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000668 ' File "%s", line %s, in %s' %
669 (exc_tb.tb_frame.f_code.co_filename,
670 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000671 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000672 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000673 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000674 # Make sure that exc_tb gets deleted since it is a memory
675 # hog; deleting everything else is just for thoroughness
676 finally:
677 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000678 finally:
679 # Prevent a race in
680 # test_threading.test_no_refcycle_through_target when
681 # the exception keeps the target alive past when we
682 # assert that it's dead.
683 #XXX self.__exc_clear()
684 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000685 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000686 with _active_limbo_lock:
687 self._stop()
688 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000689 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000690 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200691 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000692 except:
693 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000694
Guido van Rossumd0648992007-08-20 19:25:41 +0000695 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000696 self._block.acquire()
697 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000698 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000699 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000700
Guido van Rossumd0648992007-08-20 19:25:41 +0000701 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000702 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000703
Georg Brandl2067bfd2008-05-25 13:05:15 +0000704 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000705 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000706 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000707 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000708 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
709 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000710 # len(_active) is always <= 1 here, and any Thread instance created
711 # overwrites the (if any) thread currently registered in _active.
712 #
713 # An instance of _MainThread is always created by 'threading'. This
714 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000715 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000716 # same key in the dict. So when the _MainThread instance created by
717 # 'threading' tries to clean itself up when atexit calls this method
718 # it gets a KeyError if another Thread instance was created.
719 #
720 # This all means that KeyError from trying to delete something from
721 # _active if dummy_threading is being used is a red herring. But
722 # since it isn't if dummy_threading is *not* being used then don't
723 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000724
Christian Heimes969fe572008-01-25 11:23:10 +0000725 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000726 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200727 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000728 # There must not be any python code between the previous line
729 # and after the lock is released. Otherwise a tracing function
730 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000731 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000732 except KeyError:
733 if 'dummy_threading' not in _sys.modules:
734 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000735
736 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000737 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000738 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000739 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000740 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000741 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000742 raise RuntimeError("cannot join current thread")
743
Christian Heimes969fe572008-01-25 11:23:10 +0000744 self._block.acquire()
745 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000746 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000747 while not self._stopped:
748 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +0000749 else:
750 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000751 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000752 delay = deadline - _time()
753 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +0000754 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000755 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +0000756 finally:
757 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000758
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000759 @property
760 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000761 assert self._initialized, "Thread.__init__() not called"
762 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000763
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000764 @name.setter
765 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000766 assert self._initialized, "Thread.__init__() not called"
767 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000768
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000769 @property
770 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000771 assert self._initialized, "Thread.__init__() not called"
772 return self._ident
773
Benjamin Peterson672b8032008-06-11 19:14:14 +0000774 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000775 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000776 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000777
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000778 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000779
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000780 @property
781 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000782 assert self._initialized, "Thread.__init__() not called"
783 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000784
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000785 @daemon.setter
786 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000787 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000788 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000789 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000790 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000791 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000792
Benjamin Peterson6640d722008-08-18 18:16:46 +0000793 def isDaemon(self):
794 return self.daemon
795
796 def setDaemon(self, daemonic):
797 self.daemon = daemonic
798
799 def getName(self):
800 return self.name
801
802 def setName(self, name):
803 self.name = name
804
Martin v. Löwis44f86962001-09-05 13:44:54 +0000805# The timer class was contributed by Itamar Shtull-Trauring
806
Éric Araujo0cdd4452011-07-28 00:28:28 +0200807class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000808 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000809
R David Murray19aeb432013-03-30 17:19:38 -0400810 t = Timer(30.0, f, args=None, kwargs=None)
Martin v. Löwis44f86962001-09-05 13:44:54 +0000811 t.start()
812 t.cancel() # stop the timer's action if it's still waiting
813 """
Tim Petersb64bec32001-09-18 02:26:39 +0000814
R David Murray19aeb432013-03-30 17:19:38 -0400815 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000816 Thread.__init__(self)
817 self.interval = interval
818 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -0400819 self.args = args if args is not None else []
820 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +0000821 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000822
Martin v. Löwis44f86962001-09-05 13:44:54 +0000823 def cancel(self):
824 """Stop the timer if it hasn't finished yet"""
825 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000826
Martin v. Löwis44f86962001-09-05 13:44:54 +0000827 def run(self):
828 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000829 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000830 self.function(*self.args, **self.kwargs)
831 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000832
833# Special thread class to represent the main thread
834# This is garbage collected through an exit handler
835
836class _MainThread(Thread):
837
838 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000839 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000840 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000841 self._set_ident()
842 with _active_limbo_lock:
843 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000844
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000845 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000846 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000848 while t:
849 t.join()
850 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +0000851 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000852
853def _pickSomeNonDaemonThread():
854 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000855 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856 return t
857 return None
858
859
860# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000861# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000862# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000863# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000864# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000865# They are marked as daemon threads so we won't wait for them
866# when we exit (conform previous semantics).
867
868class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000869
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000870 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000871 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000872
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000873 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000874 # can never be used by a _DummyThread. Since a _DummyThread
875 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000876 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000877
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000878 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000879 self._set_ident()
880 with _active_limbo_lock:
881 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000882
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +0200883 def _stop(self):
884 pass
885
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000886 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000887 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000888
889
890# Global API functions
891
Benjamin Peterson672b8032008-06-11 19:14:14 +0000892def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000893 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200894 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000895 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000896 return _DummyThread()
897
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000898currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000899
Benjamin Peterson672b8032008-06-11 19:14:14 +0000900def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000901 with _active_limbo_lock:
902 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000903
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000904activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000905
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000906def _enumerate():
907 # Same as enumerate(), but without the lock. Internal use only.
908 return list(_active.values()) + list(_limbo.values())
909
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000910def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000911 with _active_limbo_lock:
912 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000913
Georg Brandl2067bfd2008-05-25 13:05:15 +0000914from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000915
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000916# Create the main thread object,
917# and make it available for the interpreter
918# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000919
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000920_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000921
Jim Fultond15dc062004-07-14 19:11:50 +0000922# get thread-local implementation, either from the thread
923# module, or from the python fallback
924
925try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000926 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000927except ImportError:
928 from _threading_local import local
929
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000930
Jesse Nollera8513972008-07-17 16:49:17 +0000931def _after_fork():
932 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
933 # is called from PyOS_AfterFork. Here we cleanup threading module state
934 # that should not exist after a fork.
935
936 # Reset _active_limbo_lock, in case we forked while the lock was held
937 # by another (non-forked) thread. http://bugs.python.org/issue874900
938 global _active_limbo_lock
939 _active_limbo_lock = _allocate_lock()
940
941 # fork() only copied the current thread; clear references to others.
942 new_active = {}
943 current = current_thread()
944 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000945 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +0100946 # Any lock/condition variable may be currently locked or in an
947 # invalid state, so we reinitialize them.
948 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000949 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000950 # There is only one active thread. We reset the ident to
951 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +0200952 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000953 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000954 new_active[ident] = thread
955 else:
956 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +0100957 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +0000958
959 _limbo.clear()
960 _active.clear()
961 _active.update(new_active)
962 assert len(_active) == 1