⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread-objects.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7.5.6 Thread Objects </title>
<META NAME="description" CONTENT="7.5.6 Thread Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="event-objects.html" tppabs="http://www.python.org/doc/current/lib/event-objects.html">
<LINK REL="up" href="module-threading.html" tppabs="http://www.python.org/doc/current/lib/module-threading.html">
<LINK REL="next" href="module-mutex.html" tppabs="http://www.python.org/doc/current/lib/module-mutex.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="event-objects.html" tppabs="http://www.python.org/doc/current/lib/event-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-threading.html" tppabs="http://www.python.org/doc/current/lib/module-threading.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-mutex.html" tppabs="http://www.python.org/doc/current/lib/module-mutex.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="event-objects.html" tppabs="http://www.python.org/doc/current/lib/event-objects.html">7.5.5 Event Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-threading.html" tppabs="http://www.python.org/doc/current/lib/module-threading.html">7.5 threading  </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-mutex.html" tppabs="http://www.python.org/doc/current/lib/module-mutex.html">7.6 mutex  </A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2>
<BR>
7.5.6 Thread Objects 
</H2>

<P>
This class represents an activity that is run in a separate thread
of control.  There are two ways to specify the activity: by
passing a callable object to the constructor, or by overriding the
<tt class="method">run()</tt> method in a subclass.  No other methods (except for the
constructor) should be overridden in a subclass.  In other words, 
<i>only</i>  override the <tt class="method">__init__()</tt> and <tt class="method">run()</tt>
methods of this class.

<P>
Once a thread object is created, its activity must be started by
calling the thread's <tt class="method">start()</tt> method.  This invokes the
<tt class="method">run()</tt> method in a separate thread of control.

<P>
Once the thread's activity is started, the thread is considered
'alive' and 'active' (these concepts are almost, but not quite
exactly, the same; their definition is intentionally somewhat
vague).  It stops being alive and active when its <tt class="method">run()</tt>
method terminates - either normally, or by raising an unhandled
exception.  The <tt class="method">isAlive()</tt> method tests whether the thread is
alive.

<P>
Other threads can call a thread's <tt class="method">join()</tt> method.  This blocks
the calling thread until the thread whose <tt class="method">join()</tt> method is
called is terminated.

<P>
A thread has a name.  The name can be passed to the constructor,
set with the <tt class="method">setName()</tt> method, and retrieved with the
<tt class="method">getName()</tt> method.

<P>
A thread can be flagged as a ``daemon thread''.  The significance
of this flag is that the entire Python program exits when only
daemon threads are left.  The initial value is inherited from the
creating thread.  The flag can be set with the <tt class="method">setDaemon()</tt>
method and retrieved with the <tt class="method">getDaemon()</tt> method.

<P>
There is a ``main thread'' object; this corresponds to the
initial thread of control in the Python program.  It is not a
daemon thread.

<P>
There is the possibility that ``dummy thread objects'' are
created.  These are thread objects corresponding to ``alien
threads''.  These are threads of control started outside the
threading module, e.g. directly from C code.  Dummy thread objects
have limited functionality; they are always considered alive,
active, and daemonic, and cannot be <tt class="method">join()</tt>ed.  They are never 
deleted, since it is impossible to detect the termination of alien
threads.

<P>
<dl><dt><b><a name='l2h-1661'><tt class='class'>Thread</tt></a></b> (<var>group=None, target=None, name=None,
                          args=(), kwargs={}</var>)
<dd>
This constructor should always be called with keyword
arguments.  Arguments are:

<P>
<var>group</var>
Should be <code>None</code>; reserved for future extension when a
<tt class="class">ThreadGroup</tt> class is implemented.

<P>
<var>target</var>
Callable object to be invoked by the <tt class="method">run()</tt> method.
Defaults to <code>None</code>, meaning nothing is called.

