blob: 5ac45e1abfc5f8a8db477c86da42100b35a96de8 [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
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000376 def isSet(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000377 return self.__flag
378
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000379 is_set = isSet
Benjamin Petersonf4395602008-06-11 17:50:00 +0000380
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000381 def set(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000382 self.__cond.acquire()
383 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000384 self.__flag = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000385 self.__cond.notify_all()
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000386 finally:
387 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000388
389 def clear(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000390 self.__cond.acquire()
391 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000392 self.__flag = False
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000393 finally:
394 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000395
396 def wait(self, timeout=None):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000397 self.__cond.acquire()
398 try:
Guido van Rossum21b60142002-11-21 21:08:39 +0000399 if not self.__flag:
400 self.__cond.wait(timeout)
Georg Brandlef660e82009-03-31 20:41:08 +0000401 return self.__flag
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000402 finally:
403 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000404
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000405# Helper to generate new thread names
406_counter = 0
407def _newname(template="Thread-%d"):
408 global _counter
409 _counter = _counter + 1
410 return template % _counter
411
412# Active thread administration
413_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000414_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000415_limbo = {}
416
417
418# Main class for threads
419
420class Thread(_Verbose):
421
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000422 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000423 # Need to store a reference to sys.exc_info for printing
424 # out exceptions when a thread tries to use a global var. during interp.
425 # shutdown and thus raises an exception about trying to perform some
426 # operation on/with a NoneType
427 __exc_info = _sys.exc_info
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000428 # Keep sys.exc_clear too to clear the exception just before
429 # allowing .join() to return.
430 __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000431
432 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000433 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000434 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000435 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000436 if kwargs is None:
437 kwargs = {}
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000438 self.__target = target
439 self.__name = str(name or _newname())
440 self.__args = args
441 self.__kwargs = kwargs
442 self.__daemonic = self._set_daemon()
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000443 self.__ident = None
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000444 self.__started = Event()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000445 self.__stopped = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000446 self.__block = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000447 self.__initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000448 # sys.stderr is not stored in the class like
449 # sys.exc_info since it can be changed between instances
450 self.__stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000451
452 def _set_daemon(self):
453 # Overridden in _MainThread and _DummyThread
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000454 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000455
456 def __repr__(self):
457 assert self.__initialized, "Thread.__init__() was not called"
458 status = "initial"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000459 if self.__started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000460 status = "started"
461 if self.__stopped:
462 status = "stopped"
463 if self.__daemonic:
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000464 status += " daemon"
465 if self.__ident is not None:
466 status += " %s" % self.__ident
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000467 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
468
469 def start(self):
Collin Winter50b79ce2007-06-06 00:17:35 +0000470 if not self.__initialized:
471 raise RuntimeError("thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000472 if self.__started.is_set():
Senthil Kumaranb02b3112010-04-06 03:23:33 +0000473 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000474 if __debug__:
475 self._note("%s.start(): starting thread", self)
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000476 with _active_limbo_lock:
477 _limbo[self] = self
Gregory P. Smith613c7a52010-02-28 18:36:09 +0000478 try:
479 _start_new_thread(self.__bootstrap, ())
480 except Exception:
481 with _active_limbo_lock:
482 del _limbo[self]
483 raise
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000484 self.__started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000485
486 def run(self):
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000487 try:
488 if self.__target:
489 self.__target(*self.__args, **self.__kwargs)
490 finally:
491 # Avoid a refcycle if the thread is running a function with
492 # an argument that has a member that points to the thread.
493 del self.__target, self.__args, self.__kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000494
495 def __bootstrap(self):
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000496 # Wrapper around the real bootstrap code that ignores
497 # exceptions during interpreter cleanup. Those typically
498 # happen when a daemon thread wakes up at an unfortunate
499 # moment, finds the world around it destroyed, and raises some
500 # random exception *** while trying to report the exception in
501 # __bootstrap_inner() below ***. Those random exceptions
502 # don't help anybody, and they confuse users, so we suppress
503 # them. We suppress them only when it appears that the world
504 # indeed has already been destroyed, so that exceptions in
505 # __bootstrap_inner() during normal business hours are properly
506 # reported. Also, we only suppress them for daemonic threads;
507 # if a non-daemonic encounters this, something else is wrong.
508 try:
509 self.__bootstrap_inner()
510 except:
511 if self.__daemonic and _sys is None:
512 return
513 raise
514
Benjamin Petersond906ea62009-03-31 21:34:42 +0000515 def _set_ident(self):
516 self.__ident = _get_ident()
517
Guido van Rossum54ec61e2007-08-20 15:18:04 +0000518 def __bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519 try:
Benjamin Petersond906ea62009-03-31 21:34:42 +0000520 self._set_ident()
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000521 self.__started.set()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000522 with _active_limbo_lock:
523 _active[self.__ident] = self
524 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000525 if __debug__:
526 self._note("%s.__bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000527
528 if _trace_hook:
529 self._note("%s.__bootstrap(): registering trace hook", self)
530 _sys.settrace(_trace_hook)
531 if _profile_hook:
532 self._note("%s.__bootstrap(): registering profile hook", self)
533 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000534
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000535 try:
536 self.run()
537 except SystemExit:
538 if __debug__:
539 self._note("%s.__bootstrap(): raised SystemExit", self)
540 except:
541 if __debug__:
542 self._note("%s.__bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000543 # If sys.stderr is no more (most likely from interpreter
544 # shutdown) use self.__stderr. Otherwise still use sys (as in
545 # _sys) in case sys.stderr was redefined since the creation of
546 # self.
547 if _sys:
548 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonb6a95562008-08-22 20:43:48 +0000549 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000550 else:
551 # Do the best job possible w/o a huge amt. of code to
552 # approximate a traceback (code ideas from
553 # Lib/traceback.py)
554 exc_type, exc_value, exc_tb = self.__exc_info()
555 try:
556 print>>self.__stderr, (
Benjamin Petersonb6a95562008-08-22 20:43:48 +0000557 "Exception in thread " + self.name +
Brett Cannoncc4e9352004-07-03 03:52:35 +0000558 " (most likely raised during interpreter shutdown):")
559 print>>self.__stderr, (
560 "Traceback (most recent call last):")
561 while exc_tb:
562 print>>self.__stderr, (
563 ' File "%s", line %s, in %s' %
564 (exc_tb.tb_frame.f_code.co_filename,
565 exc_tb.tb_lineno,
566 exc_tb.tb_frame.f_code.co_name))
567 exc_tb = exc_tb.tb_next
568 print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
569 # Make sure that exc_tb gets deleted since it is a memory
570 # hog; deleting everything else is just for thoroughness
571 finally:
572 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000573 else:
574 if __debug__:
575 self._note("%s.__bootstrap(): normal return", self)
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000576 finally:
577 # Prevent a race in
578 # test_threading.test_no_refcycle_through_target when
579 # the exception keeps the target alive past when we
580 # assert that it's dead.
Amaury Forgeot d'Arc504a48f2008-03-29 01:41:08 +0000581 self.__exc_clear()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000582 finally:
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000583 with _active_limbo_lock:
584 self.__stop()
585 try:
586 # We don't call self.__delete() because it also
587 # grabs _active_limbo_lock.
588 del _active[_get_ident()]
589 except:
590 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000591
592 def __stop(self):
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000593 self.__block.acquire()
594 self.__stopped = True
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000595 self.__block.notify_all()
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000596 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000597
598 def __delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000599 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000600
Tim Peters21429932004-07-21 03:36:52 +0000601 # Notes about running with dummy_thread:
602 #
603 # Must take care to not raise an exception if dummy_thread is being
604 # used (and thus this module is being used as an instance of
605 # dummy_threading). dummy_thread.get_ident() always returns -1 since
606 # there is only one thread if dummy_thread is being used. Thus
607 # len(_active) is always <= 1 here, and any Thread instance created
608 # overwrites the (if any) thread currently registered in _active.
609 #
610 # An instance of _MainThread is always created by 'threading'. This
611 # gets overwritten the instant an instance of Thread is created; both
612 # threads return -1 from dummy_thread.get_ident() and thus have the
613 # same key in the dict. So when the _MainThread instance created by
614 # 'threading' tries to clean itself up when atexit calls this method
615 # it gets a KeyError if another Thread instance was created.
616 #
617 # This all means that KeyError from trying to delete something from
618 # _active if dummy_threading is being used is a red herring. But
619 # since it isn't if dummy_threading is *not* being used then don't
620 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000621
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000622 try:
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000623 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000624 del _active[_get_ident()]
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000625 # There must not be any python code between the previous line
626 # and after the lock is released. Otherwise a tracing function
627 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000628 # current_thread()), and would block.
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000629 except KeyError:
630 if 'dummy_threading' not in _sys.modules:
631 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000632
633 def join(self, timeout=None):
Collin Winter50b79ce2007-06-06 00:17:35 +0000634 if not self.__initialized:
635 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000636 if not self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +0000637 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000638 if self is current_thread():
Collin Winter50b79ce2007-06-06 00:17:35 +0000639 raise RuntimeError("cannot join current thread")
640
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000641 if __debug__:
642 if not self.__stopped:
643 self._note("%s.join(): waiting until thread stops", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000644 self.__block.acquire()
645 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000646 if timeout is None:
647 while not self.__stopped:
648 self.__block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000649 if __debug__:
650 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000651 else:
652 deadline = _time() + timeout
653 while not self.__stopped:
654 delay = deadline - _time()
655 if delay <= 0:
656 if __debug__:
657 self._note("%s.join(): timed out", self)
658 break
659 self.__block.wait(delay)
660 else:
661 if __debug__:
662 self._note("%s.join(): thread stopped", self)
Raymond Hettinger70ec29d2008-01-24 18:12:23 +0000663 finally:
664 self.__block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000665
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000666 @property
667 def name(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000668 assert self.__initialized, "Thread.__init__() not called"
669 return self.__name
670
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000671 @name.setter
672 def name(self, name):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000673 assert self.__initialized, "Thread.__init__() not called"
674 self.__name = str(name)
675
Benjamin Petersond8a89722008-08-18 16:40:03 +0000676 @property
677 def ident(self):
Gregory P. Smith8856dda2008-06-01 23:48:47 +0000678 assert self.__initialized, "Thread.__init__() not called"
679 return self.__ident
680
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000681 def isAlive(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000682 assert self.__initialized, "Thread.__init__() not called"
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000683 return self.__started.is_set() and not self.__stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000684
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000685 is_alive = isAlive
Benjamin Peterson6ee1a312008-08-18 21:53:29 +0000686
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000687 @property
688 def daemon(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000689 assert self.__initialized, "Thread.__init__() not called"
690 return self.__daemonic
691
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000692 @daemon.setter
693 def daemon(self, daemonic):
Collin Winter50b79ce2007-06-06 00:17:35 +0000694 if not self.__initialized:
695 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000696 if self.__started.is_set():
Collin Winter50b79ce2007-06-06 00:17:35 +0000697 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000698 self.__daemonic = daemonic
699
Benjamin Petersond8106262008-08-18 18:13:17 +0000700 def isDaemon(self):
701 return self.daemon
702
703 def setDaemon(self, daemonic):
704 self.daemon = daemonic
705
706 def getName(self):
707 return self.name
708
709 def setName(self, name):
710 self.name = name
711
Martin v. Löwis44f86962001-09-05 13:44:54 +0000712# The timer class was contributed by Itamar Shtull-Trauring
713
714def Timer(*args, **kwargs):
715 return _Timer(*args, **kwargs)
716
717class _Timer(Thread):
718 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000719
Martin v. Löwis44f86962001-09-05 13:44:54 +0000720 t = Timer(30.0, f, args=[], kwargs={})
721 t.start()
722 t.cancel() # stop the timer's action if it's still waiting
723 """
Tim Petersb64bec32001-09-18 02:26:39 +0000724
Martin v. Löwis44f86962001-09-05 13:44:54 +0000725 def __init__(self, interval, function, args=[], kwargs={}):
726 Thread.__init__(self)
727 self.interval = interval
728 self.function = function
729 self.args = args
730 self.kwargs = kwargs
731 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000732
Martin v. Löwis44f86962001-09-05 13:44:54 +0000733 def cancel(self):
734 """Stop the timer if it hasn't finished yet"""
735 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000736
Martin v. Löwis44f86962001-09-05 13:44:54 +0000737 def run(self):
738 self.finished.wait(self.interval)
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000739 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000740 self.function(*self.args, **self.kwargs)
741 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000742
743# Special thread class to represent the main thread
744# This is garbage collected through an exit handler
745
746class _MainThread(Thread):
747
748 def __init__(self):
749 Thread.__init__(self, name="MainThread")
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000750 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +0000751 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000752 with _active_limbo_lock:
753 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000754
755 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000756 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000757
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000758 def _exitfunc(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759 self._Thread__stop()
760 t = _pickSomeNonDaemonThread()
761 if t:
762 if __debug__:
763 self._note("%s: waiting for other threads", self)
764 while t:
765 t.join()
766 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000767 if __debug__:
768 self._note("%s: exiting", self)
769 self._Thread__delete()
770
771def _pickSomeNonDaemonThread():
772 for t in enumerate():
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000773 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000774 return t
775 return None
776
777
778# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000779# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000780# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000781# leave an entry in the _active dict forever after.
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000782# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000783# They are marked as daemon threads so we won't wait for them
784# when we exit (conform previous semantics).
785
786class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000787
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000788 def __init__(self):
789 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000790
791 # Thread.__block consumes an OS-level locking primitive, which
792 # can never be used by a _DummyThread. Since a _DummyThread
793 # instance is immortal, that's bad, so release this resource.
Brett Cannone6539c42005-01-08 02:43:53 +0000794 del self._Thread__block
Tim Peters711906e2005-01-08 07:30:42 +0000795
Jeffrey Yasskin69e13092008-02-28 06:09:19 +0000796 self._Thread__started.set()
Benjamin Petersond906ea62009-03-31 21:34:42 +0000797 self._set_ident()
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000798 with _active_limbo_lock:
799 _active[_get_ident()] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000800
801 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000802 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000803
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000804 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000805 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000806
807
808# Global API functions
809
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000810def currentThread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000811 try:
812 return _active[_get_ident()]
813 except KeyError:
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000814 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000815 return _DummyThread()
816
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000817current_thread = currentThread
Benjamin Petersonf4395602008-06-11 17:50:00 +0000818
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000819def activeCount():
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000820 with _active_limbo_lock:
821 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822
Benjamin Peterson973e6c22008-09-01 23:12:58 +0000823active_count = activeCount
Benjamin Petersonf4395602008-06-11 17:50:00 +0000824
Antoine Pitrou99c160b2009-11-05 13:42:29 +0000825def _enumerate():
826 # Same as enumerate(), but without the lock. Internal use only.
827 return _active.values() + _limbo.values()
828
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000829def enumerate():
Benjamin Petersonbd9dd312009-03-31 21:06:30 +0000830 with _active_limbo_lock:
831 return _active.values() + _limbo.values()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000832
Andrew MacIntyre92913322006-06-13 15:04:24 +0000833from thread import stack_size
834
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000835# Create the main thread object,
836# and make it available for the interpreter
837# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000839_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000840
Jim Fultond15dc062004-07-14 19:11:50 +0000841# get thread-local implementation, either from the thread
842# module, or from the python fallback
843
844try:
845 from thread import _local as local
846except ImportError:
847 from _threading_local import local
848
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000849
Jesse Noller5e62ca42008-07-16 20:03:47 +0000850def _after_fork():
851 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
852 # is called from PyOS_AfterFork. Here we cleanup threading module state
853 # that should not exist after a fork.
854
855 # Reset _active_limbo_lock, in case we forked while the lock was held
856 # by another (non-forked) thread. http://bugs.python.org/issue874900
857 global _active_limbo_lock
858 _active_limbo_lock = _allocate_lock()
859
860 # fork() only copied the current thread; clear references to others.
861 new_active = {}
862 current = current_thread()
863 with _active_limbo_lock:
Antoine Pitrou9fb1aca2008-09-06 23:04:32 +0000864 for thread in _active.itervalues():
Jesse Noller5e62ca42008-07-16 20:03:47 +0000865 if thread is current:
Antoine Pitrou9fb1aca2008-09-06 23:04:32 +0000866 # There is only one active thread. We reset the ident to
867 # its new value since it can have changed.
868 ident = _get_ident()
869 thread._Thread__ident = ident
Jesse Noller5e62ca42008-07-16 20:03:47 +0000870 new_active[ident] = thread
871 else:
872 # All the others are already stopped.
873 # We don't call _Thread__stop() because it tries to acquire
874 # thread._Thread__block which could also have been held while
875 # we forked.
876 thread._Thread__stopped = True
877
878 _limbo.clear()
879 _active.clear()
880 _active.update(new_active)
881 assert len(_active) == 1
882
883
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000884# Self-test code
885
886def _test():
887
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000888 class BoundedQueue(_Verbose):
889
890 def __init__(self, limit):
891 _Verbose.__init__(self)
892 self.mon = RLock()
893 self.rc = Condition(self.mon)
894 self.wc = Condition(self.mon)
895 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000896 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000897
898 def put(self, item):
899 self.mon.acquire()
900 while len(self.queue) >= self.limit:
901 self._note("put(%s): queue full", item)
902 self.wc.wait()
903 self.queue.append(item)
904 self._note("put(%s): appended, length now %d",
905 item, len(self.queue))
906 self.rc.notify()
907 self.mon.release()
908
909 def get(self):
910 self.mon.acquire()
911 while not self.queue:
912 self._note("get(): queue empty")
913 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000914 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000915 self._note("get(): got %s, %d left", item, len(self.queue))
916 self.wc.notify()
917 self.mon.release()
918 return item
919
920 class ProducerThread(Thread):
921
922 def __init__(self, queue, quota):
923 Thread.__init__(self, name="Producer")
924 self.queue = queue
925 self.quota = quota
926
927 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000928 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000929 counter = 0
930 while counter < self.quota:
931 counter = counter + 1
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000932 self.queue.put("%s.%d" % (self.name, counter))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000933 _sleep(random() * 0.00001)
934
935
936 class ConsumerThread(Thread):
937
938 def __init__(self, queue, count):
939 Thread.__init__(self, name="Consumer")
940 self.queue = queue
941 self.count = count
942
943 def run(self):
944 while self.count > 0:
945 item = self.queue.get()
946 print item
947 self.count = self.count - 1
948
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000949 NP = 3
950 QL = 4
951 NI = 5
952
953 Q = BoundedQueue(QL)
954 P = []
955 for i in range(NP):
956 t = ProducerThread(Q, NI)
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000957 t.name = ("Producer-%d" % (i+1))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000958 P.append(t)
959 C = ConsumerThread(Q, NI*NP)
960 for t in P:
961 t.start()
962 _sleep(0.000001)
963 C.start()
964 for t in P:
965 t.join()
966 C.join()
967
968if __name__ == '__main__':
969 _test()