12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352 |
- """Thread module emulating a subset of Java's threading model."""
- import sys as _sys
- import _thread
- from time import monotonic as _time
- from traceback import format_exc as _format_exc
- from _weakrefset import WeakSet
- from itertools import islice as _islice, count as _count
- try:
- from _collections import deque as _deque
- except ImportError:
- from collections import deque as _deque
- __all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
- 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
- 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
- _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
- _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
- 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.
- """
- 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.
- """
- def __init__(self):
- self._block = _allocate_lock()
- self._owner = None
- self._count = 0
- def __repr__(self):
- owner = self._owner
- try:
- owner = _active[owner].name
- except KeyError:
- pass
- return "<%s %s.%s object owner=%r count=%d at %s>" % (
- "locked" if self._block.locked() else "unlocked",
- self.__class__.__module__,
- self.__class__.__qualname__,
- owner,
- self._count,
- hex(id(self))
- )
- def acquire(self, blocking=True, timeout=-1):
- """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.
- 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.
- """
- me = get_ident()
- if self._owner == me:
- self._count += 1
- return 1
- rc = self._block.acquire(blocking, timeout)
- if rc:
- self._owner = me
- self._count = 1
- 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.
- """
- if self._owner != get_ident():
- raise RuntimeError("cannot release un-acquired lock")
- self._count = count = self._count - 1
- if not count:
- self._owner = None
- self._block.release()
- def __exit__(self, t, v, tb):
- self.release()
-
- def _acquire_restore(self, state):
- self._block.acquire()
- self._count, self._owner = state
- def _release_save(self):
- 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()
- return (count, owner)
- def _is_owned(self):
- return self._owner == get_ident()
- _PyRLock = _RLock
- class Condition:
- """Class that implements a condition variable.
- 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.
- """
- def __init__(self, lock=None):
- if lock is None:
- lock = RLock()
- self._lock = lock
-
- self.acquire = lock.acquire
- self.release = lock.release
-
-
-
- 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
- self._waiters = _deque()
- def __enter__(self):
- return self._lock.__enter__()
- def __exit__(self, *args):
- return self._lock.__exit__(*args)
- def __repr__(self):
- return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
- def _release_save(self):
- self._lock.release()
- def _acquire_restore(self, x):
- self._lock.acquire()
- def _is_owned(self):
-
-
- if self._lock.acquire(0):
- self._lock.release()
- 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
- awakened by a notify() or notify_all() call for the same condition
- 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()
- self._waiters.append(waiter)
- saved_state = self._release_save()
- gotit = False
- try:
- if timeout is None:
- waiter.acquire()
- gotit = True
- else:
- if timeout > 0:
- gotit = waiter.acquire(True, timeout)
- else:
- gotit = waiter.acquire(False)
- return gotit
- finally:
- self._acquire_restore(saved_state)
- if not gotit:
- try:
- self._waiters.remove(waiter)
- except ValueError:
- pass
- 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
- 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")
- all_waiters = self._waiters
- waiters_to_notify = _deque(_islice(all_waiters, n))
- if not waiters_to_notify:
- return
- for waiter in waiters_to_notify:
- waiter.release()
- try:
- all_waiters.remove(waiter)
- except ValueError:
- pass
- def notify_all(self):
- """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.
- """
- self.notify(len(self._waiters))
- notifyAll = notify_all
- class Semaphore:
- """This class implements semaphore objects.
- 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.
- """
-
- def __init__(self, value=1):
- if value < 0:
- raise ValueError("semaphore initial value must be >= 0")
- self._cond = Condition(Lock())
- self._value = value
- def acquire(self, blocking=True, timeout=None):
- """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.
- 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.
- """
- if not blocking and timeout is not None:
- raise ValueError("can't specify timeout for non-blocking acquire")
- rc = False
- endtime = None
- with self._cond:
- while self._value == 0:
- if not blocking:
- break
- if timeout is not None:
- if endtime is None:
- endtime = _time() + timeout
- else:
- timeout = endtime - _time()
- if timeout <= 0:
- break
- self._cond.wait(timeout)
- else:
- self._value -= 1
- 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.
- """
- with self._cond:
- self._value += 1
- self._cond.notify()
- def __exit__(self, t, v, tb):
- self.release()
- class BoundedSemaphore(Semaphore):
- """Implements a bounded semaphore.
- 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.
- """
- def __init__(self, value=1):
- Semaphore.__init__(self, value)
- 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.
- """
- with self._cond:
- if self._value >= self._initial_value:
- raise ValueError("Semaphore released too many times")
- self._value += 1
- self._cond.notify()
- class Event:
- """Class implementing event objects.
- 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
- true. The flag is initially false.
- """
-
- def __init__(self):
- self._cond = Condition(Lock())
- self._flag = False
- def _reset_internal_locks(self):
-
- self._cond.__init__(Lock())
- def is_set(self):
- """Return true if and only if the internal flag is true."""
- return self._flag
- isSet = is_set
- def set(self):
- """Set the internal flag to true.
- All threads waiting for it to become true are awakened. Threads
- that call wait() once the flag is true will not block at all.
- """
- with self._cond:
- self._flag = True
- self._cond.notify_all()
- 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.
- """
- with self._cond:
- self._flag = False
- 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.
- """
- with self._cond:
- signaled = self._flag
- if not signaled:
- signaled = self._cond.wait(timeout)
- return signaled
- 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
- 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()
- index = self._count
- self._count += 1
- try:
- if index + 1 == self._parties:
-
- self._release()
- else:
-
- self._wait(timeout)
- return index
- finally:
- self._count -= 1
-
- self._exit()
-
-
- def _enter(self):
- while self._state in (-1, 1):
-
- self._cond.wait()
-
- if self._state < 0:
- raise BrokenBarrierError
- assert self._state == 0
-
-
- def _release(self):
- try:
- if self._action:
- self._action()
-
- self._state = 1
- self._cond.notify_all()
- except:
-
- self._break()
- raise
-
-
- def _wait(self, timeout):
- if not self._cond.wait_for(lambda : self._state != 0, timeout):
-
- self._break()
- raise BrokenBarrierError
- if self._state < 0:
- raise BrokenBarrierError
- assert self._state == 1
-
-
- def _exit(self):
- if self._count == 0:
- if self._state in (-1, 1):
-
- 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:
-
- self._state = -1
- elif self._state == -2:
-
-
- 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):
-
-
- 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."""
-
-
- 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
- class BrokenBarrierError(RuntimeError):
- pass
- _counter = _count().__next__
- _counter()
- def _newname(template="Thread-%d"):
- return template % _counter()
- _active_limbo_lock = _allocate_lock()
- _active = {}
- _limbo = {}
- _dangling = WeakSet()
- class Thread:
- """A class that represents a thread of control.
- 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.
- """
- _initialized = False
-
-
-
-
- _exc_info = _sys.exc_info
-
-
-
- def __init__(self, group=None, target=None, name=None,
- args=(), kwargs=None, *, daemon=None):
- """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.
- """
- assert group is None, "group argument must be None for now"
- if kwargs is None:
- kwargs = {}
- 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
-
-
- self._stderr = _sys.stderr
-
- _dangling.add(self)
- def _reset_internal_locks(self, is_alive):
-
-
- self._started._reset_internal_locks()
- if is_alive:
- self._set_tstate_lock()
- else:
-
-
- self._is_stopped = True
- self._tstate_lock = None
- def __repr__(self):
- assert self._initialized, "Thread.__init__() was not called"
- status = "initial"
- if self._started.is_set():
- status = "started"
- self.is_alive()
- if self._is_stopped:
- status = "stopped"
- if self._daemonic:
- status += " daemon"
- if self._ident is not None:
- status += " %s" % self._ident
- return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
- 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.
- """
- if not self._initialized:
- raise RuntimeError("thread.__init__() not called")
- if self._started.is_set():
- raise RuntimeError("threads can only be started once")
- with _active_limbo_lock:
- _limbo[self] = self
- try:
- _start_new_thread(self._bootstrap, ())
- except Exception:
- with _active_limbo_lock:
- del _limbo[self]
- raise
- self._started.wait()
- 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:
- if self._target:
- self._target(*self._args, **self._kwargs)
- finally:
-
-
- del self._target, self._args, self._kwargs
- def _bootstrap(self):
-
-
-
-
-
-
-
-
-
-
-
-
- try:
- self._bootstrap_inner()
- except:
- if self._daemonic and _sys is None:
- return
- raise
- def _set_ident(self):
- 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()
- def _bootstrap_inner(self):
- try:
- self._set_ident()
- self._set_tstate_lock()
- self._started.set()
- with _active_limbo_lock:
- _active[self._ident] = self
- del _limbo[self]
- if _trace_hook:
- _sys.settrace(_trace_hook)
- if _profile_hook:
- _sys.setprofile(_profile_hook)
- try:
- self.run()
- except SystemExit:
- pass
- except:
-
-
-
-
- if _sys and _sys.stderr is not None:
- print("Exception in thread %s:\n%s" %
- (self.name, _format_exc()), file=_sys.stderr)
- elif self._stderr is not None:
-
-
-
- exc_type, exc_value, exc_tb = self._exc_info()
- try:
- print((
- "Exception in thread " + self.name +
- " (most likely raised during interpreter shutdown):"), file=self._stderr)
- print((
- "Traceback (most recent call last):"), file=self._stderr)
- while exc_tb:
- print((
- ' File "%s", line %s, in %s' %
- (exc_tb.tb_frame.f_code.co_filename,
- exc_tb.tb_lineno,
- exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
- exc_tb = exc_tb.tb_next
- print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
-
-
- finally:
- del exc_type, exc_value, exc_tb
- finally:
-
-
-
-
-
- pass
- finally:
- with _active_limbo_lock:
- try:
-
-
- del _active[get_ident()]
- except:
- pass
- def _stop(self):
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- lock = self._tstate_lock
- if lock is not None:
- assert not lock.locked()
- self._is_stopped = True
- self._tstate_lock = None
- def _delete(self):
- "Remove current thread from the dict of currently running threads."
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try:
- with _active_limbo_lock:
- del _active[get_ident()]
-
-
-
-
- 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.
- """
- if not self._initialized:
- raise RuntimeError("Thread.__init__() not called")
- if not self._started.is_set():
- raise RuntimeError("cannot join thread before it is started")
- if self is current_thread():
- raise RuntimeError("cannot join current thread")
- if timeout is None:
- self._wait_for_tstate_lock()
- else:
-
-
- self._wait_for_tstate_lock(timeout=max(timeout, 0))
- def _wait_for_tstate_lock(self, block=True, timeout=-1):
-
-
-
-
-
-
- lock = self._tstate_lock
- if lock is None:
- assert self._is_stopped
- elif lock.acquire(block, timeout):
- lock.release()
- self._stop()
- @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.
- """
- assert self._initialized, "Thread.__init__() not called"
- return self._name
- @name.setter
- def name(self, name):
- assert self._initialized, "Thread.__init__() not called"
- self._name = str(name)
- @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.
- """
- assert self._initialized, "Thread.__init__() not called"
- return self._ident
- def is_alive(self):
- """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.
- """
- 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
- isAlive = is_alive
- @property
- def daemon(self):
- """A boolean value indicating whether this thread is a daemon thread.
- 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.
- """
- assert self._initialized, "Thread.__init__() not called"
- return self._daemonic
- @daemon.setter
- def daemon(self, daemonic):
- if not self._initialized:
- raise RuntimeError("Thread.__init__() not called")
- if self._started.is_set():
- raise RuntimeError("cannot set daemon status of active thread")
- self._daemonic = daemonic
- 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
- class Timer(Thread):
- """Call a function after a specified number of seconds:
- t = Timer(30.0, f, args=None, kwargs=None)
- t.start()
- t.cancel() # stop the timer's action if it's still waiting
- """
- def __init__(self, interval, function, args=None, kwargs=None):
- Thread.__init__(self)
- self.interval = interval
- self.function = function
- self.args = args if args is not None else []
- self.kwargs = kwargs if kwargs is not None else {}
- self.finished = Event()
- def cancel(self):
- """Stop the timer if it hasn't finished yet."""
- 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()
- class _MainThread(Thread):
- def __init__(self):
- Thread.__init__(self, name="MainThread", daemon=False)
- self._set_tstate_lock()
- self._started.set()
- self._set_ident()
- with _active_limbo_lock:
- _active[self._ident] = self
- class _DummyThread(Thread):
- def __init__(self):
- Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
- self._started.set()
- self._set_ident()
- with _active_limbo_lock:
- _active[self._ident] = self
- def _stop(self):
- pass
- def join(self, timeout=None):
- assert False, "cannot join a dummy thread"
- def current_thread():
- """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:
- return _active[get_ident()]
- except KeyError:
- return _DummyThread()
- currentThread = current_thread
- def active_count():
- """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)
- activeCount = active_count
- def _enumerate():
-
- return list(_active.values()) + list(_limbo.values())
- 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:
- return list(_active.values()) + list(_limbo.values())
- from _thread import stack_size
- _main_thread = _MainThread()
- def _shutdown():
-
-
-
-
-
- tlock = _main_thread._tstate_lock
-
-
- 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
- try:
- from _thread import _local as local
- except ImportError:
- from _threading_local import local
- def _after_fork():
-
-
-
-
-
- global _active_limbo_lock, _main_thread
- _active_limbo_lock = _allocate_lock()
-
- new_active = {}
- current = current_thread()
- _main_thread = current
- with _active_limbo_lock:
-
-
- threads = set(_enumerate())
- threads.update(_dangling)
- for thread in threads:
-
-
- if thread is current:
-
-
- thread._reset_internal_locks(True)
- ident = get_ident()
- thread._ident = ident
- new_active[ident] = thread
- else:
-
- thread._reset_internal_locks(False)
- thread._stop()
- _limbo.clear()
- _active.clear()
- _active.update(new_active)
- assert len(_active) == 1
|