<P>
<var>name</var>
The thread name.  By default, a unique name is constructed of the form
``Thread-<var>N</var>'' where <var>N</var> is a small decimal number.

<P>
<var>args</var>
Argument tuple for the target invocation.  Defaults to <code>()</code>.

<P>
<var>kwargs</var>
Keyword argument dictionary for the target invocation.
Defaults to <code>{}</code>.

<P>
If the subclass overrides the constructor, it must make sure
to invoke the base class constructor (<code>Thread.__init__()</code>)
before doing anything else to the thread.
</dl>

<P>
<dl><dt><b><a name='l2h-1662'><tt class='method'>start</tt></a></b> ()
<dd>
Start the thread's activity.

<P>
This must be called at most once per thread object.  It
arranges for the object's <tt class="method">run()</tt> method to be invoked in a
separate thread of control.
</dl>

<P>
<dl><dt><b><a name='l2h-1663'><tt class='method'>run</tt></a></b> ()
<dd>
Method representing the thread's activity.

<P>
You may override this method in a subclass.  The standard
<tt class="method">run()</tt> method invokes the callable object passed to the object's constructor as the
<var>target</var> argument, if any, with sequential and keyword
arguments taken from the <var>args</var> and <var>kwargs</var> arguments,
respectively.
</dl>

<P>
<dl><dt><b><a name='l2h-1664'><tt class='method'>join</tt></a></b> (<big>[</big><var>timeout</var><big>]</big>)
<dd>
Wait until the thread terminates.
This blocks the calling thread until the thread whose <tt class="method">join()</tt>
method is called terminates - either normally or through an
unhandled exception - or until the optional timeout occurs.

<P>
When the <var>timeout</var> argument is present and not <code>None</code>, it should
be a floating point number specifying a timeout for the
operation in seconds (or fractions thereof).

<P>
A thread can be <tt class="method">join()</tt>ed many times.

<P>
A thread cannot join itself because this would cause a
deadlock.

<P>
It is an error to attempt to <tt class="method">join()</tt> a thread before it has
been started.
</dl>

<P>
<dl><dt><b><a name='l2h-1665'><tt class='method'>getName</tt></a></b> ()
<dd>
Return the thread's name.
</dl>

<P>
<dl><dt><b><a name='l2h-1666'><tt class='method'>setName</tt></a></b> (<var>name</var>)
<dd>
Set the thread's name.

<P>
The name is 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.
</dl>

<P>
<dl><dt><b><a name='l2h-1667'><tt class='method'>isAlive</tt></a></b> ()
<dd>
Return whether the thread is alive.

<P>
Roughly, a thread is alive from the moment the <tt class="method">start()</tt> method
returns until its <tt class="method">run()</tt> method terminates.
</dl>

<P>
<dl><dt><b><a name='l2h-1668'><tt class='method'>isDaemon</tt></a></b> ()
<dd>
Return the thread's daemon flag.
</dl>

<P>
<dl><dt><b><a name='l2h-1669'><tt class='method'>setDaemon</tt></a></b> (<var>daemonic</var>)
<dd>
Set the thread's daemon flag to the Boolean value <var>daemonic</var>.
This must be called before <tt class="method">start()</tt> is called.

<P>
The initial value is inherited from the creating thread.

<P>
The entire Python program exits when no active non-daemon
threads are left.
</dl>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="event-objects.html" tppabs="http://www.python.org/doc/current/lib/event-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-threading.html" tppabs="http://www.python.org/doc/current/lib/module-threading.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-mutex.html" tppabs="http://www.python.org/doc/current/lib/module-mutex.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="event-objects.html" tppabs="http://www.python.org/doc/current/lib/event-objects.html">7.5.5 Event Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-threading.html" tppabs="http://www.python.org/doc/current/lib/module-threading.html">7.5 threading  </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-mutex.html" tppabs="http://www.python.org/doc/current/lib/module-mutex.html">7.6 mutex  </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -