1 """Thread module emulating a subset of Java's threading model."""
8 del _sys
.modules
[__name__
]
11 from time
import time
as _time
, sleep
as _sleep
12 from traceback
import format_exc
as _format_exc
13 from collections
import deque
15 # Rename some stuff so "from threading import *" is safe
16 __all__
= ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
17 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
18 'Timer', 'setprofile', 'settrace', 'local']
20 _start_new_thread
= thread
.start_new_thread
21 _allocate_lock
= thread
.allocate_lock
22 _get_ident
= thread
.get_ident
23 ThreadError
= thread
.error
27 # 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).
37 class _Verbose(object):
39 def __init__(self
, verbose
=None):
42 self
.__verbose
= verbose
44 def _note(self
, format
, *args
):
46 format
= format
% args
47 format
= "%s: %s\n" % (
48 currentThread().getName(), format
)
49 _sys
.stderr
.write(format
)
52 # Disable this when using "python -O"
53 class _Verbose(object):
54 def __init__(self
, verbose
=None):
56 def _note(self
, *args
):
59 # Support for profile and trace hooks
72 # Synchronization classes
76 def RLock(*args
, **kwargs
):
77 return _RLock(*args
, **kwargs
)
79 class _RLock(_Verbose
):
81 def __init__(self
, verbose
=None):
82 _Verbose
.__init
__(self
, verbose
)
83 self
.__block
= _allocate_lock()
88 return "<%s(%s, %d)>" % (
89 self
.__class
__.__name
__,
90 self
.__owner
and self
.__owner
.getName(),
93 def __context__(self
):
96 def acquire(self
, blocking
=1):
98 if self
.__owner
is me
:
99 self
.__count
= self
.__count
+ 1
101 self
._note
("%s.acquire(%s): recursive success", self
, blocking
)
103 rc
= self
.__block
.acquire(blocking
)
108 self
._note
("%s.acquire(%s): initial success", self
, blocking
)
111 self
._note
("%s.acquire(%s): failure", self
, blocking
)
118 assert self
.__owner
is me
, "release() of un-acquire()d lock"
119 self
.__count
= count
= self
.__count
- 1
122 self
.__block
.release()
124 self
._note
("%s.release(): final release", self
)
127 self
._note
("%s.release(): non-final release", self
)
129 def __exit__(self
, t
, v
, tb
):
132 # Internal methods used by condition variables
134 def _acquire_restore(self
, (count
, owner
)):
135 self
.__block
.acquire()
139 self
._note
("%s._acquire_restore()", self
)
141 def _release_save(self
):
143 self
._note
("%s._release_save()", self
)
148 self
.__block
.release()
149 return (count
, owner
)
152 return self
.__owner
is currentThread()
155 def Condition(*args
, **kwargs
):
156 return _Condition(*args
, **kwargs
)
158 class _Condition(_Verbose
):
160 def __init__(self
, lock
=None, verbose
=None):
161 _Verbose
.__init
__(self
, verbose
)
165 # Export the lock's acquire() and release() methods
166 self
.acquire
= lock
.acquire
167 self
.__enter
__ = self
.acquire
168 self
.release
= lock
.release
169 # If the lock defines _release_save() and/or _acquire_restore(),
170 # these override the default implementations (which just call
171 # release() and acquire() on the lock). Ditto for _is_owned().
173 self
._release
_save
= lock
._release
_save
174 except AttributeError:
177 self
._acquire
_restore
= lock
._acquire
_restore
178 except AttributeError:
181 self
._is
_owned
= lock
._is
_owned
182 except AttributeError:
186 def __context__(self
):
189 def __exit__(self
, t
, v
, tb
):
193 return "<Condition(%s, %d)>" % (self
.__lock
, len(self
.__waiters
))
195 def _release_save(self
):
196 self
.__lock
.release() # No state to save
198 def _acquire_restore(self
, x
):
199 self
.__lock
.acquire() # Ignore saved state
202 # Return True if lock is owned by currentThread.
203 # This method is called only if __lock doesn't have _is_owned().
204 if self
.__lock
.acquire(0):
205 self
.__lock
.release()
210 def wait(self
, timeout
=None):
211 assert self
._is
_owned
(), "wait() of un-acquire()d lock"
212 waiter
= _allocate_lock()
214 self
.__waiters
.append(waiter
)
215 saved_state
= self
._release
_save
()
216 try: # restore state no matter what (e.g., KeyboardInterrupt)
220 self
._note
("%s.wait(): got it", self
)
222 # Balancing act: We can't afford a pure busy loop, so we
223 # have to sleep; but if we sleep the whole timeout time,
224 # we'll be unresponsive. The scheme here sleeps very
225 # little at first, longer as time goes on, but never longer
226 # than 20 times per second (or the timeout time remaining).
227 endtime
= _time() + timeout
228 delay
= 0.0005 # 500 us -> initial delay of 1 ms
230 gotit
= waiter
.acquire(0)
233 remaining
= endtime
- _time()
236 delay
= min(delay
* 2, remaining
, .05)
240 self
._note
("%s.wait(%s): timed out", self
, timeout
)
242 self
.__waiters
.remove(waiter
)
247 self
._note
("%s.wait(%s): got it", self
, timeout
)
249 self
._acquire
_restore
(saved_state
)
251 def notify(self
, n
=1):
252 assert self
._is
_owned
(), "notify() of un-acquire()d lock"
253 __waiters
= self
.__waiters
254 waiters
= __waiters
[:n
]
257 self
._note
("%s.notify(): no waiters", self
)
259 self
._note
("%s.notify(): notifying %d waiter%s", self
, n
,
261 for waiter
in waiters
:
264 __waiters
.remove(waiter
)
269 self
.notify(len(self
.__waiters
))
272 def Semaphore(*args
, **kwargs
):
273 return _Semaphore(*args
, **kwargs
)
275 class _Semaphore(_Verbose
):
277 # After Tim Peters' semaphore class, but not quite the same (no maximum)
279 def __init__(self
, value
=1, verbose
=None):
280 assert value
>= 0, "Semaphore initial value must be >= 0"
281 _Verbose
.__init
__(self
, verbose
)
282 self
.__cond
= Condition(Lock())
285 def __context__(self
):
288 def acquire(self
, blocking
=1):
290 self
.__cond
.acquire()
291 while self
.__value
== 0:
295 self
._note
("%s.acquire(%s): blocked waiting, value=%s",
296 self
, blocking
, self
.__value
)
299 self
.__value
= self
.__value
- 1
301 self
._note
("%s.acquire: success, value=%s",
304 self
.__cond
.release()
310 self
.__cond
.acquire()
311 self
.__value
= self
.__value
+ 1
313 self
._note
("%s.release: success, value=%s",
316 self
.__cond
.release()
318 def __exit__(self
, t
, v
, tb
):
322 def BoundedSemaphore(*args
, **kwargs
):
323 return _BoundedSemaphore(*args
, **kwargs
)
325 class _BoundedSemaphore(_Semaphore
):
326 """Semaphore that checks that # releases is <= # acquires"""
327 def __init__(self
, value
=1, verbose
=None):
328 _Semaphore
.__init
__(self
, value
, verbose
)
329 self
._initial
_value
= value
332 if self
._Semaphore
__value
>= self
._initial
_value
:
333 raise ValueError, "Semaphore released too many times"
334 return _Semaphore
.release(self
)
337 def Event(*args
, **kwargs
):
338 return _Event(*args
, **kwargs
)
340 class _Event(_Verbose
):
342 # After Tim Peters' event class (without is_posted())
344 def __init__(self
, verbose
=None):
345 _Verbose
.__init
__(self
, verbose
)
346 self
.__cond
= Condition(Lock())
353 self
.__cond
.acquire()
356 self
.__cond
.notifyAll()
358 self
.__cond
.release()
361 self
.__cond
.acquire()
365 self
.__cond
.release()
367 def wait(self
, timeout
=None):
368 self
.__cond
.acquire()
371 self
.__cond
.wait(timeout
)
373 self
.__cond
.release()
375 # Helper to generate new thread names
377 def _newname(template
="Thread-%d"):
379 _counter
= _counter
+ 1
380 return template
% _counter
382 # Active thread administration
383 _active_limbo_lock
= _allocate_lock()
384 _active
= {} # maps thread id to Thread object
388 # Main class for threads
390 class Thread(_Verbose
):
392 __initialized
= False
393 # Need to store a reference to sys.exc_info for printing
394 # out exceptions when a thread tries to use a global var. during interp.
395 # shutdown and thus raises an exception about trying to perform some
396 # operation on/with a NoneType
397 __exc_info
= _sys
.exc_info
399 def __init__(self
, group
=None, target
=None, name
=None,
400 args
=(), kwargs
=None, verbose
=None):
401 assert group
is None, "group argument must be None for now"
402 _Verbose
.__init
__(self
, verbose
)
405 self
.__target
= target
406 self
.__name
= str(name
or _newname())
408 self
.__kwargs
= kwargs
409 self
.__daemonic
= self
._set
_daemon
()
410 self
.__started
= False
411 self
.__stopped
= False
412 self
.__block
= Condition(Lock())
413 self
.__initialized
= True
414 # sys.stderr is not stored in the class like
415 # sys.exc_info since it can be changed between instances
416 self
.__stderr
= _sys
.stderr
418 def _set_daemon(self
):
419 # Overridden in _MainThread and _DummyThread
420 return currentThread().isDaemon()
423 assert self
.__initialized
, "Thread.__init__() was not called"
430 status
= status
+ " daemon"
431 return "<%s(%s, %s)>" % (self
.__class
__.__name
__, self
.__name
, status
)
434 assert self
.__initialized
, "Thread.__init__() not called"
435 assert not self
.__started
, "thread already started"
437 self
._note
("%s.start(): starting thread", self
)
438 _active_limbo_lock
.acquire()
440 _active_limbo_lock
.release()
441 _start_new_thread(self
.__bootstrap
, ())
442 self
.__started
= True
443 _sleep(0.000001) # 1 usec, to let the thread run (Solaris hack)
447 self
.__target
(*self
.__args
, **self
.__kwargs
)
449 def __bootstrap(self
):
451 self
.__started
= True
452 _active_limbo_lock
.acquire()
453 _active
[_get_ident()] = self
455 _active_limbo_lock
.release()
457 self
._note
("%s.__bootstrap(): thread started", self
)
460 self
._note
("%s.__bootstrap(): registering trace hook", self
)
461 _sys
.settrace(_trace_hook
)
463 self
._note
("%s.__bootstrap(): registering profile hook", self
)
464 _sys
.setprofile(_profile_hook
)
470 self
._note
("%s.__bootstrap(): raised SystemExit", self
)
473 self
._note
("%s.__bootstrap(): unhandled exception", self
)
474 # If sys.stderr is no more (most likely from interpreter
475 # shutdown) use self.__stderr. Otherwise still use sys (as in
476 # _sys) in case sys.stderr was redefined since the creation of
479 _sys
.stderr
.write("Exception in thread %s:\n%s\n" %
480 (self
.getName(), _format_exc()))
482 # Do the best job possible w/o a huge amt. of code to
483 # approximate a traceback (code ideas from
485 exc_type
, exc_value
, exc_tb
= self
.__exc
_info
()
487 print>>self
.__stderr
, (
488 "Exception in thread " + self
.getName() +
489 " (most likely raised during interpreter shutdown):")
490 print>>self
.__stderr
, (
491 "Traceback (most recent call last):")
493 print>>self
.__stderr
, (
494 ' File "%s", line %s, in %s' %
495 (exc_tb
.tb_frame
.f_code
.co_filename
,
497 exc_tb
.tb_frame
.f_code
.co_name
))
498 exc_tb
= exc_tb
.tb_next
499 print>>self
.__stderr
, ("%s: %s" % (exc_type
, exc_value
))
500 # Make sure that exc_tb gets deleted since it is a memory
501 # hog; deleting everything else is just for thoroughness
503 del exc_type
, exc_value
, exc_tb
506 self
._note
("%s.__bootstrap(): normal return", self
)
515 self
.__block
.acquire()
516 self
.__stopped
= True
517 self
.__block
.notifyAll()
518 self
.__block
.release()
521 "Remove current thread from the dict of currently running threads."
523 # Notes about running with dummy_thread:
525 # Must take care to not raise an exception if dummy_thread is being
526 # used (and thus this module is being used as an instance of
527 # dummy_threading). dummy_thread.get_ident() always returns -1 since
528 # there is only one thread if dummy_thread is being used. Thus
529 # len(_active) is always <= 1 here, and any Thread instance created
530 # overwrites the (if any) thread currently registered in _active.
532 # An instance of _MainThread is always created by 'threading'. This
533 # gets overwritten the instant an instance of Thread is created; both
534 # threads return -1 from dummy_thread.get_ident() and thus have the
535 # same key in the dict. So when the _MainThread instance created by
536 # 'threading' tries to clean itself up when atexit calls this method
537 # it gets a KeyError if another Thread instance was created.
539 # This all means that KeyError from trying to delete something from
540 # _active if dummy_threading is being used is a red herring. But
541 # since it isn't if dummy_threading is *not* being used then don't
542 # hide the exception.
544 _active_limbo_lock
.acquire()
547 del _active
[_get_ident()]
549 if 'dummy_threading' not in _sys
.modules
:
552 _active_limbo_lock
.release()
554 def join(self
, timeout
=None):
555 assert self
.__initialized
, "Thread.__init__() not called"
556 assert self
.__started
, "cannot join thread before it is started"
557 assert self
is not currentThread(), "cannot join current thread"
559 if not self
.__stopped
:
560 self
._note
("%s.join(): waiting until thread stops", self
)
561 self
.__block
.acquire()
564 while not self
.__stopped
:
567 self
._note
("%s.join(): thread stopped", self
)
569 deadline
= _time() + timeout
570 while not self
.__stopped
:
571 delay
= deadline
- _time()
574 self
._note
("%s.join(): timed out", self
)
576 self
.__block
.wait(delay
)
579 self
._note
("%s.join(): thread stopped", self
)
581 self
.__block
.release()
584 assert self
.__initialized
, "Thread.__init__() not called"
587 def setName(self
, name
):
588 assert self
.__initialized
, "Thread.__init__() not called"
589 self
.__name
= str(name
)
592 assert self
.__initialized
, "Thread.__init__() not called"
593 return self
.__started
and not self
.__stopped
596 assert self
.__initialized
, "Thread.__init__() not called"
597 return self
.__daemonic
599 def setDaemon(self
, daemonic
):
600 assert self
.__initialized
, "Thread.__init__() not called"
601 assert not self
.__started
, "cannot set daemon status of active thread"
602 self
.__daemonic
= daemonic
604 # The timer class was contributed by Itamar Shtull-Trauring
606 def Timer(*args
, **kwargs
):
607 return _Timer(*args
, **kwargs
)
609 class _Timer(Thread
):
610 """Call a function after a specified number of seconds:
612 t = Timer(30.0, f, args=[], kwargs={})
614 t.cancel() # stop the timer's action if it's still waiting
617 def __init__(self
, interval
, function
, args
=[], kwargs
={}):
618 Thread
.__init
__(self
)
619 self
.interval
= interval
620 self
.function
= function
623 self
.finished
= Event()
626 """Stop the timer if it hasn't finished yet"""
630 self
.finished
.wait(self
.interval
)
631 if not self
.finished
.isSet():
632 self
.function(*self
.args
, **self
.kwargs
)
635 # Special thread class to represent the main thread
636 # This is garbage collected through an exit handler
638 class _MainThread(Thread
):
641 Thread
.__init
__(self
, name
="MainThread")
642 self
._Thread
__started
= True
643 _active_limbo_lock
.acquire()
644 _active
[_get_ident()] = self
645 _active_limbo_lock
.release()
647 atexit
.register(self
.__exitfunc
)
649 def _set_daemon(self
):
652 def __exitfunc(self
):
654 t
= _pickSomeNonDaemonThread()
657 self
._note
("%s: waiting for other threads", self
)
660 t
= _pickSomeNonDaemonThread()
662 self
._note
("%s: exiting", self
)
663 self
._Thread
__delete
()
665 def _pickSomeNonDaemonThread():
666 for t
in enumerate():
667 if not t
.isDaemon() and t
.isAlive():
672 # Dummy thread class to represent threads not started here.
673 # These aren't garbage collected when they die, nor can they be waited for.
674 # If they invoke anything in threading.py that calls currentThread(), they
675 # leave an entry in the _active dict forever after.
676 # Their purpose is to return *something* from currentThread().
677 # They are marked as daemon threads so we won't wait for them
678 # when we exit (conform previous semantics).
680 class _DummyThread(Thread
):
683 Thread
.__init
__(self
, name
=_newname("Dummy-%d"))
685 # Thread.__block consumes an OS-level locking primitive, which
686 # can never be used by a _DummyThread. Since a _DummyThread
687 # instance is immortal, that's bad, so release this resource.
688 del self
._Thread
__block
690 self
._Thread
__started
= True
691 _active_limbo_lock
.acquire()
692 _active
[_get_ident()] = self
693 _active_limbo_lock
.release()
695 def _set_daemon(self
):
698 def join(self
, timeout
=None):
699 assert False, "cannot join a dummy thread"
702 # Global API functions
706 return _active
[_get_ident()]
708 ##print "currentThread(): no current thread for", _get_ident()
709 return _DummyThread()
712 _active_limbo_lock
.acquire()
713 count
= len(_active
) + len(_limbo
)
714 _active_limbo_lock
.release()
718 _active_limbo_lock
.acquire()
719 active
= _active
.values() + _limbo
.values()
720 _active_limbo_lock
.release()
723 # Create the main thread object
727 # get thread-local implementation, either from the thread
728 # module, or from the python fallback
731 from thread
import _local
as local
733 from _threading_local
import local
740 class BoundedQueue(_Verbose
):
742 def __init__(self
, limit
):
743 _Verbose
.__init
__(self
)
745 self
.rc
= Condition(self
.mon
)
746 self
.wc
= Condition(self
.mon
)
752 while len(self
.queue
) >= self
.limit
:
753 self
._note
("put(%s): queue full", item
)
755 self
.queue
.append(item
)
756 self
._note
("put(%s): appended, length now %d",
757 item
, len(self
.queue
))
763 while not self
.queue
:
764 self
._note
("get(): queue empty")
766 item
= self
.queue
.popleft()
767 self
._note
("get(): got %s, %d left", item
, len(self
.queue
))
772 class ProducerThread(Thread
):
774 def __init__(self
, queue
, quota
):
775 Thread
.__init
__(self
, name
="Producer")
780 from random
import random
782 while counter
< self
.quota
:
783 counter
= counter
+ 1
784 self
.queue
.put("%s.%d" % (self
.getName(), counter
))
785 _sleep(random() * 0.00001)
788 class ConsumerThread(Thread
):
790 def __init__(self
, queue
, count
):
791 Thread
.__init
__(self
, name
="Consumer")
796 while self
.count
> 0:
797 item
= self
.queue
.get()
799 self
.count
= self
.count
- 1
808 t
= ProducerThread(Q
, NI
)
809 t
.setName("Producer-%d" % (i
+1))
811 C
= ConsumerThread(Q
, NI
*NP
)
820 if __name__
== '__main__':