blob: 6a06feb63526844e49fc0f01c8a981117ef70901 [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
Fred Drakea8725952002-12-30 23:32:50 +00003import sys as _sys
4
5try:
6 import thread
7except ImportError:
8 del _sys.modules[__name__]
9 raise
10
Jeffrey Yasskin105f3d42008-03-31 00:35:53 +000011import warnings
Benjamin Petersonf4395602008-06-11 17:50:00 +000012
Fred Drakea8725952002-12-30 23:32:50 +000013from time import time as _time, sleep as _sleep
Neil Schemenauerf607fc52003-11-05 23:03:00 +000014from traceback import format_exc as _format_exc
Raymond Hettinger756b3f32004-01-29 06:37:52 +000015from collections import deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000016
Benjamin Peterson973e6c22008-09-01 23:12:58 +000017# Note regarding PEP 8 compliant aliases
18# This threading model was originally inspired by Java, and inherited
19# the convention of camelCase function and method names from that
20# language. While those names are not in any imminent danger of being
21# deprecated, starting with Python 2.6, the module now provides a
22# PEP 8 compliant alias for any such method name.
23# Using the new PEP 8 compliant names also facilitates substitution
24# with the multiprocessing module, which doesn't provide the old
25# Java inspired names.
26
27
Guido van Rossum7f5013a1998-04-09 22:01:42 +000028# Rename some stuff so "from threading import *" is safe
Benjamin Peterson13f73822008-06-11 18:02:31 +000029__all__ = ['activeCount', 'active_count', 'Condition', 'currentThread',
30 'current_thread', 'enumerate', 'Event',
Tim Peters685e6972003-06-29 16:50:06 +000031 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
Andrew MacIntyre92913322006-06-13 15:04:24 +000032 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000033
Guido van Rossum7f5013a1998-04-09 22:01:42 +000034_start_new_thread = thread.start_new_thread
35_allocate_lock = thread.allocate_lock
36_get_ident = thread.get_ident
Jeremy Hyltonb5fc7492000-06-01 01:17:17 +000037ThreadError = thread.error
Guido van Rossum7f5013a1998-04-09 22:01:42 +000038del thread
39
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
Jeffrey Yasskin105f3d42008-03-31 00:35:53 +000041# sys.exc_clear is used to work around the fact that except blocks
42# don't fully clear the exception until 3.0.
43warnings.filterwarnings('ignore', category=DeprecationWarning,
44 module='threading', message='sys.exc_clear')
45
Tim Peters59aba122003-07-01 20:01:55 +000046# Debug support (adapted from ihooks.py).
47# All the major classes here derive from _Verbose. We force that to
48# be a new-style class so that all the major classes here are new-style.
49# This helps debugging (type(instance) is more revealing for instances
50# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000051
Tim Peters0939fac2003-07-01 19:28:44 +000052_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
54if __debug__:
55
Tim Peters59aba122003-07-01 20:01:55 +000056 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000057
58 def __init__(self, verbose=None):
59 if verbose is None:
60 verbose = _VERBOSE
61 self.__verbose = verbose
62
63 def _note(self, format, *args):
64 if self.__verbose:
65 format = format % args
Antoine Pitrou47900cf2010-12-17 17:45:12 +000066 # Issue #4188: calling current_thread() can incur an infinite
67 # recursion if it has to create a DummyThread on the fly.
68 ident = _get_ident()
69 try:
70 name = _active[ident].name
71 except KeyError:
72 name = "<OS thread %d>" % ident
73 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000074 _sys.stderr.write(format)
75
76else:
77 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000078 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000079 def __init__(self, verbose=None):
80 pass
81 def _note(self, *args):
82 pass
83
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000084# Support for profile and trace hooks
85
86_profile_hook = None
87_trace_hook = None
88
89def setprofile(func):
90 global _profile_hook
91 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000092
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000093def settrace(func):
94 global _trace_hook
95 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000096
97# Synchronization classes
98
99Lock = _allocate_lock
100
101def RLock(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000102 return _RLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000103
104class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +0000105
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000106 def __init__(self, verbose=None):
107 _Verbose.__init__(self, verbose)
108 self.__block = _allocate_lock()
109 self.__owner = None
110 self.__count = 0
111
112 def __repr__(self):
Nick Coghlanf8bbaa92007-07-31 13:38:01 +0000113 owner = self.__owner
Antoine Pitroud7158d42009-11-09 16:00:11 +0000114 try:
115 owner = _active[owner].name
116 except KeyError:
117 pass
118 return "<%s owner=%r count=%d>" % (
119 self.__class__.__name__, owner, self.__count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000120
121 def acquire(self, blocking=1):
Antoine Pitroud7158d42009-11-09 16:00:11 +0000122 me = _get_ident()
123 if self.__owner == me:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124 self.__count = self.__count + 1
125 if __debug__:
126 self._note("%s.acquire(%s): recursive success", self, blocking)
127 return 1
128 rc = self.__block.acquire(blocking)
129 if rc:
130 self.__owner = me
131 self.__count = 1
132 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000133 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000134 else:
135 if __debug__:
136 self._note("%s.acquire(%s): failure", self, blocking)
137 return rc
138
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000139 __enter__ = acquire
140
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000141 def release(self):
Antoine Pitroud7158d42009-11-09 16:00:11 +0000142 if self.__owner != _get_ident():
Georg Brandle1254d72009-10-14 15:51:48 +0000143 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000144 self.__count = count = self.__count - 1
145 if not count:
146 self.__owner = None
147 self.__block.release()
148 if __debug__:
149 self._note("%s.release(): final release", self)
150 else:
151 if __debug__:
152 self._note("%s.release(): non-final release", self)
153
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000154 def __exit__(self, t, v, tb):
155 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000156
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000157 # Internal methods used by condition variables
158
Brett Cannon20050502008-08-02 03:13:46 +0000159 def _acquire_restore(self, count_owner):
160 count, owner = count_owner
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000161 self.__block.acquire()
162 self.__count = count
163 self.__owner = owner
164 if __debug__:
165 self._note("%s._acquire_restore()", self)
166
167 def _release_save(self):
168 if __debug__:
169 self._note("%s._release_save()", self)
170 count = self.__count
171 self.__count = 0
172 owner = self.__owner
173 self.__owner = None
174 self.__block.release()
175 return (count, owner)
176
177 def _is_owned(self):
Antoine Pitroud7158d42009-11-09 16:00:11 +0000178 return self.__owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000179
180
181def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000182 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000183
184class _Condition(_Verbose):
185
186 def __init__(self, lock=None, verbose=None):
187 _Verbose.__init__(self, verbose)
188 if lock is None:
189 lock = RLock()
190 self.__lock = lock
191 # Export the lock's acquire() and release() methods
192 self.acquire = lock.acquire
193 self.release = lock.release
194 # If the lock defines _release_save() and/or _acquire_restore(),
195 # these override the default implementations (which just call
196 # release() and acquire() on the lock). Ditto for _is_owned().
197 try:
198 self._release_save = lock._release_save
199 except AttributeError:
200 pass
201 try:
202 self._acquire_restore = lock._acquire_restore
203 except AttributeError:
204 pass
205 try:
206 self._is_owned = lock._is_owned
207 except AttributeError:
208 pass
209 self.__waiters = []
210
Guido van Rossumda5b7012006-05-02 19:47:52 +0000211 def __enter__(self):
212 return self.__lock.__enter__()
213
214 def __exit__(self, *args):
215 return self.__lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000216
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217 def __repr__(self):
218 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
219
220 def _release_save(self):
221 self.__lock.release() # No state to save
222
223 def _acquire_restore(self, x):
224 self.__lock.acquire() # Ignore saved state
225
226 def _is_owned(self):
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000227 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000228 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000229 if self.__lock.acquire(0):
230 self.__lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000231 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000233 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000234
235 def wait(self, timeout=None):
Collin Winter50b79ce2007-06-06 00:17:35 +0000236 if not self._is_owned():
Georg Brandle1254d72009-10-14 15:51:48 +0000237 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000238 waiter = _allocate_lock()
239 waiter.acquire()
240 self.__waiters.append(waiter)
241 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000242 try: # restore state no matter what (e.g., KeyboardInterrupt)
243 if timeout is None:
244 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000246 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000247 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000248 # Balancing act: We can't afford a pure busy loop, so we
249 # have to sleep; but if we sleep the whole timeout time,
250 # we'll be unresponsive. The scheme here sleeps very
251 # little at first, longer as time goes on, but never longer
252 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000253 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000254 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000255 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000256 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000257 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000258 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000259 remaining = endtime - _time()
260 if remaining <= 0:
261 break
262 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000263 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000264 if not gotit:
265 if __debug__:
266 self._note("%s.wait(%s): timed out", self, timeout)
267 try:
268 self.__waiters.remove(waiter)
269 except ValueError:
270 pass
271 else:
272 if __debug__:
273 self._note("%s.wait(%s): got it", self, timeout)
274 finally:
275 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276
277 def notify(self, n=1):
Collin Winter50b79ce2007-06-06 00:17:35 +0000278 if not self._is_owned():
Georg Brandle1254d72009-10-14 15:51:48 +0000279 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000280 __waiters = self.__waiters
281 waiters = __waiters[:n]
282 if not waiters:
283 if __debug__:
284 self._note("%s.notify(): no waiters", self)
285 return
286 self._note("%s.notify(): notifying %d waiter%s", self, n,
287 n!=1 and "s" or "")
288 for waiter in waiters:
289 waiter.release()
290 try:
291 __waiters.remove(waiter)
292 except ValueError:
293 pass
294
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000295 def notifyAll(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000296 self.notify(len(self.__waiters))
297
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000298 notify_all = notifyAll
Benjamin Petersonf4395602008-06-11 17:50:00 +0000299
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000300
301def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000302 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000303
304class _Semaphore(_Verbose):
305
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000306 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000307
308 def __init__(self, value=1, verbose=None):
Collin Winter50b79ce2007-06-06 00:17:35 +0000309 if value < 0:
310 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000311 _Verbose.__init__(self, verbose)
312 self.__cond = Condition(Lock())
313 self.__value = value
314
315 def acquire(self, blocking=1):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000316 rc = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317 self.__cond.acquire()
318 while self.__value == 0:
319 if not blocking:
320 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000321 if __debug__:
322 self._note("%s.acquire(%s): blocked waiting, value=%s",
323 self, blocking, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000324 self.__cond.wait()
325 else:
326 self.__value = self.__value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000327 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000328 self._note("%s.acquire: success, value=%s",
329 self, self.__value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000330 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000331 self.__cond.release()
332 return rc
333
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000334 __enter__ = acquire
335
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000336 def release(self):
337 self.__cond.acquire()
338 self.__value = self.__value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000339 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000340 self._note("%s.release: success, value=%s",
341 self, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000342 self.__cond.notify()
343 self.__cond.release()
344
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000345 def __exit__(self, t, v, tb):
346 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000347
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000348
Skip Montanaroe428bb72001-08-20 20:27:58 +0000349def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000350 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000351
352class _BoundedSemaphore(_Semaphore):
353 """Semaphore that checks that # releases is <= # acquires"""
354 def __init__(self, value=1, verbose=None):
355 _Semaphore.__init__(self, value, verbose)
356 self._initial_value = value
357
358 def release(self):
359 if self._Semaphore__value >= self._initial_value:
360 raise ValueError, "Semaphore released too many times"
361 return _Semaphore.release(self)
362
363
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000364def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000365 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000366
367class _Event(_Verbose):
368
369 # After Tim Peters' event class (without is_posted())
370
371 def __init__(self, verbose=None):
372 _Verbose.__init__(self, verbose)
373 self.__cond = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000374 self.__flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000376 def _reset_internal_locks(self):
377 # private! called by Thread._reset_internal_locks by _after_fork()
378 self.__cond.__init__()
379
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000380 def isSet(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000381 return self.__flag
382
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000383 is_set = isSet
Benjamin Petersonf4395602008-06-11 17:50:00 +0000384
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000385 def set(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000386 self.__cond.acquire()
387 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000388 self.__flag = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000389 self.__cond.notify_all()
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000390 finally:
391 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000392
393 def clear(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000394 self.__cond.acquire()
395 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000396 self.__flag = False
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000397 finally:
398 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000399
400 def wait(self, timeout=None):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000401 self.__cond.acquire()
402 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000403 if not self.__flag:
404 self.__cond.wait(timeout)
Georg Brandlef660e82009-03-31 20:41:08 +0000405 return self.__flag
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000406 finally:
407 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000408
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000409# Helper to generate new thread names
410_counter = 0
411def _newname(template="Thread-%d"):
412 global _counter
413 _counter = _counter + 1
414 return template % _counter
415
416# Active thread administration
417_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000418_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000419_limbo = {}
420
421
422# Main class for threads
423
424class Thread(_Verbose):
425
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000426 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000427 # Need to store a reference to sys.exc_info for printing
428 # out exceptions when a thread tries to use a global var. during interp.
429 # shutdown and thus raises an exception about trying to perform some
430 # operation on/with a NoneType
431 __exc_info = _sys.exc_info
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000432 # Keep sys.exc_clear too to clear the exception just before
433 # allowing .join() to return.
434 __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000435
436 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000437 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000438 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000439 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000440 if kwargs is None:
441 kwargs = {}
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000442 self.__target = target
443 self.__name = str(name or _newname())
444 self.__args = args
445 self.__kwargs = kwargs
446 self.__daemonic = self._set_daemon()
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000447 self.__ident = None
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000448 self.__started = Event()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000449 self.__stopped = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000450 self.__block = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000451 self.__initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000452 # sys.stderr is not stored in the class like
453 # sys.exc_info since it can be changed between instances
454 self.__stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000455
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000456 def _reset_internal_locks(self):
457 # private! Called by _after_fork() to reset our internal locks as
458 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smithc8762022011-01-04 18:43:54 +0000459 if hasattr(self, '_Thread__block'): # DummyThread deletes self.__block
460 self.__block.__init__()
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000461 self.__started._reset_internal_locks()
462
463 @property
464 def _block(self):
465 # used by a unittest
466 return self.__block
467
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000468 def _set_daemon(self):
469 # Overridden in _MainThread and _DummyThread
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000470 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000471
472 def __repr__(self):
473 assert self.__initialized, "Thread.__init__() was not called"
474 status = "initial"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000475 if self.__started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000476 status = "started"
477 if self.__stopped:
478 status = "stopped"
479 if self.__daemonic:
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000480 status += " daemon"
481 if self.__ident is not None:
482 status += " %s" % self.__ident
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000483 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
484
485 def start(self):
Collin Winter50b79ce2007-06-06 00:17:35 +0000486 if not self.__initialized:
487 raise RuntimeError("thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000488 if self.__started.is_set():
Senthil Kumaranb02b3112010-04-06 03:23:33 +0000489 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000490 if __debug__:
491 self._note("%s.start(): starting thread", self)
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000492 with _active_limbo_lock:
493 _limbo[self] = self
Gregory P. Smith613c7a52010-02-28 18:36:09 +0000494 try:
495 _start_new_thread(self.__bootstrap, ())
496 except Exception:
497 with _active_limbo_lock:
498 del _limbo[self]
499 raise
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000500 self.__started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000501
502 def run(self):
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000503 try:
504 if self.__target:
505 self.__target(*self.__args, **self.__kwargs)
506 finally:
507 # Avoid a refcycle if the thread is running a function with
508 # an argument that has a member that points to the thread.
509 del self.__target, self.__args, self.__kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510
511 def __bootstrap(self):
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000512 # Wrapper around the real bootstrap code that ignores
513 # exceptions during interpreter cleanup. Those typically
514 # happen when a daemon thread wakes up at an unfortunate
515 # moment, finds the world around it destroyed, and raises some
516 # random exception *** while trying to report the exception in
517 # __bootstrap_inner() below ***. Those random exceptions
518 # don't help anybody, and they confuse users, so we suppress
519 # them. We suppress them only when it appears that the world
520 # indeed has already been destroyed, so that exceptions in
521 # __bootstrap_inner() during normal business hours are properly
522 # reported. Also, we only suppress them for daemonic threads;
523 # if a non-daemonic encounters this, something else is wrong.
524 try:
525 self.__bootstrap_inner()
526 except:
527 if self.__daemonic and _sys is None:
528 return
529 raise
530
Benjamin Petersond906ea62009-03-31 21:34:42 +0000531 def _set_ident(self):
532 self.__ident = _get_ident()
533
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000534 def __bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000535 try:
Benjamin Petersond906ea62009-03-31 21:34:42 +0000536 self._set_ident()
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000537 self.__started.set()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000538 with _active_limbo_lock:
539 _active[self.__ident] = self
540 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000541 if __debug__:
542 self._note("%s.__bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000543
544 if _trace_hook:
545 self._note("%s.__bootstrap(): registering trace hook", self)
546 _sys.settrace(_trace_hook)
547 if _profile_hook:
548 self._note("%s.__bootstrap(): registering profile hook", self)
549 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000550
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000551 try:
552 self.run()
553 except SystemExit:
554 if __debug__:
555 self._note("%s.__bootstrap(): raised SystemExit", self)
556 except:
557 if __debug__:
558 self._note("%s.__bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000559 # If sys.stderr is no more (most likely from interpreter
560 # shutdown) use self.__stderr. Otherwise still use sys (as in
561 # _sys) in case sys.stderr was redefined since the creation of
562 # self.
563 if _sys:
564 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonb6a95562008-08-22 20:43:48 +0000565 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000566 else:
567 # Do the best job possible w/o a huge amt. of code to
568 # approximate a traceback (code ideas from
569 # Lib/traceback.py)
570 exc_type, exc_value, exc_tb = self.__exc_info()
571 try:
572 print>>self.__stderr, (
Benjamin Petersonb6a95562008-08-22 20:43:48 +0000573 "Exception in thread " + self.name +
Brett Cannoncc4e9352004-07-03 03:52:35 +0000574 " (most likely raised during interpreter shutdown):")
575 print>>self.__stderr, (
576 "Traceback (most recent call last):")
577 while exc_tb:
578 print>>self.__stderr, (
579 ' File "%s", line %s, in %s' %
580 (exc_tb.tb_frame.f_code.co_filename,
581 exc_tb.tb_lineno,
582 exc_tb.tb_frame.f_code.co_name))
583 exc_tb = exc_tb.tb_next
584 print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
585 # Make sure that exc_tb gets deleted since it is a memory
586 # hog; deleting everything else is just for thoroughness
587 finally:
588 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000589 else:
590 if __debug__:
591 self._note("%s.__bootstrap(): normal return", self)
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000592 finally:
593 # Prevent a race in
594 # test_threading.test_no_refcycle_through_target when
595 # the exception keeps the target alive past when we
596 # assert that it's dead.
Amaury Forgeot d'Arc504a48f2008-03-29 01:41:08 +0000597 self.__exc_clear()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000598 finally:
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000599 with _active_limbo_lock:
600 self.__stop()
601 try:
602 # We don't call self.__delete() because it also
603 # grabs _active_limbo_lock.
604 del _active[_get_ident()]
605 except:
606 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000607
608 def __stop(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000609 self.__block.acquire()
610 self.__stopped = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000611 self.__block.notify_all()
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000612 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000613
614 def __delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000615 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000616
Tim Peters21429932004-07-21 03:36:52 +0000617 # Notes about running with dummy_thread:
618 #
619 # Must take care to not raise an exception if dummy_thread is being
620 # used (and thus this module is being used as an instance of
621 # dummy_threading). dummy_thread.get_ident() always returns -1 since
622 # there is only one thread if dummy_thread is being used. Thus
623 # len(_active) is always <= 1 here, and any Thread instance created
624 # overwrites the (if any) thread currently registered in _active.
625 #
626 # An instance of _MainThread is always created by 'threading'. This
627 # gets overwritten the instant an instance of Thread is created; both
628 # threads return -1 from dummy_thread.get_ident() and thus have the
629 # same key in the dict. So when the _MainThread instance created by
630 # 'threading' tries to clean itself up when atexit calls this method
631 # it gets a KeyError if another Thread instance was created.
632 #
633 # This all means that KeyError from trying to delete something from
634 # _active if dummy_threading is being used is a red herring. But
635 # since it isn't if dummy_threading is *not* being used then don't
636 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000637
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000638 try:
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000639 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000640 del _active[_get_ident()]
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000641 # There must not be any python code between the previous line
642 # and after the lock is released. Otherwise a tracing function
643 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000644 # current_thread()), and would block.
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000645 except KeyError:
646 if 'dummy_threading' not in _sys.modules:
647 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000648
649 def join(self, timeout=None):
Collin Winter50b79ce2007-06-06 00:17:35 +0000650 if not self.__initialized:
651 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000652 if not self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +0000653 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000654 if self is current_thread():
Collin Winter50b79ce2007-06-06 00:17:35 +0000655 raise RuntimeError("cannot join current thread")
656
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000657 if __debug__:
658 if not self.__stopped:
659 self._note("%s.join(): waiting until thread stops", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000660 self.__block.acquire()
661 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000662 if timeout is None:
663 while not self.__stopped:
664 self.__block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000665 if __debug__:
666 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000667 else:
668 deadline = _time() + timeout
669 while not self.__stopped:
670 delay = deadline - _time()
671 if delay <= 0:
672 if __debug__:
673 self._note("%s.join(): timed out", self)
674 break
675 self.__block.wait(delay)
676 else:
677 if __debug__:
678 self._note("%s.join(): thread stopped", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000679 finally:
680 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000681
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000682 @property
683 def name(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000684 assert self.__initialized, "Thread.__init__() not called"
685 return self.__name
686
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000687 @name.setter
688 def name(self, name):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000689 assert self.__initialized, "Thread.__init__() not called"
690 self.__name = str(name)
691
Benjamin Petersond8a89722008-08-18 16:40:03 +0000692 @property
693 def ident(self):
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000694 assert self.__initialized, "Thread.__init__() not called"
695 return self.__ident
696
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000697 def isAlive(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000698 assert self.__initialized, "Thread.__init__() not called"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000699 return self.__started.is_set() and not self.__stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000700
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000701 is_alive = isAlive
Benjamin Peterson6ee1a312008-08-18 21:53:29 +0000702
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000703 @property
704 def daemon(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000705 assert self.__initialized, "Thread.__init__() not called"
706 return self.__daemonic
707
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000708 @daemon.setter
709 def daemon(self, daemonic):
Collin Winter50b79ce2007-06-06 00:17:35 +0000710 if not self.__initialized:
711 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000712 if self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +0000713 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000714 self.__daemonic = daemonic
715
Benjamin Petersond8106262008-08-18 18:13:17 +0000716 def isDaemon(self):
717 return self.daemon
718
719 def setDaemon(self, daemonic):
720 self.daemon = daemonic
721
722 def getName(self):
723 return self.name
724
725 def setName(self, name):
726 self.name = name
727
Martin v. Löwis44f86962001-09-05 13:44:54 +0000728# The timer class was contributed by Itamar Shtull-Trauring
729
730def Timer(*args, **kwargs):
731 return _Timer(*args, **kwargs)
732
733class _Timer(Thread):
734 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000735
Martin v. Löwis44f86962001-09-05 13:44:54 +0000736 t = Timer(30.0, f, args=[], kwargs={})
737 t.start()
738 t.cancel() # stop the timer's action if it's still waiting
739 """
Tim Petersb64bec32001-09-18 02:26:39 +0000740
Martin v. Löwis44f86962001-09-05 13:44:54 +0000741 def __init__(self, interval, function, args=[], kwargs={}):
742 Thread.__init__(self)
743 self.interval = interval
744 self.function = function
745 self.args = args
746 self.kwargs = kwargs
747 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000748
Martin v. Löwis44f86962001-09-05 13:44:54 +0000749 def cancel(self):
750 """Stop the timer if it hasn't finished yet"""
751 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000752
Martin v. Löwis44f86962001-09-05 13:44:54 +0000753 def run(self):
754 self.finished.wait(self.interval)
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000755 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000756 self.function(*self.args, **self.kwargs)
757 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000758
759# Special thread class to represent the main thread
760# This is garbage collected through an exit handler
761
762class _MainThread(Thread):
763
764 def __init__(self):
765 Thread.__init__(self, name="MainThread")
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000766 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +0000767 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000768 with _active_limbo_lock:
769 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000770
771 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000772 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000773
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000774 def _exitfunc(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775 self._Thread__stop()
776 t = _pickSomeNonDaemonThread()
777 if t:
778 if __debug__:
779 self._note("%s: waiting for other threads", self)
780 while t:
781 t.join()
782 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000783 if __debug__:
784 self._note("%s: exiting", self)
785 self._Thread__delete()
786
787def _pickSomeNonDaemonThread():
788 for t in enumerate():
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000789 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000790 return t
791 return None
792
793
794# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000795# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000796# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000797# leave an entry in the _active dict forever after.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000798# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000799# They are marked as daemon threads so we won't wait for them
800# when we exit (conform previous semantics).
801
802class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000803
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000804 def __init__(self):
805 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000806
807 # Thread.__block consumes an OS-level locking primitive, which
808 # can never be used by a _DummyThread. Since a _DummyThread
809 # instance is immortal, that's bad, so release this resource.
Brett Cannone6539c42005-01-08 02:43:53 +0000810 del self._Thread__block
Tim Peters711906e2005-01-08 07:30:42 +0000811
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000812 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +0000813 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000814 with _active_limbo_lock:
815 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000816
817 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000818 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000819
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000820 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000821 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822
823
824# Global API functions
825
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000826def currentThread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000827 try:
828 return _active[_get_ident()]
829 except KeyError:
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000830 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000831 return _DummyThread()
832
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000833current_thread = currentThread
Benjamin Petersonf4395602008-06-11 17:50:00 +0000834
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000835def activeCount():
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000836 with _active_limbo_lock:
837 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000839active_count = activeCount
Benjamin Petersonf4395602008-06-11 17:50:00 +0000840
Antoine Pitrou99c160b2009-11-05 13:42:29 +0000841def _enumerate():
842 # Same as enumerate(), but without the lock. Internal use only.
843 return _active.values() + _limbo.values()
844
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000845def enumerate():
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000846 with _active_limbo_lock:
847 return _active.values() + _limbo.values()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000848
Andrew MacIntyre92913322006-06-13 15:04:24 +0000849from thread import stack_size
850
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000851# Create the main thread object,
852# and make it available for the interpreter
853# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000854
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000855_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856
Jim Fultond15dc062004-07-14 19:11:50 +0000857# get thread-local implementation, either from the thread
858# module, or from the python fallback
859
860try:
861 from thread import _local as local
862except ImportError:
863 from _threading_local import local
864
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000865
Jesse Noller5e62ca42008-07-16 20:03:47 +0000866def _after_fork():
867 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
868 # is called from PyOS_AfterFork. Here we cleanup threading module state
869 # that should not exist after a fork.
870
871 # Reset _active_limbo_lock, in case we forked while the lock was held
872 # by another (non-forked) thread. http://bugs.python.org/issue874900
873 global _active_limbo_lock
874 _active_limbo_lock = _allocate_lock()
875
876 # fork() only copied the current thread; clear references to others.
877 new_active = {}
878 current = current_thread()
879 with _active_limbo_lock:
Antoine Pitrou9fb1aca2008-09-06 23:04:32 +0000880 for thread in _active.itervalues():
Jesse Noller5e62ca42008-07-16 20:03:47 +0000881 if thread is current:
Antoine Pitrou9fb1aca2008-09-06 23:04:32 +0000882 # There is only one active thread. We reset the ident to
883 # its new value since it can have changed.
884 ident = _get_ident()
885 thread._Thread__ident = ident
Gregory P. Smith2b79a812011-01-04 01:10:08 +0000886 # Any condition variables hanging off of the active thread may
887 # be in an invalid state, so we reinitialize them.
Gregory P. Smithc8762022011-01-04 18:43:54 +0000888 if hasattr(thread, '_reset_internal_locks'):
889 thread._reset_internal_locks()
Jesse Noller5e62ca42008-07-16 20:03:47 +0000890 new_active[ident] = thread
891 else:
892 # All the others are already stopped.
893 # We don't call _Thread__stop() because it tries to acquire
894 # thread._Thread__block which could also have been held while
895 # we forked.
896 thread._Thread__stopped = True
897
898 _limbo.clear()
899 _active.clear()
900 _active.update(new_active)
901 assert len(_active) == 1
902
903
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000904# Self-test code
905
906def _test():
907
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000908 class BoundedQueue(_Verbose):
909
910 def __init__(self, limit):
911 _Verbose.__init__(self)
912 self.mon = RLock()
913 self.rc = Condition(self.mon)
914 self.wc = Condition(self.mon)
915 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000916 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000917
918 def put(self, item):
919 self.mon.acquire()
920 while len(self.queue) >= self.limit:
921 self._note("put(%s): queue full", item)
922 self.wc.wait()
923 self.queue.append(item)
924 self._note("put(%s): appended, length now %d",
925 item, len(self.queue))
926 self.rc.notify()
927 self.mon.release()
928
929 def get(self):
930 self.mon.acquire()
931 while not self.queue:
932 self._note("get(): queue empty")
933 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000934 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000935 self._note("get(): got %s, %d left", item, len(self.queue))
936 self.wc.notify()
937 self.mon.release()
938 return item
939
940 class ProducerThread(Thread):
941
942 def __init__(self, queue, quota):
943 Thread.__init__(self, name="Producer")
944 self.queue = queue
945 self.quota = quota
946
947 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000948 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000949 counter = 0
950 while counter < self.quota:
951 counter = counter + 1
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000952 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000953 _sleep(random() * 0.00001)
954
955
956 class ConsumerThread(Thread):
957
958 def __init__(self, queue, count):
959 Thread.__init__(self, name="Consumer")
960 self.queue = queue
961 self.count = count
962
963 def run(self):
964 while self.count > 0:
965 item = self.queue.get()
966 print item
967 self.count = self.count - 1
968
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000969 NP = 3
970 QL = 4
971 NI = 5
972
973 Q = BoundedQueue(QL)
974 P = []
975 for i in range(NP):
976 t = ProducerThread(Q, NI)
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000977 t.name = ("Producer-%d" % (i+1))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000978 P.append(t)
979 C = ConsumerThread(Q, NI*NP)
980 for t in P:
981 t.start()
982 _sleep(0.000001)
983 C.start()
984 for t in P:
985 t.join()
986 C.join()
987
988if __name__ == '__main__':
989 _test()