Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
threading.Thread Class Reference
Inheritance diagram for threading.Thread:
Inheritance graph
[legend]
Collaboration diagram for threading.Thread:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def start
def run
def join
def name
def name
def ident
def is_alive
def daemon
def daemon
def isDaemon
def setDaemon
def getName
def setName

Public Attributes

 daemon
 name
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 isAlive = is_alive

Private Member Functions

def _reset_internal_locks
def _set_daemon
def _bootstrap
def _set_ident
def _bootstrap_inner
def _stop
def _delete

Private Attributes

 _target
 _name
 _args
 _kwargs
 _daemonic
 _ident
 _started
 _stopped
 _block
 _initialized
 _stderr

Static Private Attributes

 __initialized = False
 __exc_info = _sys.exc_info

Detailed Description

Definition at line 615 of file threading.py.


Constructor & Destructor Documentation

def threading.Thread.__init__ (   self,
  group = None,
  target = None,
  name = None,
  args = (),
  kwargs = None,
  verbose = None 
)

Definition at line 628 of file threading.py.

00628 
00629                  args=(), kwargs=None, verbose=None):
00630         assert group is None, "group argument must be None for now"
00631         _Verbose.__init__(self, verbose)
00632         if kwargs is None:
00633             kwargs = {}
00634         self._target = target
00635         self._name = str(name or _newname())
00636         self._args = args
00637         self._kwargs = kwargs
00638         self._daemonic = self._set_daemon()
00639         self._ident = None
00640         self._started = Event()
00641         self._stopped = False
00642         self._block = Condition(Lock())
00643         self._initialized = True
00644         # sys.stderr is not stored in the class like
00645         # sys.exc_info since it can be changed between instances
00646         self._stderr = _sys.stderr
00647         _dangling.add(self)

Here is the caller graph for this function:


Member Function Documentation

def threading.Thread.__repr__ (   self)

Definition at line 659 of file threading.py.

00659 
00660     def __repr__(self):
00661         assert self._initialized, "Thread.__init__() was not called"
00662         status = "initial"
00663         if self._started.is_set():
00664             status = "started"
00665         if self._stopped:
00666             status = "stopped"
00667         if self._daemonic:
00668             status += " daemon"
00669         if self._ident is not None:
00670             status += " %s" % self._ident
00671         return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)

def threading.Thread._bootstrap (   self) [private]

Definition at line 699 of file threading.py.

00699 
00700     def _bootstrap(self):
00701         # Wrapper around the real bootstrap code that ignores
00702         # exceptions during interpreter cleanup.  Those typically
00703         # happen when a daemon thread wakes up at an unfortunate
00704         # moment, finds the world around it destroyed, and raises some
00705         # random exception *** while trying to report the exception in
00706         # _bootstrap_inner() below ***.  Those random exceptions
00707         # don't help anybody, and they confuse users, so we suppress
00708         # them.  We suppress them only when it appears that the world
00709         # indeed has already been destroyed, so that exceptions in
00710         # _bootstrap_inner() during normal business hours are properly
00711         # reported.  Also, we only suppress them for daemonic threads;
00712         # if a non-daemonic encounters this, something else is wrong.
00713         try:
00714             self._bootstrap_inner()
00715         except:
00716             if self._daemonic and _sys is None:
00717                 return
00718             raise

Here is the call graph for this function:

Here is the caller graph for this function:

def threading.Thread._bootstrap_inner (   self) [private]

Definition at line 722 of file threading.py.

