openmedialibrary_platform/Darwin/lib/python3.4/threading.py

1359 lines
48 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
"""Thread module emulating a subset of Java's threading model."""
import sys as _sys
2014-09-30 16:15:32 +00:00
import _thread
2013-10-11 17:28:32 +00:00
try:
2014-09-30 16:15:32 +00:00
from time import monotonic as _time
2013-10-11 17:28:32 +00:00
except ImportError:
2014-09-30 16:15:32 +00:00
from time import time as _time
2013-10-11 17:28:32 +00:00
from traceback import format_exc as _format_exc
2014-09-30 16:15:32 +00:00
from _weakrefset import WeakSet
from itertools import islice as _islice
try:
from _collections import deque as _deque
except ImportError:
from collections import deque as _deque
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
# Note regarding PEP 8 compliant names
2013-10-11 17:28:32 +00:00
# This threading model was originally inspired by Java, and inherited
# the convention of camelCase function and method names from that
2014-09-30 16:15:32 +00:00
# language. Those original names are not in any imminent danger of
# being deprecated (even for Py3k),so this module provides them as an
# alias for the PEP 8 compliant names
# Note that using the new PEP 8 compliant names facilitates substitution
2013-10-11 17:28:32 +00:00
# with the multiprocessing module, which doesn't provide the old
# Java inspired names.
2014-09-30 16:15:32 +00:00
__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
2013-10-11 17:28:32 +00:00
# Rename some stuff so "from threading import *" is safe
2014-09-30 16:15:32 +00:00
_start_new_thread = _thread.start_new_thread
_allocate_lock = _thread.allocate_lock
_set_sentinel = _thread._set_sentinel
get_ident = _thread.get_ident
ThreadError = _thread.error
try:
_CRLock = _thread.RLock
except AttributeError:
_CRLock = None
TIMEOUT_MAX = _thread.TIMEOUT_MAX
del _thread
2013-10-11 17:28:32 +00:00
# Support for profile and trace hooks
_profile_hook = None
_trace_hook = None
def setprofile(func):
"""Set a profile function for all threads started from the threading module.
The func will be passed to sys.setprofile() for each thread, before its
run() method is called.
"""
global _profile_hook
_profile_hook = func
def settrace(func):
"""Set a trace function for all threads started from the threading module.
The func will be passed to sys.settrace() for each thread, before its run()
method is called.
"""
global _trace_hook
_trace_hook = func
# Synchronization classes
Lock = _allocate_lock
def RLock(*args, **kwargs):
"""Factory function that returns a new reentrant lock.
A reentrant lock must be released by the thread that acquired it. Once a
thread has acquired a reentrant lock, the same thread may acquire it again
without blocking; the thread must release it once for each time it has
acquired it.
"""
2014-09-30 16:15:32 +00:00
if _CRLock is None:
return _PyRLock(*args, **kwargs)
return _CRLock(*args, **kwargs)
class _RLock:
"""This class implements reentrant lock objects.
A reentrant lock must be released by the thread that acquired it. Once a
thread has acquired a reentrant lock, the same thread may acquire it
again without blocking; the thread must release it once for each time it
has acquired it.
2013-10-11 17:28:32 +00:00
"""
2014-09-30 16:15:32 +00:00
def __init__(self):
self._block = _allocate_lock()
self._owner = None
self._count = 0
2013-10-11 17:28:32 +00:00
def __repr__(self):
2014-09-30 16:15:32 +00:00
owner = self._owner
2013-10-11 17:28:32 +00:00
try:
owner = _active[owner].name
except KeyError:
pass
return "<%s owner=%r count=%d>" % (
2014-09-30 16:15:32 +00:00
self.__class__.__name__, owner, self._count)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def acquire(self, blocking=True, timeout=-1):
2013-10-11 17:28:32 +00:00
"""Acquire a lock, blocking or non-blocking.
When invoked without arguments: if this thread already owns the lock,
increment the recursion level by one, and return immediately. Otherwise,
if another thread owns the lock, block until the lock is unlocked. Once
the lock is unlocked (not owned by any thread), then grab ownership, set
the recursion level to one, and return. If more than one thread is
blocked waiting until the lock is unlocked, only one at a time will be
able to grab ownership of the lock. There is no return value in this
case.
When invoked with the blocking argument set to true, do the same thing
as when called without arguments, and return true.
When invoked with the blocking argument set to false, do not block. If a
call without an argument would block, return false immediately;
otherwise, do the same thing as when called without arguments, and
return true.
2014-09-30 16:15:32 +00:00
When invoked with the floating-point timeout argument set to a positive
value, block for at most the number of seconds specified by timeout
and as long as the lock cannot be acquired. Return true if the lock has
been acquired, false if the timeout has elapsed.
2013-10-11 17:28:32 +00:00
"""
2014-09-30 16:15:32 +00:00
me = get_ident()
if self._owner == me:
self._count += 1
2013-10-11 17:28:32 +00:00
return 1
2014-09-30 16:15:32 +00:00
rc = self._block.acquire(blocking, timeout)
2013-10-11 17:28:32 +00:00
if rc:
2014-09-30 16:15:32 +00:00
self._owner = me
self._count = 1
2013-10-11 17:28:32 +00:00
return rc
__enter__ = acquire
def release(self):
"""Release a lock, decrementing the recursion level.
If after the decrement it is zero, reset the lock to unlocked (not owned
by any thread), and if any other threads are blocked waiting for the
lock to become unlocked, allow exactly one of them to proceed. If after
the decrement the recursion level is still nonzero, the lock remains
locked and owned by the calling thread.
Only call this method when the calling thread owns the lock. A
RuntimeError is raised if this method is called when the lock is
unlocked.
There is no return value.
"""
2014-09-30 16:15:32 +00:00
if self._owner != get_ident():
2013-10-11 17:28:32 +00:00
raise RuntimeError("cannot release un-acquired lock")
2014-09-30 16:15:32 +00:00
self._count = count = self._count - 1
2013-10-11 17:28:32 +00:00
if not count:
2014-09-30 16:15:32 +00:00
self._owner = None
self._block.release()
2013-10-11 17:28:32 +00:00
def __exit__(self, t, v, tb):
self.release()
# Internal methods used by condition variables
2014-09-30 16:15:32 +00:00
def _acquire_restore(self, state):
self._block.acquire()
self._count, self._owner = state
2013-10-11 17:28:32 +00:00
def _release_save(self):
2014-09-30 16:15:32 +00:00
if self._count == 0:
raise RuntimeError("cannot release un-acquired lock")
count = self._count
self._count = 0
owner = self._owner
self._owner = None
self._block.release()
2013-10-11 17:28:32 +00:00
return (count, owner)
def _is_owned(self):
2014-09-30 16:15:32 +00:00
return self._owner == get_ident()
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
_PyRLock = _RLock
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
class Condition:
"""Class that implements a condition variable.
2013-10-11 17:28:32 +00:00
A condition variable allows one or more threads to wait until they are
notified by another thread.
If the lock argument is given and not None, it must be a Lock or RLock
object, and it is used as the underlying lock. Otherwise, a new RLock object
is created and used as the underlying lock.
"""
2014-09-30 16:15:32 +00:00
def __init__(self, lock=None):
2013-10-11 17:28:32 +00:00
if lock is None:
lock = RLock()
2014-09-30 16:15:32 +00:00
self._lock = lock
2013-10-11 17:28:32 +00:00
# Export the lock's acquire() and release() methods
self.acquire = lock.acquire
self.release = lock.release
# If the lock defines _release_save() and/or _acquire_restore(),
# these override the default implementations (which just call
# release() and acquire() on the lock). Ditto for _is_owned().
try:
self._release_save = lock._release_save
except AttributeError:
pass
try:
self._acquire_restore = lock._acquire_restore
except AttributeError:
pass
try:
self._is_owned = lock._is_owned
except AttributeError:
pass
2014-09-30 16:15:32 +00:00
self._waiters = _deque()
2013-10-11 17:28:32 +00:00
def __enter__(self):
2014-09-30 16:15:32 +00:00
return self._lock.__enter__()
2013-10-11 17:28:32 +00:00
def __exit__(self, *args):
2014-09-30 16:15:32 +00:00
return self._lock.__exit__(*args)
2013-10-11 17:28:32 +00:00
def __repr__(self):
2014-09-30 16:15:32 +00:00
return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
2013-10-11 17:28:32 +00:00
def _release_save(self):
2014-09-30 16:15:32 +00:00
self._lock.release() # No state to save
2013-10-11 17:28:32 +00:00
def _acquire_restore(self, x):
2014-09-30 16:15:32 +00:00
self._lock.acquire() # Ignore saved state
2013-10-11 17:28:32 +00:00
def _is_owned(self):
# Return True if lock is owned by current_thread.
# This method is called only if __lock doesn't have _is_owned().
2014-09-30 16:15:32 +00:00
if self._lock.acquire(0):
self._lock.release()
2013-10-11 17:28:32 +00:00
return False
else:
return True
def wait(self, timeout=None):
"""Wait until notified or until a timeout occurs.
If the calling thread has not acquired the lock when this method is
called, a RuntimeError is raised.
This method releases the underlying lock, and then blocks until it is
2014-09-30 16:15:32 +00:00
awakened by a notify() or notify_all() call for the same condition
2013-10-11 17:28:32 +00:00
variable in another thread, or until the optional timeout occurs. Once
awakened or timed out, it re-acquires the lock and returns.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof).
When the underlying lock is an RLock, it is not released using its
release() method, since this may not actually unlock the lock when it
was acquired multiple times recursively. Instead, an internal interface
of the RLock class is used, which really unlocks it even when it has
been recursively acquired several times. Another internal interface is
then used to restore the recursion level when the lock is reacquired.
"""
if not self._is_owned():
raise RuntimeError("cannot wait on un-acquired lock")
waiter = _allocate_lock()
waiter.acquire()
2014-09-30 16:15:32 +00:00
self._waiters.append(waiter)
2013-10-11 17:28:32 +00:00
saved_state = self._release_save()
try: # restore state no matter what (e.g., KeyboardInterrupt)
if timeout is None:
waiter.acquire()
2014-09-30 16:15:32 +00:00
gotit = True
2013-10-11 17:28:32 +00:00
else:
2014-09-30 16:15:32 +00:00
if timeout > 0:
gotit = waiter.acquire(True, timeout)
else:
gotit = waiter.acquire(False)
2013-10-11 17:28:32 +00:00
if not gotit:
try:
2014-09-30 16:15:32 +00:00
self._waiters.remove(waiter)
2013-10-11 17:28:32 +00:00
except ValueError:
pass
2014-09-30 16:15:32 +00:00
return gotit
2013-10-11 17:28:32 +00:00
finally:
self._acquire_restore(saved_state)
2014-09-30 16:15:32 +00:00
def wait_for(self, predicate, timeout=None):
"""Wait until a condition evaluates to True.
predicate should be a callable which result will be interpreted as a
boolean value. A timeout may be provided giving the maximum time to
wait.
"""
endtime = None
waittime = timeout
result = predicate()
while not result:
if waittime is not None:
if endtime is None:
endtime = _time() + waittime
else:
waittime = endtime - _time()
if waittime <= 0:
break
self.wait(waittime)
result = predicate()
return result
2013-10-11 17:28:32 +00:00
def notify(self, n=1):
"""Wake up one or more threads waiting on this condition, if any.
If the calling thread has not acquired the lock when this method is
called, a RuntimeError is raised.
This method wakes up at most n of the threads waiting for the condition
variable; it is a no-op if no threads are waiting.
"""
if not self._is_owned():
raise RuntimeError("cannot notify on un-acquired lock")
2014-09-30 16:15:32 +00:00
all_waiters = self._waiters
waiters_to_notify = _deque(_islice(all_waiters, n))
if not waiters_to_notify:
2013-10-11 17:28:32 +00:00
return
2014-09-30 16:15:32 +00:00
for waiter in waiters_to_notify:
2013-10-11 17:28:32 +00:00
waiter.release()
try:
2014-09-30 16:15:32 +00:00
all_waiters.remove(waiter)
2013-10-11 17:28:32 +00:00
except ValueError:
pass
2014-09-30 16:15:32 +00:00
def notify_all(self):
2013-10-11 17:28:32 +00:00
"""Wake up all threads waiting on this condition.
If the calling thread has not acquired the lock when this method
is called, a RuntimeError is raised.
"""
2014-09-30 16:15:32 +00:00
self.notify(len(self._waiters))
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
notifyAll = notify_all
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
class Semaphore:
"""This class implements semaphore objects.
2013-10-11 17:28:32 +00:00
Semaphores manage a counter representing the number of release() calls minus
the number of acquire() calls, plus an initial value. The acquire() method
blocks if necessary until it can return without making the counter
negative. If not given, value defaults to 1.
"""
# After Tim Peters' semaphore class, but not quite the same (no maximum)
2014-09-30 16:15:32 +00:00
def __init__(self, value=1):
2013-10-11 17:28:32 +00:00
if value < 0:
raise ValueError("semaphore initial value must be >= 0")
2014-09-30 16:15:32 +00:00
self._cond = Condition(Lock())
self._value = value
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def acquire(self, blocking=True, timeout=None):
2013-10-11 17:28:32 +00:00
"""Acquire a semaphore, decrementing the internal counter by one.
When invoked without arguments: if the internal counter is larger than
zero on entry, decrement it by one and return immediately. If it is zero
on entry, block, waiting until some other thread has called release() to
make it larger than zero. This is done with proper interlocking so that
if multiple acquire() calls are blocked, release() will wake exactly one
of them up. The implementation may pick one at random, so the order in
which blocked threads are awakened should not be relied on. There is no
return value in this case.
When invoked with blocking set to true, do the same thing as when called
without arguments, and return true.
When invoked with blocking set to false, do not block. If a call without
an argument would block, return false immediately; otherwise, do the
same thing as when called without arguments, and return true.
2014-09-30 16:15:32 +00:00
When invoked with a timeout other than None, it will block for at
most timeout seconds. If acquire does not complete successfully in
that interval, return false. Return true otherwise.
2013-10-11 17:28:32 +00:00
"""
2014-09-30 16:15:32 +00:00
if not blocking and timeout is not None:
raise ValueError("can't specify timeout for non-blocking acquire")
2013-10-11 17:28:32 +00:00
rc = False
2014-09-30 16:15:32 +00:00
endtime = None
with self._cond:
while self._value == 0:
2013-10-11 17:28:32 +00:00
if not blocking:
break
2014-09-30 16:15:32 +00:00
if timeout is not None:
if endtime is None:
endtime = _time() + timeout
else:
timeout = endtime - _time()
if timeout <= 0:
break
self._cond.wait(timeout)
2013-10-11 17:28:32 +00:00
else:
2014-09-30 16:15:32 +00:00
self._value -= 1
2013-10-11 17:28:32 +00:00
rc = True
return rc
__enter__ = acquire
def release(self):
"""Release a semaphore, incrementing the internal counter by one.
When the counter is zero on entry and another thread is waiting for it
to become larger than zero again, wake up that thread.
"""
2014-09-30 16:15:32 +00:00
with self._cond:
self._value += 1
self._cond.notify()
2013-10-11 17:28:32 +00:00
def __exit__(self, t, v, tb):
self.release()
2014-09-30 16:15:32 +00:00
class BoundedSemaphore(Semaphore):
"""Implements a bounded semaphore.
2013-10-11 17:28:32 +00:00
A bounded semaphore checks to make sure its current value doesn't exceed its
initial value. If it does, ValueError is raised. In most situations
semaphores are used to guard resources with limited capacity.
If the semaphore is released too many times it's a sign of a bug. If not
given, value defaults to 1.
Like regular semaphores, bounded semaphores manage a counter representing
the number of release() calls minus the number of acquire() calls, plus an
initial value. The acquire() method blocks if necessary until it can return
without making the counter negative. If not given, value defaults to 1.
"""
2014-09-30 16:15:32 +00:00
def __init__(self, value=1):
Semaphore.__init__(self, value)
2013-10-11 17:28:32 +00:00
self._initial_value = value
def release(self):
"""Release a semaphore, incrementing the internal counter by one.
When the counter is zero on entry and another thread is waiting for it
to become larger than zero again, wake up that thread.
If the number of releases exceeds the number of acquires,
raise a ValueError.
"""
2014-09-30 16:15:32 +00:00
with self._cond:
if self._value >= self._initial_value:
2013-10-11 17:28:32 +00:00
raise ValueError("Semaphore released too many times")
2014-09-30 16:15:32 +00:00
self._value += 1
self._cond.notify()
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
class Event:
"""Class implementing event objects.
2013-10-11 17:28:32 +00:00
Events manage a flag that can be set to true with the set() method and reset
to false with the clear() method. The wait() method blocks until the flag is
2014-09-30 16:15:32 +00:00
true. The flag is initially false.
2013-10-11 17:28:32 +00:00
"""
# After Tim Peters' event class (without is_posted())
2014-09-30 16:15:32 +00:00
def __init__(self):
self._cond = Condition(Lock())
self._flag = False
2013-10-11 17:28:32 +00:00
def _reset_internal_locks(self):
# private! called by Thread._reset_internal_locks by _after_fork()
2014-09-30 16:15:32 +00:00
self._cond.__init__()
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def is_set(self):
"""Return true if and only if the internal flag is true."""
return self._flag
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
isSet = is_set
2013-10-11 17:28:32 +00:00
def set(self):
"""Set the internal flag to true.
2014-09-30 16:15:32 +00:00
All threads waiting for it to become true are awakened. Threads
2013-10-11 17:28:32 +00:00
that call wait() once the flag is true will not block at all.
"""
2014-09-30 16:15:32 +00:00
self._cond.acquire()
2013-10-11 17:28:32 +00:00
try:
2014-09-30 16:15:32 +00:00
self._flag = True
self._cond.notify_all()
2013-10-11 17:28:32 +00:00
finally:
2014-09-30 16:15:32 +00:00
self._cond.release()
2013-10-11 17:28:32 +00:00
def clear(self):
"""Reset the internal flag to false.
Subsequently, threads calling wait() will block until set() is called to
set the internal flag to true again.
"""
2014-09-30 16:15:32 +00:00
self._cond.acquire()
2013-10-11 17:28:32 +00:00
try:
2014-09-30 16:15:32 +00:00
self._flag = False
2013-10-11 17:28:32 +00:00
finally:
2014-09-30 16:15:32 +00:00
self._cond.release()
2013-10-11 17:28:32 +00:00
def wait(self, timeout=None):
"""Block until the internal flag is true.
If the internal flag is true on entry, return immediately. Otherwise,
block until another thread calls set() to set the flag to true, or until
the optional timeout occurs.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof).
This method returns the internal flag on exit, so it will always return
True except if a timeout is given and the operation times out.
"""
2014-09-30 16:15:32 +00:00
self._cond.acquire()
2013-10-11 17:28:32 +00:00
try:
2014-09-30 16:15:32 +00:00
signaled = self._flag
if not signaled:
signaled = self._cond.wait(timeout)
return signaled
2013-10-11 17:28:32 +00:00
finally:
2014-09-30 16:15:32 +00:00
self._cond.release()
# A barrier class. Inspired in part by the pthread_barrier_* api and
# the CyclicBarrier class from Java. See
# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
# CyclicBarrier.html
# for information.
# We maintain two main states, 'filling' and 'draining' enabling the barrier
# to be cyclic. Threads are not allowed into it until it has fully drained
# since the previous cycle. In addition, a 'resetting' state exists which is
# similar to 'draining' except that threads leave with a BrokenBarrierError,
# and a 'broken' state in which all threads get the exception.
class Barrier:
"""Implements a Barrier.
Useful for synchronizing a fixed number of threads at known synchronization
points. Threads block on 'wait()' and are simultaneously once they have all
made that call.
"""
def __init__(self, parties, action=None, timeout=None):
"""Create a barrier, initialised to 'parties' threads.
'action' is a callable which, when supplied, will be called by one of
the threads after they have all entered the barrier and just prior to
releasing them all. If a 'timeout' is provided, it is uses as the
default for all subsequent 'wait()' calls.
"""
self._cond = Condition(Lock())
self._action = action
self._timeout = timeout
self._parties = parties
self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
self._count = 0
def wait(self, timeout=None):
"""Wait for the barrier.
When the specified number of threads have started waiting, they are all
simultaneously awoken. If an 'action' was provided for the barrier, one
of the threads will have executed that callback prior to returning.
Returns an individual index number from 0 to 'parties-1'.
"""
if timeout is None:
timeout = self._timeout
with self._cond:
self._enter() # Block while the barrier drains.
index = self._count
self._count += 1
try:
if index + 1 == self._parties:
# We release the barrier
self._release()
else:
# We wait until someone releases us
self._wait(timeout)
return index
finally:
self._count -= 1
# Wake up any threads waiting for barrier to drain.
self._exit()
# Block until the barrier is ready for us, or raise an exception
# if it is broken.
def _enter(self):
while self._state in (-1, 1):
# It is draining or resetting, wait until done
self._cond.wait()
#see if the barrier is in a broken state
if self._state < 0:
raise BrokenBarrierError
assert self._state == 0
# Optionally run the 'action' and release the threads waiting
# in the barrier.
def _release(self):
try:
if self._action:
self._action()
# enter draining state
self._state = 1
self._cond.notify_all()
except:
#an exception during the _action handler. Break and reraise
self._break()
raise
# Wait in the barrier until we are relased. Raise an exception
# if the barrier is reset or broken.
def _wait(self, timeout):
if not self._cond.wait_for(lambda : self._state != 0, timeout):
#timed out. Break the barrier
self._break()
raise BrokenBarrierError
if self._state < 0:
raise BrokenBarrierError
assert self._state == 1
# If we are the last thread to exit the barrier, signal any threads
# waiting for the barrier to drain.
def _exit(self):
if self._count == 0:
if self._state in (-1, 1):
#resetting or draining
self._state = 0
self._cond.notify_all()
def reset(self):
"""Reset the barrier to the initial state.
Any threads currently waiting will get the BrokenBarrier exception
raised.
"""
with self._cond:
if self._count > 0:
if self._state == 0:
#reset the barrier, waking up threads
self._state = -1
elif self._state == -2:
#was broken, set it to reset state
#which clears when the last thread exits
self._state = -1
else:
self._state = 0
self._cond.notify_all()
def abort(self):
"""Place the barrier into a 'broken' state.
Useful in case of error. Any currently waiting threads and threads
attempting to 'wait()' will have BrokenBarrierError raised.
"""
with self._cond:
self._break()
def _break(self):
# An internal error was detected. The barrier is set to
# a broken state all parties awakened.
self._state = -2
self._cond.notify_all()
@property
def parties(self):
"""Return the number of threads required to trip the barrier."""
return self._parties
@property
def n_waiting(self):
"""Return the number of threads currently waiting at the barrier."""
# We don't need synchronization here since this is an ephemeral result
# anyway. It returns the correct value in the steady state.
if self._state == 0:
return self._count
return 0
@property
def broken(self):
"""Return True if the barrier is in a broken state."""
return self._state == -2
# exception raised by the Barrier class
class BrokenBarrierError(RuntimeError):
pass
2013-10-11 17:28:32 +00:00
# Helper to generate new thread names
_counter = 0
def _newname(template="Thread-%d"):
global _counter
2014-09-30 16:15:32 +00:00
_counter += 1
2013-10-11 17:28:32 +00:00
return template % _counter
# Active thread administration
_active_limbo_lock = _allocate_lock()
_active = {} # maps thread id to Thread object
_limbo = {}
2014-09-30 16:15:32 +00:00
_dangling = WeakSet()
2013-10-11 17:28:32 +00:00
# Main class for threads
2014-09-30 16:15:32 +00:00
class Thread:
2013-10-11 17:28:32 +00:00
"""A class that represents a thread of control.
2014-09-30 16:15:32 +00:00
This class can be safely subclassed in a limited fashion. There are two ways
to specify the activity: by passing a callable object to the constructor, or
by overriding the run() method in a subclass.
2013-10-11 17:28:32 +00:00
"""
2014-09-30 16:15:32 +00:00
2013-10-11 17:28:32 +00:00
__initialized = False
# Need to store a reference to sys.exc_info for printing
# out exceptions when a thread tries to use a global var. during interp.
# shutdown and thus raises an exception about trying to perform some
# operation on/with a NoneType
__exc_info = _sys.exc_info
# Keep sys.exc_clear too to clear the exception just before
# allowing .join() to return.
2014-09-30 16:15:32 +00:00
#XXX __exc_clear = _sys.exc_clear
2013-10-11 17:28:32 +00:00
def __init__(self, group=None, target=None, name=None,
2014-09-30 16:15:32 +00:00
args=(), kwargs=None, *, daemon=None):
2013-10-11 17:28:32 +00:00
"""This constructor should always be called with keyword arguments. Arguments are:
*group* should be None; reserved for future extension when a ThreadGroup
class is implemented.
*target* is the callable object to be invoked by the run()
method. Defaults to None, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of
the form "Thread-N" where N is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ().
*kwargs* is a dictionary of keyword arguments for the target
invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke
the base class constructor (Thread.__init__()) before doing anything
else to the thread.
2014-09-30 16:15:32 +00:00
"""
2013-10-11 17:28:32 +00:00
assert group is None, "group argument must be None for now"
if kwargs is None:
kwargs = {}
2014-09-30 16:15:32 +00:00
self._target = target
self._name = str(name or _newname())
self._args = args
self._kwargs = kwargs
if daemon is not None:
self._daemonic = daemon
else:
self._daemonic = current_thread().daemon
self._ident = None
self._tstate_lock = None
self._started = Event()
self._is_stopped = False
self._initialized = True
2013-10-11 17:28:32 +00:00
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
2014-09-30 16:15:32 +00:00
self._stderr = _sys.stderr
# For debugging and _after_fork()
_dangling.add(self)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def _reset_internal_locks(self, is_alive):
2013-10-11 17:28:32 +00:00
# private! Called by _after_fork() to reset our internal locks as
# they may be in an invalid state leading to a deadlock or crash.
2014-09-30 16:15:32 +00:00
self._started._reset_internal_locks()
if is_alive:
self._set_tstate_lock()
else:
# The thread isn't alive after fork: it doesn't have a tstate
# anymore.
self._is_stopped = True
self._tstate_lock = None
2013-10-11 17:28:32 +00:00
def __repr__(self):
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() was not called"
2013-10-11 17:28:32 +00:00
status = "initial"
2014-09-30 16:15:32 +00:00
if self._started.is_set():
2013-10-11 17:28:32 +00:00
status = "started"
2014-09-30 16:15:32 +00:00
self.is_alive() # easy way to get ._is_stopped set when appropriate
if self._is_stopped:
2013-10-11 17:28:32 +00:00
status = "stopped"
2014-09-30 16:15:32 +00:00
if self._daemonic:
2013-10-11 17:28:32 +00:00
status += " daemon"
2014-09-30 16:15:32 +00:00
if self._ident is not None:
status += " %s" % self._ident
return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
2013-10-11 17:28:32 +00:00
def start(self):
"""Start the thread's activity.
It must be called at most once per thread object. It arranges for the
object's run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the
same thread object.
"""
2014-09-30 16:15:32 +00:00
if not self._initialized:
2013-10-11 17:28:32 +00:00
raise RuntimeError("thread.__init__() not called")
2014-09-30 16:15:32 +00:00
if self._started.is_set():
2013-10-11 17:28:32 +00:00
raise RuntimeError("threads can only be started once")
with _active_limbo_lock:
_limbo[self] = self
try:
2014-09-30 16:15:32 +00:00
_start_new_thread(self._bootstrap, ())
2013-10-11 17:28:32 +00:00
except Exception:
with _active_limbo_lock:
del _limbo[self]
raise
2014-09-30 16:15:32 +00:00
self._started.wait()
2013-10-11 17:28:32 +00:00
def run(self):
"""Method representing the thread's activity.
You may override this method in a subclass. The standard run() method
invokes the callable object passed to the object's constructor as the
target argument, if any, with sequential and keyword arguments taken
from the args and kwargs arguments, respectively.
"""
try:
2014-09-30 16:15:32 +00:00
if self._target:
self._target(*self._args, **self._kwargs)
2013-10-11 17:28:32 +00:00
finally:
# Avoid a refcycle if the thread is running a function with
# an argument that has a member that points to the thread.
2014-09-30 16:15:32 +00:00
del self._target, self._args, self._kwargs
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def _bootstrap(self):
2013-10-11 17:28:32 +00:00
# Wrapper around the real bootstrap code that ignores
# exceptions during interpreter cleanup. Those typically
# happen when a daemon thread wakes up at an unfortunate
# moment, finds the world around it destroyed, and raises some
# random exception *** while trying to report the exception in
2014-09-30 16:15:32 +00:00
# _bootstrap_inner() below ***. Those random exceptions
2013-10-11 17:28:32 +00:00
# don't help anybody, and they confuse users, so we suppress
# them. We suppress them only when it appears that the world
# indeed has already been destroyed, so that exceptions in
2014-09-30 16:15:32 +00:00
# _bootstrap_inner() during normal business hours are properly
2013-10-11 17:28:32 +00:00
# reported. Also, we only suppress them for daemonic threads;
# if a non-daemonic encounters this, something else is wrong.
try:
2014-09-30 16:15:32 +00:00
self._bootstrap_inner()
2013-10-11 17:28:32 +00:00
except:
2014-09-30 16:15:32 +00:00
if self._daemonic and _sys is None:
2013-10-11 17:28:32 +00:00
return
raise
def _set_ident(self):
2014-09-30 16:15:32 +00:00
self._ident = get_ident()
def _set_tstate_lock(self):
"""
Set a lock object which will be released by the interpreter when
the underlying thread state (see pystate.h) gets deleted.
"""
self._tstate_lock = _set_sentinel()
self._tstate_lock.acquire()
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def _bootstrap_inner(self):
2013-10-11 17:28:32 +00:00
try:
self._set_ident()
2014-09-30 16:15:32 +00:00
self._set_tstate_lock()
self._started.set()
2013-10-11 17:28:32 +00:00
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
_active[self._ident] = self
2013-10-11 17:28:32 +00:00
del _limbo[self]
if _trace_hook:
_sys.settrace(_trace_hook)
if _profile_hook:
_sys.setprofile(_profile_hook)
try:
self.run()
except SystemExit:
2014-09-30 16:15:32 +00:00
pass
2013-10-11 17:28:32 +00:00
except:
# If sys.stderr is no more (most likely from interpreter
2014-09-30 16:15:32 +00:00
# shutdown) use self._stderr. Otherwise still use sys (as in
2013-10-11 17:28:32 +00:00
# _sys) in case sys.stderr was redefined since the creation of
# self.
if _sys:
_sys.stderr.write("Exception in thread %s:\n%s\n" %
(self.name, _format_exc()))
else:
# Do the best job possible w/o a huge amt. of code to
# approximate a traceback (code ideas from
# Lib/traceback.py)
2014-09-30 16:15:32 +00:00
exc_type, exc_value, exc_tb = self._exc_info()
2013-10-11 17:28:32 +00:00
try:
2014-09-30 16:15:32 +00:00
print((
2013-10-11 17:28:32 +00:00
"Exception in thread " + self.name +
2014-09-30 16:15:32 +00:00
" (most likely raised during interpreter shutdown):"), file=self._stderr)
print((
"Traceback (most recent call last):"), file=self._stderr)
2013-10-11 17:28:32 +00:00
while exc_tb:
2014-09-30 16:15:32 +00:00
print((
2013-10-11 17:28:32 +00:00
' File "%s", line %s, in %s' %
(exc_tb.tb_frame.f_code.co_filename,
exc_tb.tb_lineno,
2014-09-30 16:15:32 +00:00
exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
2013-10-11 17:28:32 +00:00
exc_tb = exc_tb.tb_next
2014-09-30 16:15:32 +00:00
print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
2013-10-11 17:28:32 +00:00
# Make sure that exc_tb gets deleted since it is a memory
# hog; deleting everything else is just for thoroughness
finally:
del exc_type, exc_value, exc_tb
finally:
# Prevent a race in
# test_threading.test_no_refcycle_through_target when
# the exception keeps the target alive past when we
# assert that it's dead.
2014-09-30 16:15:32 +00:00
#XXX self.__exc_clear()
pass
2013-10-11 17:28:32 +00:00
finally:
with _active_limbo_lock:
try:
2014-09-30 16:15:32 +00:00
# We don't call self._delete() because it also
2013-10-11 17:28:32 +00:00
# grabs _active_limbo_lock.
2014-09-30 16:15:32 +00:00
del _active[get_ident()]
2013-10-11 17:28:32 +00:00
except:
pass
2014-09-30 16:15:32 +00:00
def _stop(self):
# After calling ._stop(), .is_alive() returns False and .join() returns
# immediately. ._tstate_lock must be released before calling ._stop().
#
# Normal case: C code at the end of the thread's life
# (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
# that's detected by our ._wait_for_tstate_lock(), called by .join()
# and .is_alive(). Any number of threads _may_ call ._stop()
# simultaneously (for example, if multiple threads are blocked in
# .join() calls), and they're not serialized. That's harmless -
# they'll just make redundant rebindings of ._is_stopped and
# ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
# "assert self._is_stopped" in ._wait_for_tstate_lock() always works
# (the assert is executed only if ._tstate_lock is None).
#
# Special case: _main_thread releases ._tstate_lock via this
# module's _shutdown() function.
lock = self._tstate_lock
if lock is not None:
assert not lock.locked()
self._is_stopped = True
self._tstate_lock = None
def _delete(self):
2013-10-11 17:28:32 +00:00
"Remove current thread from the dict of currently running threads."
2014-09-30 16:15:32 +00:00
# Notes about running with _dummy_thread:
2013-10-11 17:28:32 +00:00
#
2014-09-30 16:15:32 +00:00
# Must take care to not raise an exception if _dummy_thread is being
2013-10-11 17:28:32 +00:00
# used (and thus this module is being used as an instance of
2014-09-30 16:15:32 +00:00
# dummy_threading). _dummy_thread.get_ident() always returns -1 since
# there is only one thread if _dummy_thread is being used. Thus
2013-10-11 17:28:32 +00:00
# len(_active) is always <= 1 here, and any Thread instance created
# overwrites the (if any) thread currently registered in _active.
#
# An instance of _MainThread is always created by 'threading'. This
# gets overwritten the instant an instance of Thread is created; both
2014-09-30 16:15:32 +00:00
# threads return -1 from _dummy_thread.get_ident() and thus have the
2013-10-11 17:28:32 +00:00
# same key in the dict. So when the _MainThread instance created by
# 'threading' tries to clean itself up when atexit calls this method
# it gets a KeyError if another Thread instance was created.
#
# This all means that KeyError from trying to delete something from
# _active if dummy_threading is being used is a red herring. But
# since it isn't if dummy_threading is *not* being used then don't
# hide the exception.
try:
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
del _active[get_ident()]
2013-10-11 17:28:32 +00:00
# There must not be any python code between the previous line
# and after the lock is released. Otherwise a tracing function
# could try to acquire the lock again in the same thread, (in
# current_thread()), and would block.
except KeyError:
if 'dummy_threading' not in _sys.modules:
raise
def join(self, timeout=None):
"""Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method is
called terminates -- either normally or through an unhandled exception
or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof). As join() always returns None, you must call
isAlive() after join() to decide whether a timeout happened -- if the
thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will
block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current
thread as that would cause a deadlock. It is also an error to join() a
thread before it has been started and attempts to do so raises the same
exception.
"""
2014-09-30 16:15:32 +00:00
if not self._initialized:
2013-10-11 17:28:32 +00:00
raise RuntimeError("Thread.__init__() not called")
2014-09-30 16:15:32 +00:00
if not self._started.is_set():
2013-10-11 17:28:32 +00:00
raise RuntimeError("cannot join thread before it is started")
if self is current_thread():
raise RuntimeError("cannot join current thread")
2014-09-30 16:15:32 +00:00
if timeout is None:
self._wait_for_tstate_lock()
else:
# the behavior of a negative timeout isn't documented, but
# historically .join(timeout=x) for x<0 has acted as if timeout=0
self._wait_for_tstate_lock(timeout=max(timeout, 0))
def _wait_for_tstate_lock(self, block=True, timeout=-1):
# Issue #18808: wait for the thread state to be gone.
# At the end of the thread's life, after all knowledge of the thread
# is removed from C data structures, C code releases our _tstate_lock.
# This method passes its arguments to _tstate_lock.aquire().
# If the lock is acquired, the C code is done, and self._stop() is
# called. That sets ._is_stopped to True, and ._tstate_lock to None.
lock = self._tstate_lock
if lock is None: # already determined that the C code is done
assert self._is_stopped
elif lock.acquire(block, timeout):
lock.release()
self._stop()
2013-10-11 17:28:32 +00:00
@property
def name(self):
"""A string used for identification purposes only.
It has no semantics. Multiple threads may be given the same name. The
initial name is set by the constructor.
"""
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() not called"
return self._name
2013-10-11 17:28:32 +00:00
@name.setter
def name(self, name):
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() not called"
self._name = str(name)
2013-10-11 17:28:32 +00:00
@property
def ident(self):
"""Thread identifier of this thread or None if it has not been started.
This is a nonzero integer. See the thread.get_ident() function. Thread
identifiers may be recycled when a thread exits and another thread is
created. The identifier is available even after the thread has exited.
"""
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() not called"
return self._ident
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def is_alive(self):
2013-10-11 17:28:32 +00:00
"""Return whether the thread is alive.
This method returns True just before the run() method starts until just
after the run() method terminates. The module function enumerate()
returns a list of all alive threads.
"""
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() not called"
if self._is_stopped or not self._started.is_set():
return False
self._wait_for_tstate_lock(False)
return not self._is_stopped
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
isAlive = is_alive
2013-10-11 17:28:32 +00:00
@property
def daemon(self):
2014-09-30 16:15:32 +00:00
"""A boolean value indicating whether this thread is a daemon thread.
2013-10-11 17:28:32 +00:00
This must be set before start() is called, otherwise RuntimeError is
raised. Its initial value is inherited from the creating thread; the
main thread is not a daemon thread and therefore all threads created in
the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are
left.
"""
2014-09-30 16:15:32 +00:00
assert self._initialized, "Thread.__init__() not called"
return self._daemonic
2013-10-11 17:28:32 +00:00
@daemon.setter
def daemon(self, daemonic):
2014-09-30 16:15:32 +00:00
if not self._initialized:
2013-10-11 17:28:32 +00:00
raise RuntimeError("Thread.__init__() not called")
2014-09-30 16:15:32 +00:00
if self._started.is_set():
raise RuntimeError("cannot set daemon status of active thread")
self._daemonic = daemonic
2013-10-11 17:28:32 +00:00
def isDaemon(self):
return self.daemon
def setDaemon(self, daemonic):
self.daemon = daemonic
def getName(self):
return self.name
def setName(self, name):
self.name = name
# The timer class was contributed by Itamar Shtull-Trauring
2014-09-30 16:15:32 +00:00
class Timer(Thread):
2013-10-11 17:28:32 +00:00
"""Call a function after a specified number of seconds:
2014-09-30 16:15:32 +00:00
t = Timer(30.0, f, args=None, kwargs=None)
2013-10-11 17:28:32 +00:00
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
2014-09-30 16:15:32 +00:00
def __init__(self, interval, function, args=None, kwargs=None):
2013-10-11 17:28:32 +00:00
Thread.__init__(self)
self.interval = interval
self.function = function
2014-09-30 16:15:32 +00:00
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
2013-10-11 17:28:32 +00:00
self.finished = Event()
def cancel(self):
2014-09-30 16:15:32 +00:00
"""Stop the timer if it hasn't finished yet."""
2013-10-11 17:28:32 +00:00
self.finished.set()
def run(self):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.set()
# Special thread class to represent the main thread
# This is garbage collected through an exit handler
class _MainThread(Thread):
def __init__(self):
2014-09-30 16:15:32 +00:00
Thread.__init__(self, name="MainThread", daemon=False)
self._set_tstate_lock()
self._started.set()
2013-10-11 17:28:32 +00:00
self._set_ident()
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
_active[self._ident] = self
2013-10-11 17:28:32 +00:00
# Dummy thread class to represent threads not started here.
# These aren't garbage collected when they die, nor can they be waited for.
# If they invoke anything in threading.py that calls current_thread(), they
# leave an entry in the _active dict forever after.
# Their purpose is to return *something* from current_thread().
# They are marked as daemon threads so we won't wait for them
# when we exit (conform previous semantics).
class _DummyThread(Thread):
def __init__(self):
2014-09-30 16:15:32 +00:00
Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
self._started.set()
2013-10-11 17:28:32 +00:00
self._set_ident()
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
_active[self._ident] = self
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def _stop(self):
pass
2013-10-11 17:28:32 +00:00
def join(self, timeout=None):
assert False, "cannot join a dummy thread"
# Global API functions
2014-09-30 16:15:32 +00:00
def current_thread():
2013-10-11 17:28:32 +00:00
"""Return the current Thread object, corresponding to the caller's thread of control.
If the caller's thread of control was not created through the threading
module, a dummy thread object with limited functionality is returned.
"""
try:
2014-09-30 16:15:32 +00:00
return _active[get_ident()]
2013-10-11 17:28:32 +00:00
except KeyError:
return _DummyThread()
2014-09-30 16:15:32 +00:00
currentThread = current_thread
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
def active_count():
2013-10-11 17:28:32 +00:00
"""Return the number of Thread objects currently alive.
The returned count is equal to the length of the list returned by
enumerate().
"""
with _active_limbo_lock:
return len(_active) + len(_limbo)
2014-09-30 16:15:32 +00:00
activeCount = active_count
2013-10-11 17:28:32 +00:00
def _enumerate():
# Same as enumerate(), but without the lock. Internal use only.
2014-09-30 16:15:32 +00:00
return list(_active.values()) + list(_limbo.values())
2013-10-11 17:28:32 +00:00
def enumerate():
"""Return a list of all Thread objects currently alive.
The list includes daemonic threads, dummy thread objects created by
current_thread(), and the main thread. It excludes terminated threads and
threads that have not yet been started.
"""
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
return list(_active.values()) + list(_limbo.values())
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
from _thread import stack_size
2013-10-11 17:28:32 +00:00
# Create the main thread object,
# and make it available for the interpreter
# (Py_Main) as threading._shutdown.
2014-09-30 16:15:32 +00:00
_main_thread = _MainThread()
def _shutdown():
# Obscure: other threads may be waiting to join _main_thread. That's
# dubious, but some code does it. We can't wait for C code to release
# the main thread's tstate_lock - that won't happen until the interpreter
# is nearly dead. So we release it here. Note that just calling _stop()
# isn't enough: other threads may already be waiting on _tstate_lock.
tlock = _main_thread._tstate_lock
# The main thread isn't finished yet, so its thread state lock can't have
# been released.
assert tlock is not None
assert tlock.locked()
tlock.release()
_main_thread._stop()
t = _pickSomeNonDaemonThread()
while t:
t.join()
t = _pickSomeNonDaemonThread()
_main_thread._delete()
def _pickSomeNonDaemonThread():
for t in enumerate():
if not t.daemon and t.is_alive():
return t
return None
def main_thread():
"""Return the main thread object.
In normal conditions, the main thread is the thread from which the
Python interpreter was started.
"""
return _main_thread
2013-10-11 17:28:32 +00:00
# get thread-local implementation, either from the thread
# module, or from the python fallback
try:
2014-09-30 16:15:32 +00:00
from _thread import _local as local
2013-10-11 17:28:32 +00:00
except ImportError:
from _threading_local import local
def _after_fork():
# This function is called by Python/ceval.c:PyEval_ReInitThreads which
# is called from PyOS_AfterFork. Here we cleanup threading module state
# that should not exist after a fork.
# Reset _active_limbo_lock, in case we forked while the lock was held
# by another (non-forked) thread. http://bugs.python.org/issue874900
2014-09-30 16:15:32 +00:00
global _active_limbo_lock, _main_thread
2013-10-11 17:28:32 +00:00
_active_limbo_lock = _allocate_lock()
# fork() only copied the current thread; clear references to others.
new_active = {}
current = current_thread()
2014-09-30 16:15:32 +00:00
_main_thread = current
2013-10-11 17:28:32 +00:00
with _active_limbo_lock:
2014-09-30 16:15:32 +00:00
# Dangling thread instances must still have their locks reset,
# because someone may join() them.
threads = set(_enumerate())
threads.update(_dangling)
for thread in threads:
2013-10-11 17:28:32 +00:00
# Any lock/condition variable may be currently locked or in an
# invalid state, so we reinitialize them.
if thread is current:
# There is only one active thread. We reset the ident to
# its new value since it can have changed.
2014-09-30 16:15:32 +00:00
thread._reset_internal_locks(True)
ident = get_ident()
thread._ident = ident
2013-10-11 17:28:32 +00:00
new_active[ident] = thread
else:
# All the others are already stopped.
2014-09-30 16:15:32 +00:00
thread._reset_internal_locks(False)
thread._stop()
2013-10-11 17:28:32 +00:00
_limbo.clear()
_active.clear()
_active.update(new_active)
assert len(_active) == 1