Lines Matching defs:Thread
0 """Thread module emulating a subset of Java's threading model."""
6 import thread
31 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
34 _start_new_thread = thread.start_new_thread
35 _allocate_lock = thread.allocate_lock
36 _get_ident = thread.get_ident
37 ThreadError = thread.error
38 del thread
72 name = "<OS thread %d>" % ident
92 The func will be passed to sys.setprofile() for each thread, before its
102 The func will be passed to sys.settrace() for each thread, before its run()
116 A reentrant lock must be released by the thread that acquired it. Once a
117 thread has acquired a reentrant lock, the same thread may acquire it again
118 without blocking; the thread must release it once for each time it has
125 """A reentrant lock must be released by the thread that acquired it. Once a
126 thread has acquired a reentrant lock, the same thread may acquire it
127 again without blocking; the thread must release it once for each time it
149 When invoked without arguments: if this thread already owns the lock,
151 if another thread owns the lock, block until the lock is unlocked. Once
152 the lock is unlocked (not owned by any thread), then grab ownership, set
153 the recursion level to one, and return. If more than one thread is
190 by any thread), and if any other threads are blocked waiting for the
193 locked and owned by the calling thread.
195 Only call this method when the calling thread owns the lock. A
245 notified by another thread.
256 notified by another thread.
311 If the calling thread has not acquired the lock when this method is
316 variable in another thread, or until the optional timeout occurs. Once
375 If the calling thread has not acquired the lock when this method is
402 If the calling thread has not acquired the lock when this method
444 on entry, block, waiting until some other thread has called release() to
481 When the counter is zero on entry and another thread is waiting for it
482 to become larger than zero again, wake up that thread.
527 When the counter is zero on entry and another thread is waiting for it
528 to become larger than zero again, wake up that thread.
564 # private! called by Thread._reset_internal_locks by _after_fork()
604 block until another thread calls set() to set the flag to true, or until
623 # Helper to generate new thread names
625 def _newname(template="Thread-%d"):
630 # Active thread administration
632 _active = {} # maps thread id to Thread object
638 class Thread(_Verbose):
639 """A class that represents a thread of control.
646 # out exceptions when a thread tries to use a global var. during interp.
664 *name* is the thread name. By default, a unique name is constructed of
665 the form "Thread-N" where N is a small decimal number.
673 the base class constructor (Thread.__init__()) before doing anything
674 else to the thread.
712 assert self.__initialized, "Thread.__init__() was not called"
725 """Start the thread's activity.
727 It must be called at most once per thread object. It arranges for the
728 object's run() method to be invoked in a separate thread of control.
731 same thread object.
735 raise RuntimeError("thread.__init__() not called")
739 self._note("%s.start(): starting thread", self)
751 """Method representing the thread's activity.
763 # Avoid a refcycle if the thread is running a function with
764 # an argument that has a member that points to the thread.
770 # happen when a daemon thread wakes up at an unfortunate
798 self._note("%s.__bootstrap(): thread started", self)
820 _sys.stderr.write("Exception in thread %s:\n%s\n" %
829 "Exception in thread " + self.name +
875 "Remove current thread from the dict of currently running threads."
882 # there is only one thread if dummy_thread is being used. Thus
883 # len(_active) is always <= 1 here, and any Thread instance created
884 # overwrites the (if any) thread currently registered in _active.
887 # gets overwritten the instant an instance of Thread
891 # it gets a KeyError if another Thread instance was created.
903 # could try to acquire the lock again in the same thread, (in
910 """Wait until the thread terminates.
912 This blocks the calling thread until the thread whose join() method is
920 thread is still alive, the join() call timed out.
923 block until the thread terminates.
925 A thread can be join()ed many times.
928 thread as that would cause a deadlock. It is also an error to join() a
929 thread before it has been started and attempts to do so raises the same
934 raise RuntimeError("Thread.__init__() not called")
936 raise RuntimeError("cannot join thread before it is started")
938 raise RuntimeError("cannot join current thread")
942 self._note("%s.join(): waiting until thread stops", self)
949 self._note("%s.join(): thread stopped", self)
961 self._note("%s.join(): thread stopped", self)
973 assert self.__initialized, "Thread.__init__() not called"
978 assert self.__initialized, "Thread.__init__() not called"
983 """Thread identifier of this thread or None if it has not been started.
985 This is a nonzero integer. See the thread.get_ident() function. Thread
986 identifiers may be recycled when a thread exits and another thread is
987 created. The identifier is available even after the thread has exited.
990 assert self.__initialized, "Thread.__init__() not called"
994 """Return whether the thread is alive.
1001 assert self.__initialized, "Thread.__init__() not called"
1008 """A boolean value indicating whether this thread is a daemon thread (True) or not (False).
1011 raised. Its initial value is inherited from the creating thread; the
1012 main thread is not a daemon thread and therefore all threads created in
1013 the main thread default to daemon = False.
1019 assert self.__initialized, "Thread.__init__() not called"
1025 raise RuntimeError("Thread.__init__() not called")
1027 raise RuntimeError("cannot set daemon status of active thread");
1056 class _Timer(Thread):
1066 Thread.__init__(self)
1083 # Special thread class to represent the main thread
1086 class _MainThread(Thread):
1089 Thread.__init__(self, name="MainThread")
1118 # Dummy thread class to represent threads not started here.
1126 class _DummyThread(Thread):
1129 Thread.__init__(self, name=_newname("Dummy-%d"))
1131 # Thread.__block consumes an OS-level locking primitive, which
1145 assert False, "cannot join a dummy thread"
1151 """Return the current Thread object, corresponding to the caller's thread of control.
1153 If the caller's thread of control was not created through the threading
1154 module, a dummy thread object with limited functionality is returned.
1160 ##print "current_thread(): no current thread for", _get_ident()
1166 """Return the number of Thread objects currently alive.
1182 """Return a list of all Thread objects currently alive.
1184 The list includes daemonic threads, dummy thread objects created by
1185 current_thread(), and the main thread. It excludes terminated threads and
1192 from thread import stack_size
1194 # Create the main thread object,
1200 # get thread-local implementation, either from the thread
1204 from thread import _local as local
1215 # by another (non-forked) thread. http://bugs.python.org/issue874900
1219 # fork() only copied the current thread; clear references to others.
1223 for thread in _active.itervalues():
1226 if hasattr(thread, '_reset_internal_locks'):
1227 thread._reset_internal_locks()
1228 if thread is current:
1229 # There is only one active thread. We reset the ident to
1232 thread._Thread__ident = ident
1233 new_active[ident] = thread
1236 thread._Thread__stop()
1280 class ProducerThread(Thread):
1283 Thread.__init__(self, name="Producer")
1296 class ConsumerThread(Thread):
1299 Thread.__init__(self, name="Consumer")