00722 
00723     def _bootstrap_inner(self):
00724         try:
00725             self._set_ident()
00726             self._started.set()
00727             with _active_limbo_lock:
00728                 _active[self._ident] = self
00729                 del _limbo[self]
00730             if __debug__:
00731                 self._note("%s._bootstrap(): thread started", self)
00732 
00733             if _trace_hook:
00734                 self._note("%s._bootstrap(): registering trace hook", self)
00735                 _sys.settrace(_trace_hook)
00736             if _profile_hook:
00737                 self._note("%s._bootstrap(): registering profile hook", self)
00738                 _sys.setprofile(_profile_hook)
00739 
00740             try:
00741                 self.run()
00742             except SystemExit:
00743                 if __debug__:
00744                     self._note("%s._bootstrap(): raised SystemExit", self)
00745             except:
00746                 if __debug__:
00747                     self._note("%s._bootstrap(): unhandled exception", self)
00748                 # If sys.stderr is no more (most likely from interpreter
00749                 # shutdown) use self._stderr.  Otherwise still use sys (as in
00750                 # _sys) in case sys.stderr was redefined since the creation of
00751                 # self.
00752                 if _sys:
00753                     _sys.stderr.write("Exception in thread %s:\n%s\n" %
00754                                       (self.name, _format_exc()))
00755                 else:
00756                     # Do the best job possible w/o a huge amt. of code to
00757                     # approximate a traceback (code ideas from
00758                     # Lib/traceback.py)
00759                     exc_type, exc_value, exc_tb = self._exc_info()
00760                     try:
00761                         print((
00762                             "Exception in thread " + self.name +
00763                             " (most likely raised during interpreter shutdown):"), file=self._stderr)
00764                         print((
00765                             "Traceback (most recent call last):"), file=self._stderr)
00766                         while exc_tb:
00767                             print((
00768                                 '  File "%s", line %s, in %s' %
00769                                 (exc_tb.tb_frame.f_code.co_filename,
00770                                     exc_tb.tb_lineno,
00771                                     exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
00772                             exc_tb = exc_tb.tb_next
00773                         print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
00774                     # Make sure that exc_tb gets deleted since it is a memory
00775                     # hog; deleting everything else is just for thoroughness
00776                     finally:
00777                         del exc_type, exc_value, exc_tb
00778             else:
00779                 if __debug__:
00780                     self._note("%s._bootstrap(): normal return", self)
00781             finally:
00782                 # Prevent a race in
00783                 # test_threading.test_no_refcycle_through_target when
00784                 # the exception keeps the target alive past when we
00785                 # assert that it's dead.
00786                 #XXX self.__exc_clear()
00787                 pass
00788         finally:
00789             with _active_limbo_lock:
00790                 self._stop()
00791                 try:
00792                     # We don't call self._delete() because it also
00793                     # grabs _active_limbo_lock.
00794                     del _active[_get_ident()]
00795                 except:
00796                     pass

Here is the caller graph for this function:

def threading.Thread._delete (   self) [private]

Definition at line 803 of file threading.py.

00803 
00804     def _delete(self):
00805         "Remove current thread from the dict of currently running threads."
00806 
00807         # Notes about running with _dummy_thread:
00808         #
00809         # Must take care to not raise an exception if _dummy_thread is being
00810         # used (and thus this module is being used as an instance of
00811         # dummy_threading).  _dummy_thread.get_ident() always returns -1 since
00812         # there is only one thread if _dummy_thread is being used.  Thus
00813         # len(_active) is always <= 1 here, and any Thread instance created
00814         # overwrites the (if any) thread currently registered in _active.
00815         #
00816         # An instance of _MainThread is always created by 'threading'.  This
00817         # gets overwritten the instant an instance of Thread is created; both
00818         # threads return -1 from _dummy_thread.get_ident() and thus have the
00819         # same key in the dict.  So when the _MainThread instance created by
00820         # 'threading' tries to clean itself up when atexit calls this method
00821         # it gets a KeyError if another Thread instance was created.
00822         #
00823         # This all means that KeyError from trying to delete something from
00824         # _active if dummy_threading is being used is a red herring.  But
00825         # since it isn't if dummy_threading is *not* being used then don't
00826         # hide the exception.
00827 
00828         try:
00829             with _active_limbo_lock:
00830                 del _active[_get_ident()]
00831                 # There must not be any python code between the previous line
00832                 # and after the lock is released.  Otherwise a tracing function
00833                 # could try to acquire the lock again in the same thread, (in
00834                 # current_thread()), and would block.
00835         except KeyError:
00836             if 'dummy_threading' not in _sys.modules:
00837                 raise

Here is the caller graph for this function:

def threading.Thread._reset_internal_locks (   self) [private]

Definition at line 648 of file threading.py.

00648 
00649     def _reset_internal_locks(self):
00650         # private!  Called by _after_fork() to reset our internal locks as
00651         # they may be in an invalid state leading to a deadlock or crash.
00652         if hasattr(self, '_block'):  # DummyThread deletes _block
00653             self._block.__init__()
00654         self._started._reset_internal_locks()

def threading.Thread._set_daemon (   self) [private]

Reimplemented in threading._DummyThread, and threading._MainThread.

Definition at line 655 of file threading.py.

00655 
00656     def _set_daemon(self):
00657         # Overridden in _MainThread and _DummyThread
00658         return current_thread().daemon

Here is the call graph for this function:

def threading.Thread._set_ident (   self) [private]

Definition at line 719 of file threading.py.

00719 
00720     def _set_ident(self):
00721         self._ident = _get_ident()

Here is the caller graph for this function:

def threading.Thread._stop (   self) [private]

Definition at line 797 of file threading.py.

00797 
00798     def _stop(self):
00799         self._block.acquire()
00800         self._stopped = True
00801         self._block.notify_all()
00802         self._block.release()

Here is the caller graph for this function:

def threading.Thread.daemon (   self)

Definition at line 894 of file threading.py.

00894 
00895     def daemon(self):
00896         assert self._initialized, "Thread.__init__() not called"
00897         return self._daemonic

def threading.Thread.daemon (   self,
  daemonic 
)

Definition at line 899 of file threading.py.

00899 
00900     def daemon(self, daemonic):
00901         if not self._initialized:
00902             raise RuntimeError("Thread.__init__() not called")
00903         if self._started.is_set():
00904             raise RuntimeError("cannot set daemon status of active thread");
00905         self._daemonic = daemonic

def threading.Thread.getName (   self)

Definition at line 912 of file threading.py.

00912 
00913     def getName(self):
00914         return self.name

def threading.Thread.ident (   self)

Definition at line 883 of file threading.py.

00883 
00884     def ident(self):
00885         assert self._initialized, "Thread.__init__() not called"
00886         return self._ident

def threading.Thread.is_alive (   self)

Definition at line 887 of file threading.py.

00887 
00888     def is_alive(self):
00889         assert self._initialized, "Thread.__init__() not called"
00890         return self._started.is_set() and not self._stopped

Here is the caller graph for this function:

def threading.Thread.isDaemon (   self)

Definition at line 906 of file threading.py.

00906 
00907     def isDaemon(self):
00908         return self.daemon

def threading.Thread.join (   self,
  timeout = None 
)

Reimplemented in threading._DummyThread.

Definition at line 838 of file threading.py.

00838 
00839     def join(self, timeout=None):
00840         if not self._initialized:
00841             raise RuntimeError("Thread.__init__() not called")
00842         if not self._started.is_set():
00843             raise RuntimeError("cannot join thread before it is started")
00844         if self is current_thread():
00845             raise RuntimeError("cannot join current thread")
00846 
00847         if __debug__:
00848             if not self._stopped:
00849                 self._note("%s.join(): waiting until thread stops", self)
00850 
00851         self._block.acquire()
00852         try:
00853             if timeout is None:
00854                 while not self._stopped:
00855                     self._block.wait()
00856                 if __debug__:
00857                     self._note("%s.join(): thread stopped", self)
00858             else:
00859                 deadline = _time() + timeout
00860                 while not self._stopped:
00861                     delay = deadline - _time()
00862                     if delay <= 0:
00863                         if __debug__:
00864                             self._note("%s.join(): timed out", self)
00865                         break
00866                     self._block.wait(delay)
00867                 else:
00868                     if __debug__:
00869                         self._note("%s.join(): thread stopped", self)
00870         finally:
00871             self._block.release()

Here is the call graph for this function:

Here is the caller graph for this function:

def threading.Thread.name (   self)

Definition at line 873 of file threading.py.

00873 
00874     def name(self):
00875         assert self._initialized, "Thread.__init__() not called"
00876         return self._name

Here is the caller graph for this function:

def threading.Thread.name (   self,
  name 
)

Definition at line 878 of file threading.py.

00878 
00879     def name(self, name):
00880         assert self._initialized, "Thread.__init__() not called"
00881         self._name = str(name)

Here is the caller graph for this function:

def threading.Thread.run (   self)

Reimplemented in test.test_ssl.AsyncoreEchoServer, threading._Timer, test.test_ssl.ThreadedEchoServer, test.test_ssl.ThreadedEchoServer.ConnectionHandler, test.test_ftplib.DummyFTPServer, test.ssl_servers.HTTPSServerThread, test.test_poplib.DummyPOP3Server, test.test_urllib2_localnet.LoopbackHttpServerThread, test.test_httpservers.TestServerThread, test.test_threading.TestThread, test.test_asynchat.echo_server, test.test_threadedtempfile.TempFileGreedy, test.threaded_import_hangers.Worker, and test.test_queue._TriggerThread.

Definition at line 690 of file threading.py.

00690 
00691     def run(self):
00692         try:
00693             if self._target:
00694                 self._target(*self._args, **self._kwargs)
00695         finally:
00696             # Avoid a refcycle if the thread is running a function with
00697             # an argument that has a member that points to the thread.
00698             del self._target, self._args, self._kwargs

Here is the caller graph for this function:

def threading.Thread.setDaemon (   self,
  daemonic 
)

Definition at line 909 of file threading.py.

00909 
00910     def setDaemon(self, daemonic):
00911         self.daemon = daemonic

def threading.Thread.setName (   self,
  name 
)

Definition at line 915 of file threading.py.

00915 
00916     def setName(self, name):
00917         self.name = name
00918 
00919 # The timer class was contributed by Itamar Shtull-Trauring

def threading.Thread.start (   self)

Reimplemented in test.test_ftplib.DummyFTPServer, test.test_poplib.DummyPOP3Server, and multiprocessing.dummy.DummyProcess.

Definition at line 672 of file threading.py.

00672 
00673     def start(self):
00674         if not self._initialized:
00675             raise RuntimeError("thread.__init__() not called")
00676 
00677         if self._started.is_set():
00678             raise RuntimeError("threads can only be started once")
00679         if __debug__:
00680             self._note("%s.start(): starting thread", self)
00681         with _active_limbo_lock:
00682             _limbo[self] = self
00683         try:
00684             _start_new_thread(self._bootstrap, ())
00685         except Exception:
00686             with _active_limbo_lock:
00687                 del _limbo[self]
00688             raise
00689         self._started.wait()

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

threading.Thread.__exc_info = _sys.exc_info [static, private]

Definition at line 622 of file threading.py.

threading.Thread.__initialized = False [static, private]

Definition at line 617 of file threading.py.

Definition at line 635 of file threading.py.

Definition at line 641 of file threading.py.

Definition at line 637 of file threading.py.

Definition at line 638 of file threading.py.

Definition at line 642 of file threading.py.

Definition at line 636 of file threading.py.

Definition at line 634 of file threading.py.

Definition at line 639 of file threading.py.

Definition at line 645 of file threading.py.

Definition at line 640 of file threading.py.

Definition at line 633 of file threading.py.

Definition at line 891 of file threading.py.

Definition at line 916 of file threading.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


The documentation for this class was generated from the following file: