blob: cbcc1f91b280f8a06289d45626b12a55166c1391 [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
Fred Drakea8725952002-12-30 23:32:50 +000011from time import time as _time, sleep as _sleep
Neil Schemenauerf607fc52003-11-05 23:03:00 +000012from traceback import format_exc as _format_exc
Raymond Hettinger756b3f32004-01-29 06:37:52 +000013from collections import deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000014
15# Rename some stuff so "from threading import *" is safe
Guido van Rossumc262a1f2002-12-30 21:59:55 +000016__all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
Tim Peters685e6972003-06-29 16:50:06 +000017 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
Jim Fultond15dc062004-07-14 19:11:50 +000018 'Timer', 'setprofile', 'settrace', 'local']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000019
Guido van Rossum7f5013a1998-04-09 22:01:42 +000020_start_new_thread = thread.start_new_thread
21_allocate_lock = thread.allocate_lock
22_get_ident = thread.get_ident
Jeremy Hyltonb5fc7492000-06-01 01:17:17 +000023ThreadError = thread.error
Guido van Rossum7f5013a1998-04-09 22:01:42 +000024del thread
25
Guido van Rossum7f5013a1998-04-09 22:01:42 +000026
Tim Peters59aba122003-07-01 20:01:55 +000027# Debug support (adapted from ihooks.py).
28# All the major classes here derive from _Verbose. We force that to
29# be a new-style class so that all the major classes here are new-style.
30# This helps debugging (type(instance) is more revealing for instances
31# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000032
Tim Peters0939fac2003-07-01 19:28:44 +000033_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000034
35if __debug__:
36
Tim Peters59aba122003-07-01 20:01:55 +000037 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000038
39 def __init__(self, verbose=None):
40 if verbose is None:
41 verbose = _VERBOSE
42 self.__verbose = verbose
43
44 def _note(self, format, *args):
45 if self.__verbose:
46 format = format % args
47 format = "%s: %s\n" % (
48 currentThread().getName(), format)
49 _sys.stderr.write(format)
50
51else:
52 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000053 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000054 def __init__(self, verbose=None):
55 pass
56 def _note(self, *args):
57 pass
58
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000059# Support for profile and trace hooks
60
61_profile_hook = None
62_trace_hook = None
63
64def setprofile(func):
65 global _profile_hook
66 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000067
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000068def settrace(func):
69 global _trace_hook
70 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000071
72# Synchronization classes
73
74Lock = _allocate_lock
75
76def RLock(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +000077 return _RLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000078
79class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000080
Guido van Rossum7f5013a1998-04-09 22:01:42 +000081 def __init__(self, verbose=None):
82 _Verbose.__init__(self, verbose)
83 self.__block = _allocate_lock()
84 self.__owner = None
85 self.__count = 0
86
87 def __repr__(self):
88 return "<%s(%s, %d)>" % (
89 self.__class__.__name__,
90 self.__owner and self.__owner.getName(),
91 self.__count)
92
93 def acquire(self, blocking=1):
94 me = currentThread()
95 if self.__owner is me:
96 self.__count = self.__count + 1
97 if __debug__:
98 self._note("%s.acquire(%s): recursive success", self, blocking)
99 return 1
100 rc = self.__block.acquire(blocking)
101 if rc:
102 self.__owner = me
103 self.__count = 1
104 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000105 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000106 else:
107 if __debug__:
108 self._note("%s.acquire(%s): failure", self, blocking)
109 return rc
110
111 def release(self):
112 me = currentThread()
113 assert self.__owner is me, "release() of un-acquire()d lock"
114 self.__count = count = self.__count - 1
115 if not count:
116 self.__owner = None
117 self.__block.release()
118 if __debug__:
119 self._note("%s.release(): final release", self)
120 else:
121 if __debug__:
122 self._note("%s.release(): non-final release", self)
123
124 # Internal methods used by condition variables
125
126 def _acquire_restore(self, (count, owner)):
127 self.__block.acquire()
128 self.__count = count
129 self.__owner = owner
130 if __debug__:
131 self._note("%s._acquire_restore()", self)
132
133 def _release_save(self):
134 if __debug__:
135 self._note("%s._release_save()", self)
136 count = self.__count
137 self.__count = 0
138 owner = self.__owner
139 self.__owner = None
140 self.__block.release()
141 return (count, owner)
142
143 def _is_owned(self):
144 return self.__owner is currentThread()
145
146
147def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000148 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000149
150class _Condition(_Verbose):
151
152 def __init__(self, lock=None, verbose=None):
153 _Verbose.__init__(self, verbose)
154 if lock is None:
155 lock = RLock()
156 self.__lock = lock
157 # Export the lock's acquire() and release() methods
158 self.acquire = lock.acquire
159 self.release = lock.release
160 # If the lock defines _release_save() and/or _acquire_restore(),
161 # these override the default implementations (which just call
162 # release() and acquire() on the lock). Ditto for _is_owned().
163 try:
164 self._release_save = lock._release_save
165 except AttributeError:
166 pass
167 try:
168 self._acquire_restore = lock._acquire_restore
169 except AttributeError:
170 pass
171 try:
172 self._is_owned = lock._is_owned
173 except AttributeError:
174 pass
175 self.__waiters = []
176
177 def __repr__(self):
178 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
179
180 def _release_save(self):
181 self.__lock.release() # No state to save
182
183 def _acquire_restore(self, x):
184 self.__lock.acquire() # Ignore saved state
185
186 def _is_owned(self):
Jeremy Hylton39c12bf2002-08-14 17:46:40 +0000187 # Return True if lock is owned by currentThread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000188 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000189 if self.__lock.acquire(0):
190 self.__lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000191 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000192 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000193 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000194
195 def wait(self, timeout=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000196 assert self._is_owned(), "wait() of un-acquire()d lock"
197 waiter = _allocate_lock()
198 waiter.acquire()
199 self.__waiters.append(waiter)
200 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000201 try: # restore state no matter what (e.g., KeyboardInterrupt)
202 if timeout is None:
203 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000205 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000206 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000207 # Balancing act: We can't afford a pure busy loop, so we
208 # have to sleep; but if we sleep the whole timeout time,
209 # we'll be unresponsive. The scheme here sleeps very
210 # little at first, longer as time goes on, but never longer
211 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000212 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000213 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000214 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000215 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000216 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000217 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000218 remaining = endtime - _time()
219 if remaining <= 0:
220 break
221 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000222 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000223 if not gotit:
224 if __debug__:
225 self._note("%s.wait(%s): timed out", self, timeout)
226 try:
227 self.__waiters.remove(waiter)
228 except ValueError:
229 pass
230 else:
231 if __debug__:
232 self._note("%s.wait(%s): got it", self, timeout)
233 finally:
234 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235
236 def notify(self, n=1):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237 assert self._is_owned(), "notify() of un-acquire()d lock"
238 __waiters = self.__waiters
239 waiters = __waiters[:n]
240 if not waiters:
241 if __debug__:
242 self._note("%s.notify(): no waiters", self)
243 return
244 self._note("%s.notify(): notifying %d waiter%s", self, n,
245 n!=1 and "s" or "")
246 for waiter in waiters:
247 waiter.release()
248 try:
249 __waiters.remove(waiter)
250 except ValueError:
251 pass
252
253 def notifyAll(self):
254 self.notify(len(self.__waiters))
255
256
257def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000258 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000259
260class _Semaphore(_Verbose):
261
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000262 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000263
264 def __init__(self, value=1, verbose=None):
265 assert value >= 0, "Semaphore initial value must be >= 0"
266 _Verbose.__init__(self, verbose)
267 self.__cond = Condition(Lock())
268 self.__value = value
269
270 def acquire(self, blocking=1):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000271 rc = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000272 self.__cond.acquire()
273 while self.__value == 0:
274 if not blocking:
275 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000276 if __debug__:
277 self._note("%s.acquire(%s): blocked waiting, value=%s",
278 self, blocking, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000279 self.__cond.wait()
280 else:
281 self.__value = self.__value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000282 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000283 self._note("%s.acquire: success, value=%s",
284 self, self.__value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000285 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286 self.__cond.release()
287 return rc
288
289 def release(self):
290 self.__cond.acquire()
291 self.__value = self.__value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000292 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000293 self._note("%s.release: success, value=%s",
294 self, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000295 self.__cond.notify()
296 self.__cond.release()
297
298
Skip Montanaroe428bb72001-08-20 20:27:58 +0000299def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000300 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000301
302class _BoundedSemaphore(_Semaphore):
303 """Semaphore that checks that # releases is <= # acquires"""
304 def __init__(self, value=1, verbose=None):
305 _Semaphore.__init__(self, value, verbose)
306 self._initial_value = value
307
308 def release(self):
309 if self._Semaphore__value >= self._initial_value:
310 raise ValueError, "Semaphore released too many times"
311 return _Semaphore.release(self)
312
313
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000314def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000315 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000316
317class _Event(_Verbose):
318
319 # After Tim Peters' event class (without is_posted())
320
321 def __init__(self, verbose=None):
322 _Verbose.__init__(self, verbose)
323 self.__cond = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000324 self.__flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000325
326 def isSet(self):
327 return self.__flag
328
329 def set(self):
330 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000331 try:
332 self.__flag = True
333 self.__cond.notifyAll()
334 finally:
335 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000336
337 def clear(self):
338 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000339 try:
340 self.__flag = False
341 finally:
342 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000343
344 def wait(self, timeout=None):
345 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000346 try:
347 if not self.__flag:
348 self.__cond.wait(timeout)
349 finally:
350 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000351
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000352# Helper to generate new thread names
353_counter = 0
354def _newname(template="Thread-%d"):
355 global _counter
356 _counter = _counter + 1
357 return template % _counter
358
359# Active thread administration
360_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000361_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000362_limbo = {}
363
364
365# Main class for threads
366
367class Thread(_Verbose):
368
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000369 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000370 # Need to store a reference to sys.exc_info for printing
371 # out exceptions when a thread tries to use a global var. during interp.
372 # shutdown and thus raises an exception about trying to perform some
373 # operation on/with a NoneType
374 __exc_info = _sys.exc_info
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
376 def __init__(self, group=None, target=None, name=None,
377 args=(), kwargs={}, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000378 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000379 _Verbose.__init__(self, verbose)
380 self.__target = target
381 self.__name = str(name or _newname())
382 self.__args = args
383 self.__kwargs = kwargs
384 self.__daemonic = self._set_daemon()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000385 self.__started = False
386 self.__stopped = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000387 self.__block = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000388 self.__initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000389 # sys.stderr is not stored in the class like
390 # sys.exc_info since it can be changed between instances
391 self.__stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000392
393 def _set_daemon(self):
394 # Overridden in _MainThread and _DummyThread
395 return currentThread().isDaemon()
396
397 def __repr__(self):
398 assert self.__initialized, "Thread.__init__() was not called"
399 status = "initial"
400 if self.__started:
401 status = "started"
402 if self.__stopped:
403 status = "stopped"
404 if self.__daemonic:
405 status = status + " daemon"
406 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
407
408 def start(self):
409 assert self.__initialized, "Thread.__init__() not called"
410 assert not self.__started, "thread already started"
411 if __debug__:
412 self._note("%s.start(): starting thread", self)
413 _active_limbo_lock.acquire()
414 _limbo[self] = self
415 _active_limbo_lock.release()
416 _start_new_thread(self.__bootstrap, ())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000417 self.__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000418 _sleep(0.000001) # 1 usec, to let the thread run (Solaris hack)
419
420 def run(self):
421 if self.__target:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000422 self.__target(*self.__args, **self.__kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000423
424 def __bootstrap(self):
425 try:
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000426 self.__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000427 _active_limbo_lock.acquire()
428 _active[_get_ident()] = self
429 del _limbo[self]
430 _active_limbo_lock.release()
431 if __debug__:
432 self._note("%s.__bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000433
434 if _trace_hook:
435 self._note("%s.__bootstrap(): registering trace hook", self)
436 _sys.settrace(_trace_hook)
437 if _profile_hook:
438 self._note("%s.__bootstrap(): registering profile hook", self)
439 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000440
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000441 try:
442 self.run()
443 except SystemExit:
444 if __debug__:
445 self._note("%s.__bootstrap(): raised SystemExit", self)
446 except:
447 if __debug__:
448 self._note("%s.__bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000449 # If sys.stderr is no more (most likely from interpreter
450 # shutdown) use self.__stderr. Otherwise still use sys (as in
451 # _sys) in case sys.stderr was redefined since the creation of
452 # self.
453 if _sys:
454 _sys.stderr.write("Exception in thread %s:\n%s\n" %
455 (self.getName(), _format_exc()))
456 else:
457 # Do the best job possible w/o a huge amt. of code to
458 # approximate a traceback (code ideas from
459 # Lib/traceback.py)
460 exc_type, exc_value, exc_tb = self.__exc_info()
461 try:
462 print>>self.__stderr, (
463 "Exception in thread " + self.getName() +
464 " (most likely raised during interpreter shutdown):")
465 print>>self.__stderr, (
466 "Traceback (most recent call last):")
467 while exc_tb:
468 print>>self.__stderr, (
469 ' File "%s", line %s, in %s' %
470 (exc_tb.tb_frame.f_code.co_filename,
471 exc_tb.tb_lineno,
472 exc_tb.tb_frame.f_code.co_name))
473 exc_tb = exc_tb.tb_next
474 print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
475 # Make sure that exc_tb gets deleted since it is a memory
476 # hog; deleting everything else is just for thoroughness
477 finally:
478 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000479 else:
480 if __debug__:
481 self._note("%s.__bootstrap(): normal return", self)
482 finally:
483 self.__stop()
Guido van Rossumf21b2aa2001-12-28 22:07:09 +0000484 try:
485 self.__delete()
486 except:
487 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000488
489 def __stop(self):
490 self.__block.acquire()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000491 self.__stopped = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000492 self.__block.notifyAll()
493 self.__block.release()
494
495 def __delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000496 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000497
Tim Peters21429932004-07-21 03:36:52 +0000498 # Notes about running with dummy_thread:
499 #
500 # Must take care to not raise an exception if dummy_thread is being
501 # used (and thus this module is being used as an instance of
502 # dummy_threading). dummy_thread.get_ident() always returns -1 since
503 # there is only one thread if dummy_thread is being used. Thus
504 # len(_active) is always <= 1 here, and any Thread instance created
505 # overwrites the (if any) thread currently registered in _active.
506 #
507 # An instance of _MainThread is always created by 'threading'. This
508 # gets overwritten the instant an instance of Thread is created; both
509 # threads return -1 from dummy_thread.get_ident() and thus have the
510 # same key in the dict. So when the _MainThread instance created by
511 # 'threading' tries to clean itself up when atexit calls this method
512 # it gets a KeyError if another Thread instance was created.
513 #
514 # This all means that KeyError from trying to delete something from
515 # _active if dummy_threading is being used is a red herring. But
516 # since it isn't if dummy_threading is *not* being used then don't
517 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000518
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519 _active_limbo_lock.acquire()
Tim Peters21429932004-07-21 03:36:52 +0000520 try:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000521 try:
522 del _active[_get_ident()]
523 except KeyError:
Tim Peters21429932004-07-21 03:36:52 +0000524 if 'dummy_threading' not in _sys.modules:
525 raise
526 finally:
527 _active_limbo_lock.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000528
529 def join(self, timeout=None):
530 assert self.__initialized, "Thread.__init__() not called"
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000531 assert self.__started, "cannot join thread before it is started"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000532 assert self is not currentThread(), "cannot join current thread"
533 if __debug__:
534 if not self.__stopped:
535 self._note("%s.join(): waiting until thread stops", self)
536 self.__block.acquire()
537 if timeout is None:
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000538 while not self.__stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000539 self.__block.wait()
540 if __debug__:
541 self._note("%s.join(): thread stopped", self)
542 else:
Guido van Rossumb39e4611998-05-29 17:47:10 +0000543 deadline = _time() + timeout
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000544 while not self.__stopped:
Guido van Rossumb39e4611998-05-29 17:47:10 +0000545 delay = deadline - _time()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000546 if delay <= 0:
547 if __debug__:
548 self._note("%s.join(): timed out", self)
549 break
550 self.__block.wait(delay)
551 else:
552 if __debug__:
553 self._note("%s.join(): thread stopped", self)
554 self.__block.release()
555
556 def getName(self):
557 assert self.__initialized, "Thread.__init__() not called"
558 return self.__name
559
560 def setName(self, name):
561 assert self.__initialized, "Thread.__init__() not called"
562 self.__name = str(name)
563
564 def isAlive(self):
565 assert self.__initialized, "Thread.__init__() not called"
566 return self.__started and not self.__stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000567
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000568 def isDaemon(self):
569 assert self.__initialized, "Thread.__init__() not called"
570 return self.__daemonic
571
572 def setDaemon(self, daemonic):
573 assert self.__initialized, "Thread.__init__() not called"
574 assert not self.__started, "cannot set daemon status of active thread"
575 self.__daemonic = daemonic
576
Martin v. Löwis44f86962001-09-05 13:44:54 +0000577# The timer class was contributed by Itamar Shtull-Trauring
578
579def Timer(*args, **kwargs):
580 return _Timer(*args, **kwargs)
581
582class _Timer(Thread):
583 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000584
Martin v. Löwis44f86962001-09-05 13:44:54 +0000585 t = Timer(30.0, f, args=[], kwargs={})
586 t.start()
587 t.cancel() # stop the timer's action if it's still waiting
588 """
Tim Petersb64bec32001-09-18 02:26:39 +0000589
Martin v. Löwis44f86962001-09-05 13:44:54 +0000590 def __init__(self, interval, function, args=[], kwargs={}):
591 Thread.__init__(self)
592 self.interval = interval
593 self.function = function
594 self.args = args
595 self.kwargs = kwargs
596 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000597
Martin v. Löwis44f86962001-09-05 13:44:54 +0000598 def cancel(self):
599 """Stop the timer if it hasn't finished yet"""
600 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000601
Martin v. Löwis44f86962001-09-05 13:44:54 +0000602 def run(self):
603 self.finished.wait(self.interval)
604 if not self.finished.isSet():
605 self.function(*self.args, **self.kwargs)
606 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000607
608# Special thread class to represent the main thread
609# This is garbage collected through an exit handler
610
611class _MainThread(Thread):
612
613 def __init__(self):
614 Thread.__init__(self, name="MainThread")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000615 self._Thread__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000616 _active_limbo_lock.acquire()
617 _active[_get_ident()] = self
618 _active_limbo_lock.release()
Fred Drake7b4fc172000-08-18 15:50:54 +0000619 import atexit
620 atexit.register(self.__exitfunc)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000621
622 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000623 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000624
625 def __exitfunc(self):
626 self._Thread__stop()
627 t = _pickSomeNonDaemonThread()
628 if t:
629 if __debug__:
630 self._note("%s: waiting for other threads", self)
631 while t:
632 t.join()
633 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000634 if __debug__:
635 self._note("%s: exiting", self)
636 self._Thread__delete()
637
638def _pickSomeNonDaemonThread():
639 for t in enumerate():
640 if not t.isDaemon() and t.isAlive():
641 return t
642 return None
643
644
645# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000646# These aren't garbage collected when they die, nor can they be waited for.
647# If they invoke anything in threading.py that calls currentThread(), they
648# leave an entry in the _active dict forever after.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000649# Their purpose is to return *something* from currentThread().
650# They are marked as daemon threads so we won't wait for them
651# when we exit (conform previous semantics).
652
653class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000654
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000655 def __init__(self):
656 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000657
658 # Thread.__block consumes an OS-level locking primitive, which
659 # can never be used by a _DummyThread. Since a _DummyThread
660 # instance is immortal, that's bad, so release this resource.
Brett Cannone6539c42005-01-08 02:43:53 +0000661 del self._Thread__block
Tim Peters711906e2005-01-08 07:30:42 +0000662
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000663 self._Thread__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000664 _active_limbo_lock.acquire()
665 _active[_get_ident()] = self
666 _active_limbo_lock.release()
667
668 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000669 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000670
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000671 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000672 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000673
674
675# Global API functions
676
677def currentThread():
678 try:
679 return _active[_get_ident()]
680 except KeyError:
Guido van Rossum5080b332000-12-15 20:08:39 +0000681 ##print "currentThread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000682 return _DummyThread()
683
684def activeCount():
685 _active_limbo_lock.acquire()
686 count = len(_active) + len(_limbo)
687 _active_limbo_lock.release()
688 return count
689
690def enumerate():
691 _active_limbo_lock.acquire()
692 active = _active.values() + _limbo.values()
693 _active_limbo_lock.release()
694 return active
695
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000696# Create the main thread object
697
698_MainThread()
699
Jim Fultond15dc062004-07-14 19:11:50 +0000700# get thread-local implementation, either from the thread
701# module, or from the python fallback
702
703try:
704 from thread import _local as local
705except ImportError:
706 from _threading_local import local
707
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000708
709# Self-test code
710
711def _test():
712
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000713 class BoundedQueue(_Verbose):
714
715 def __init__(self, limit):
716 _Verbose.__init__(self)
717 self.mon = RLock()
718 self.rc = Condition(self.mon)
719 self.wc = Condition(self.mon)
720 self.limit = limit
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000721 self.queue = deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000722
723 def put(self, item):
724 self.mon.acquire()
725 while len(self.queue) >= self.limit:
726 self._note("put(%s): queue full", item)
727 self.wc.wait()
728 self.queue.append(item)
729 self._note("put(%s): appended, length now %d",
730 item, len(self.queue))
731 self.rc.notify()
732 self.mon.release()
733
734 def get(self):
735 self.mon.acquire()
736 while not self.queue:
737 self._note("get(): queue empty")
738 self.rc.wait()
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000739 item = self.queue.popleft()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000740 self._note("get(): got %s, %d left", item, len(self.queue))
741 self.wc.notify()
742 self.mon.release()
743 return item
744
745 class ProducerThread(Thread):
746
747 def __init__(self, queue, quota):
748 Thread.__init__(self, name="Producer")
749 self.queue = queue
750 self.quota = quota
751
752 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000753 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000754 counter = 0
755 while counter < self.quota:
756 counter = counter + 1
757 self.queue.put("%s.%d" % (self.getName(), counter))
758 _sleep(random() * 0.00001)
759
760
761 class ConsumerThread(Thread):
762
763 def __init__(self, queue, count):
764 Thread.__init__(self, name="Consumer")
765 self.queue = queue
766 self.count = count
767
768 def run(self):
769 while self.count > 0:
770 item = self.queue.get()
771 print item
772 self.count = self.count - 1
773
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000774 NP = 3
775 QL = 4
776 NI = 5
777
778 Q = BoundedQueue(QL)
779 P = []
780 for i in range(NP):
781 t = ProducerThread(Q, NI)
782 t.setName("Producer-%d" % (i+1))
783 P.append(t)
784 C = ConsumerThread(Q, NI*NP)
785 for t in P:
786 t.start()
787 _sleep(0.000001)
788 C.start()
789 for t in P:
790 t.join()
791 C.join()
792
793if __name__ == '__main__':
794 _test()