Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.129.42.198
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : threading.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>16.2. threading — Higher-level threading interface — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="16. Optional Operating System Services" href="someos.html" /> <link rel="next" title="16.3. thread — Multiple threads of control" href="thread.html" /> <link rel="prev" title="16.1. select — Waiting for I/O completion" href="select.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="thread.html" title="16.3. thread — Multiple threads of control" accesskey="N">next</a> |</li> <li class="right" > <a href="select.html" title="16.1. select — Waiting for I/O completion" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="someos.html" accesskey="U">16. Optional Operating System Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-threading"> <span id="threading-higher-level-threading-interface"></span><h1>16.2. <a class="reference internal" href="#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> — Higher-level threading interface<a class="headerlink" href="#module-threading" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/threading.py">Lib/threading.py</a></p> <hr class="docutils" /> <p>This module constructs higher-level threading interfaces on top of the lower level <a class="reference internal" href="thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><tt class="xref py py-mod docutils literal"><span class="pre">thread</span></tt></a> module. See also the <a class="reference internal" href="mutex.html#module-mutex" title="mutex: Lock and queue for mutual exclusion. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">mutex</span></tt></a> and <a class="reference internal" href="queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-mod docutils literal"><span class="pre">Queue</span></tt></a> modules.</p> <p>The <a class="reference internal" href="dummy_threading.html#module-dummy_threading" title="dummy_threading: Drop-in replacement for the threading module."><tt class="xref py py-mod docutils literal"><span class="pre">dummy_threading</span></tt></a> module is provided for situations where <a class="reference internal" href="#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> cannot be used because <a class="reference internal" href="thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><tt class="xref py py-mod docutils literal"><span class="pre">thread</span></tt></a> is missing.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Starting with Python 2.6, this module provides <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> compliant aliases and properties to replace the <tt class="docutils literal"><span class="pre">camelCase</span></tt> names that were inspired by Java’s threading API. This updated API is compatible with that of the <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based "threading" interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> module. However, no schedule has been set for the deprecation of the <tt class="docutils literal"><span class="pre">camelCase</span></tt> names and they remain fully supported in both Python 2.x and 3.x.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Starting with Python 2.5, several Thread methods raise <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> instead of <a class="reference internal" href="exceptions.html#exceptions.AssertionError" title="exceptions.AssertionError"><tt class="xref py py-exc docutils literal"><span class="pre">AssertionError</span></tt></a> if called erroneously.</p> </div> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> In CPython, due to the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><em class="xref std std-term">Global Interpreter Lock</em></a>, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based "threading" interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a>. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.</p> </div> <p>This module defines the following functions and objects:</p> <dl class="function"> <dt id="threading.active_count"> <tt class="descclassname">threading.</tt><tt class="descname">active_count</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.active_count" title="Permalink to this definition">¶</a></dt> <dt id="threading.activeCount"> <tt class="descclassname">threading.</tt><tt class="descname">activeCount</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.activeCount" title="Permalink to this definition">¶</a></dt> <dd><p>Return the number of <a class="reference internal" href="#threading.Thread" title="threading.Thread"><tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt></a> objects currently alive. The returned count is equal to the length of the list returned by <a class="reference internal" href="#threading.enumerate" title="threading.enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a>.</p> </dd></dl> <dl class="function"> <dt> <tt class="descclassname">threading.</tt><tt class="descname">Condition</tt><big>(</big><big>)</big></dt> <dd><p>A factory function that returns a new condition variable object. A condition variable allows one or more threads to wait until they are notified by another thread.</p> <p>See <a class="reference internal" href="#condition-objects"><em>Condition Objects</em></a>.</p> </dd></dl> <dl class="function"> <dt id="threading.current_thread"> <tt class="descclassname">threading.</tt><tt class="descname">current_thread</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.current_thread" title="Permalink to this definition">¶</a></dt> <dt id="threading.currentThread"> <tt class="descclassname">threading.</tt><tt class="descname">currentThread</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.currentThread" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current <a class="reference internal" href="#threading.Thread" title="threading.Thread"><tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt></a> object, corresponding to the caller’s thread of control. If the caller’s thread of control was not created through the <a class="reference internal" href="#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module, a dummy thread object with limited functionality is returned.</p> </dd></dl> <dl class="function"> <dt id="threading.enumerate"> <tt class="descclassname">threading.</tt><tt class="descname">enumerate</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.enumerate" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of all <a class="reference internal" href="#threading.Thread" title="threading.Thread"><tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt></a> objects currently alive. The list includes daemonic threads, dummy thread objects created by <a class="reference internal" href="#threading.current_thread" title="threading.current_thread"><tt class="xref py py-func docutils literal"><span class="pre">current_thread()</span></tt></a>, and the main thread. It excludes terminated threads and threads that have not yet been started.</p> </dd></dl> <dl class="function"> <dt> <tt class="descclassname">threading.</tt><tt class="descname">Event</tt><big>(</big><big>)</big></dt> <dd><p>A factory function that returns a new event object. An event manages a flag that can be set to true with the <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> method and reset to false with the <tt class="xref py py-meth docutils literal"><span class="pre">clear()</span></tt> method. The <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> method blocks until the flag is true.</p> <p>See <a class="reference internal" href="#event-objects"><em>Event Objects</em></a>.</p> </dd></dl> <dl class="class"> <dt id="threading.local"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">local</tt><a class="headerlink" href="#threading.local" title="Permalink to this definition">¶</a></dt> <dd><p>A class that represents thread-local data. Thread-local data are data whose values are thread specific. To manage thread-local data, just create an instance of <a class="reference internal" href="#threading.local" title="threading.local"><tt class="xref py py-class docutils literal"><span class="pre">local</span></tt></a> (or a subclass) and store attributes on it:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">mydata</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span> <span class="n">mydata</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span> </pre></div> </div> <p>The instance’s values will be different for separate threads.</p> <p>For more details and extensive examples, see the documentation string of the <tt class="xref py py-mod docutils literal"><span class="pre">_threading_local</span></tt> module.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> </dd></dl> <dl class="function"> <dt id="threading.Lock"> <tt class="descclassname">threading.</tt><tt class="descname">Lock</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Lock" title="Permalink to this definition">¶</a></dt> <dd><p>A factory function that returns a new primitive lock object. Once a thread has acquired it, subsequent attempts to acquire it block, until it is released; any thread may release it.</p> <p>See <a class="reference internal" href="#lock-objects"><em>Lock Objects</em></a>.</p> </dd></dl> <dl class="function"> <dt id="threading.RLock"> <tt class="descclassname">threading.</tt><tt class="descname">RLock</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.RLock" title="Permalink to this definition">¶</a></dt> <dd><p>A factory function that returns a new reentrant lock object. 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.</p> <p>See <a class="reference internal" href="#rlock-objects"><em>RLock Objects</em></a>.</p> </dd></dl> <dl class="function"> <dt> <tt class="descclassname">threading.</tt><tt class="descname">Semaphore</tt><big>(</big><span class="optional">[</span><em>value</em><span class="optional">]</span><big>)</big></dt> <dd><p>A factory function that returns a new semaphore object. A semaphore manages a counter representing the number of <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> calls minus the number of <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> calls, plus an initial value. The <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> method blocks if necessary until it can return without making the counter negative. If not given, <em>value</em> defaults to 1.</p> <p>See <a class="reference internal" href="#semaphore-objects"><em>Semaphore Objects</em></a>.</p> </dd></dl> <dl class="function"> <dt id="threading.BoundedSemaphore"> <tt class="descclassname">threading.</tt><tt class="descname">BoundedSemaphore</tt><big>(</big><span class="optional">[</span><em>value</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.BoundedSemaphore" title="Permalink to this definition">¶</a></dt> <dd><p>A factory function that returns a new bounded semaphore object. A bounded semaphore checks to make sure its current value doesn’t exceed its initial value. If it does, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> 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, <em>value</em> defaults to 1.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Thread</tt></dt> <dd><p>A class that represents a thread of control. This class can be safely subclassed in a limited fashion.</p> <p>See <a class="reference internal" href="#thread-objects"><em>Thread Objects</em></a>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Timer</tt></dt> <dd><p>A thread that executes a function after a specified interval has passed.</p> <p>See <a class="reference internal" href="#timer-objects"><em>Timer Objects</em></a>.</p> </dd></dl> <dl class="function"> <dt id="threading.settrace"> <tt class="descclassname">threading.</tt><tt class="descname">settrace</tt><big>(</big><em>func</em><big>)</big><a class="headerlink" href="#threading.settrace" title="Permalink to this definition">¶</a></dt> <dd><p id="index-1">Set a trace function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module. The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.settrace" title="sys.settrace"><tt class="xref py py-func docutils literal"><span class="pre">sys.settrace()</span></tt></a> for each thread, before its <tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt> method is called.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="function"> <dt id="threading.setprofile"> <tt class="descclassname">threading.</tt><tt class="descname">setprofile</tt><big>(</big><em>func</em><big>)</big><a class="headerlink" href="#threading.setprofile" title="Permalink to this definition">¶</a></dt> <dd><p id="index-2">Set a profile function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module. The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.setprofile" title="sys.setprofile"><tt class="xref py py-func docutils literal"><span class="pre">sys.setprofile()</span></tt></a> for each thread, before its <tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt> method is called.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="function"> <dt id="threading.stack_size"> <tt class="descclassname">threading.</tt><tt class="descname">stack_size</tt><big>(</big><span class="optional">[</span><em>size</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.stack_size" title="Permalink to this definition">¶</a></dt> <dd><p>Return the thread stack size used when creating new threads. The optional <em>size</em> argument specifies the stack size to be used for subsequently created threads, and must be 0 (use platform or configured default) or a positive integer value of at least 32,768 (32kB). If changing the thread stack size is unsupported, a <tt class="xref py py-exc docutils literal"><span class="pre">ThreadError</span></tt> is raised. If the specified stack size is invalid, a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised and the stack size is unmodified. 32kB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself. Note that some platforms may have particular restrictions on values for the stack size, such as requiring a minimum stack size > 32kB or requiring allocation in multiples of the system memory page size - platform documentation should be referred to for more information (4kB pages are common; using multiples of 4096 for the stack size is the suggested approach in the absence of more specific information). Availability: Windows, systems with POSIX threads.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <p>Detailed interfaces for the objects are documented below.</p> <p>The design of this module is loosely based on Java’s threading model. However, where Java makes locks and condition variables basic behavior of every object, they are separate objects in Python. Python’s <a class="reference internal" href="#threading.Thread" title="threading.Thread"><tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt></a> class supports a subset of the behavior of Java’s Thread class; currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted. The static methods of Java’s Thread class, when implemented, are mapped to module-level functions.</p> <p>All of the methods described below are executed atomically.</p> <div class="section" id="thread-objects"> <span id="id1"></span><h2>16.2.1. Thread Objects<a class="headerlink" href="#thread-objects" title="Permalink to this headline">¶</a></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="xref py py-meth docutils literal"><span class="pre">run()</span></tt> method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, <em>only</em> override the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> and <tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt> methods of this class.</p> <p>Once a thread object is created, its activity must be started by calling the thread’s <tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt> method. This invokes the <tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt> method in a separate thread of control.</p> <p>Once the thread’s activity is started, the thread is considered ‘alive’. It stops being alive when its <tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt> method terminates – either normally, or by raising an unhandled exception. The <tt class="xref py py-meth docutils literal"><span class="pre">is_alive()</span></tt> method tests whether the thread is alive.</p> <p>Other threads can call a thread’s <tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt> method. This blocks the calling thread until the thread whose <tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt> method is called is terminated.</p> <p>A thread has a name. The name can be passed to the constructor, and read or changed through the <tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt> attribute.</p> <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 through the <tt class="xref py py-attr docutils literal"><span class="pre">daemon</span></tt> property.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an <a class="reference internal" href="#threading.Event" title="threading.Event"><tt class="xref py py-class docutils literal"><span class="pre">Event</span></tt></a>.</p> </div> <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> <p>There is the possibility that “dummy thread objects” are created. These are thread objects corresponding to “alien threads”, which are threads of control started outside the threading module, such as directly from C code. Dummy thread objects have limited functionality; they are always considered alive and daemonic, and cannot be <tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt>ed. They are never deleted, since it is impossible to detect the termination of alien threads.</p> <dl class="class"> <dt id="threading.Thread"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Thread</tt><big>(</big><em>group=None</em>, <em>target=None</em>, <em>name=None</em>, <em>args=()</em>, <em>kwargs={}</em><big>)</big><a class="headerlink" href="#threading.Thread" title="Permalink to this definition">¶</a></dt> <dd><p>This constructor should always be called with keyword arguments. Arguments are:</p> <p><em>group</em> should be <tt class="docutils literal"><span class="pre">None</span></tt>; reserved for future extension when a <tt class="xref py py-class docutils literal"><span class="pre">ThreadGroup</span></tt> class is implemented.</p> <p><em>target</em> is the callable object to be invoked by the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt></a> method. Defaults to <tt class="docutils literal"><span class="pre">None</span></tt>, meaning nothing is called.</p> <p><em>name</em> is the thread name. By default, a unique name is constructed of the form “Thread-<em>N</em>” where <em>N</em> is a small decimal number.</p> <p><em>args</em> is the argument tuple for the target invocation. Defaults to <tt class="docutils literal"><span class="pre">()</span></tt>.</p> <p><em>kwargs</em> is a dictionary of keyword arguments for the target invocation. Defaults to <tt class="docutils literal"><span class="pre">{}</span></tt>.</p> <p>If the subclass overrides the constructor, it must make sure to invoke the base class constructor (<tt class="docutils literal"><span class="pre">Thread.__init__()</span></tt>) before doing anything else to the thread.</p> <dl class="method"> <dt id="threading.Thread.start"> <tt class="descname">start</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.start" title="Permalink to this definition">¶</a></dt> <dd><p>Start the thread’s activity.</p> <p>It must be called at most once per thread object. It arranges for the object’s <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt></a> method to be invoked in a separate thread of control.</p> <p>This method will raise a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> if called more than once on the same thread object.</p> </dd></dl> <dl class="method"> <dt id="threading.Thread.run"> <tt class="descname">run</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.run" title="Permalink to this definition">¶</a></dt> <dd><p>Method representing the thread’s activity.</p> <p>You may override this method in a subclass. The standard <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt></a> method invokes the callable object passed to the object’s constructor as the <em>target</em> argument, if any, with sequential and keyword arguments taken from the <em>args</em> and <em>kwargs</em> arguments, respectively.</p> </dd></dl> <dl class="method"> <dt id="threading.Thread.join"> <tt class="descname">join</tt><big>(</big><span class="optional">[</span><em>timeout</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Thread.join" title="Permalink to this definition">¶</a></dt> <dd><p>Wait until the thread terminates. This blocks the calling thread until the thread whose <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.</p> <p>When the <em>timeout</em> argument is present and not <tt class="docutils literal"><span class="pre">None</span></tt>, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> always returns <tt class="docutils literal"><span class="pre">None</span></tt>, you must call <a class="reference internal" href="#threading.Thread.isAlive" title="threading.Thread.isAlive"><tt class="xref py py-meth docutils literal"><span class="pre">isAlive()</span></tt></a> after <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> to decide whether a timeout happened – if the thread is still alive, the <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> call timed out.</p> <p>When the <em>timeout</em> argument is not present or <tt class="docutils literal"><span class="pre">None</span></tt>, the operation will block until the thread terminates.</p> <p>A thread can be <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a>ed many times.</p> <p><a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> raises a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> a thread before it has been started and attempts to do so raises the same exception.</p> </dd></dl> <dl class="method"> <dt id="threading.Thread.getName"> <tt class="descname">getName</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.getName" title="Permalink to this definition">¶</a></dt> <dt id="threading.Thread.setName"> <tt class="descname">setName</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.setName" title="Permalink to this definition">¶</a></dt> <dd><p>Old API for <a class="reference internal" href="#threading.Thread.name" title="threading.Thread.name"><tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="threading.Thread.name"> <tt class="descname">name</tt><a class="headerlink" href="#threading.Thread.name" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> </dd></dl> <dl class="attribute"> <dt id="threading.Thread.ident"> <tt class="descname">ident</tt><a class="headerlink" href="#threading.Thread.ident" title="Permalink to this definition">¶</a></dt> <dd><p>The ‘thread identifier’ of this thread or <tt class="docutils literal"><span class="pre">None</span></tt> if the thread has not been started. This is a nonzero integer. See the <a class="reference internal" href="thread.html#thread.get_ident" title="thread.get_ident"><tt class="xref py py-func docutils literal"><span class="pre">thread.get_ident()</span></tt></a> 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.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="threading.Thread.is_alive"> <tt class="descname">is_alive</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.is_alive" title="Permalink to this definition">¶</a></dt> <dt id="threading.Thread.isAlive"> <tt class="descname">isAlive</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.isAlive" title="Permalink to this definition">¶</a></dt> <dd><p>Return whether the thread is alive.</p> <p>This method returns <tt class="docutils literal"><span class="pre">True</span></tt> just before the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt></a> method starts until just after the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><tt class="xref py py-meth docutils literal"><span class="pre">run()</span></tt></a> method terminates. The module function <a class="reference internal" href="#threading.enumerate" title="threading.enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a> returns a list of all alive threads.</p> </dd></dl> <dl class="method"> <dt id="threading.Thread.isDaemon"> <tt class="descname">isDaemon</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.isDaemon" title="Permalink to this definition">¶</a></dt> <dt id="threading.Thread.setDaemon"> <tt class="descname">setDaemon</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Thread.setDaemon" title="Permalink to this definition">¶</a></dt> <dd><p>Old API for <a class="reference internal" href="#threading.Thread.daemon" title="threading.Thread.daemon"><tt class="xref py py-attr docutils literal"><span class="pre">daemon</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="threading.Thread.daemon"> <tt class="descname">daemon</tt><a class="headerlink" href="#threading.Thread.daemon" title="Permalink to this definition">¶</a></dt> <dd><p>A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before <a class="reference internal" href="#threading.Thread.start" title="threading.Thread.start"><tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt></a> is called, otherwise <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> 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 <a class="reference internal" href="#threading.Thread.daemon" title="threading.Thread.daemon"><tt class="xref py py-attr docutils literal"><span class="pre">daemon</span></tt></a> = <tt class="docutils literal"><span class="pre">False</span></tt>.</p> <p>The entire Python program exits when no alive non-daemon threads are left.</p> </dd></dl> </dd></dl> </div> <div class="section" id="lock-objects"> <span id="id2"></span><h2>16.2.2. Lock Objects<a class="headerlink" href="#lock-objects" title="Permalink to this headline">¶</a></h2> <p>A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. In Python, it is currently the lowest level synchronization primitive available, implemented directly by the <a class="reference internal" href="thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><tt class="xref py py-mod docutils literal"><span class="pre">thread</span></tt></a> extension module.</p> <p>A primitive lock is in one of two states, “locked” or “unlocked”. It is created in the unlocked state. It has two basic methods, <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt>. When the state is unlocked, <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> changes the state to locked and returns immediately. When the state is locked, <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> blocks until a call to <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> in another thread changes it to unlocked, then the <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> call resets it to locked and returns. The <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> will be raised.</p> <p>When more than one thread is blocked in <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> waiting for the state to turn to unlocked, only one thread proceeds when a <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> call resets the state to unlocked; which one of the waiting threads proceeds is not defined, and may vary across implementations.</p> <p>All methods are executed atomically.</p> <dl class="method"> <dt id="threading.Lock.acquire"> <tt class="descclassname">Lock.</tt><tt class="descname">acquire</tt><big>(</big><span class="optional">[</span><em>blocking</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Lock.acquire" title="Permalink to this definition">¶</a></dt> <dd><p>Acquire a lock, blocking or non-blocking.</p> <p>When invoked with the <em>blocking</em> argument set to <tt class="docutils literal"><span class="pre">True</span></tt> (the default), block until the lock is unlocked, then set it to locked and return <tt class="docutils literal"><span class="pre">True</span></tt>.</p> <p>When invoked with the <em>blocking</em> argument set to <tt class="docutils literal"><span class="pre">False</span></tt>, do not block. If a call with <em>blocking</em> set to <tt class="docutils literal"><span class="pre">True</span></tt> would block, return <tt class="docutils literal"><span class="pre">False</span></tt> immediately; otherwise, set the lock to locked and return <tt class="docutils literal"><span class="pre">True</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="threading.Lock.release"> <tt class="descclassname">Lock.</tt><tt class="descname">release</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Lock.release" title="Permalink to this definition">¶</a></dt> <dd><p>Release a lock.</p> <p>When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.</p> <p>When invoked on an unlocked lock, a <tt class="xref py py-exc docutils literal"><span class="pre">ThreadError</span></tt> is raised.</p> <p>There is no return value.</p> </dd></dl> </div> <div class="section" id="rlock-objects"> <span id="id3"></span><h2>16.2.3. RLock Objects<a class="headerlink" href="#rlock-objects" title="Permalink to this headline">¶</a></h2> <p>A reentrant lock is a synchronization primitive that may be acquired multiple times by the same thread. Internally, it uses the concepts of “owning thread” and “recursion level” in addition to the locked/unlocked state used by primitive locks. In the locked state, some thread owns the lock; in the unlocked state, no thread owns it.</p> <p>To lock the lock, a thread calls its <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> method; this returns once the thread owns the lock. To unlock the lock, a thread calls its <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> method. <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt>/<tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> call pairs may be nested; only the final <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> (the <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> of the outermost pair) resets the lock to unlocked and allows another thread blocked in <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> to proceed.</p> <dl class="method"> <dt id="threading.RLock.acquire"> <tt class="descclassname">RLock.</tt><tt class="descname">acquire</tt><big>(</big><span class="optional">[</span><em>blocking=1</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.RLock.acquire" title="Permalink to this definition">¶</a></dt> <dd><p>Acquire a lock, blocking or non-blocking.</p> <p>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.</p> <p>When invoked with the <em>blocking</em> argument set to true, do the same thing as when called without arguments, and return true.</p> <p>When invoked with the <em>blocking</em> 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.</p> </dd></dl> <dl class="method"> <dt id="threading.RLock.release"> <tt class="descclassname">RLock.</tt><tt class="descname">release</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.RLock.release" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> <p>Only call this method when the calling thread owns the lock. A <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised if this method is called when the lock is unlocked.</p> <p>There is no return value.</p> </dd></dl> </div> <div class="section" id="condition-objects"> <span id="id4"></span><h2>16.2.4. Condition Objects<a class="headerlink" href="#condition-objects" title="Permalink to this headline">¶</a></h2> <p>A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default. (Passing one in is useful when several condition variables must share the same lock.)</p> <p>A condition variable has <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> methods that call the corresponding methods of the associated lock. It also has a <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> method, and <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> methods. These three must only be called when the calling thread has acquired the lock, otherwise a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised.</p> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> method releases the lock, and then blocks until it is awakened by a <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> call for the same condition variable in another thread. Once awakened, it re-acquires the lock and returns. It is also possible to specify a timeout.</p> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> method wakes up one of the threads waiting for the condition variable, if any are waiting. The <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> method wakes up all threads waiting for the condition variable.</p> <p>Note: the <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> methods don’t release the lock; this means that the thread or threads awakened will not return from their <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> call immediately, but only when the thread that called <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> finally relinquishes ownership of the lock.</p> <p>Tip: the typical programming style using condition variables uses the lock to synchronize access to some shared state; threads that are interested in a particular change of state call <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> repeatedly until they see the desired state, while threads that modify the state call <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt> when they change the state in such a way that it could possibly be a desired state for one of the waiters. For example, the following code is a generic producer-consumer situation with unlimited buffer capacity:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># Consume one item</span> <span class="n">cv</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span> <span class="k">while</span> <span class="ow">not</span> <span class="n">an_item_is_available</span><span class="p">():</span> <span class="n">cv</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="n">get_an_available_item</span><span class="p">()</span> <span class="n">cv</span><span class="o">.</span><span class="n">release</span><span class="p">()</span> <span class="c"># Produce one item</span> <span class="n">cv</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span> <span class="n">make_an_item_available</span><span class="p">()</span> <span class="n">cv</span><span class="o">.</span><span class="n">notify</span><span class="p">()</span> <span class="n">cv</span><span class="o">.</span><span class="n">release</span><span class="p">()</span> </pre></div> </div> <p>To choose between <tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt>, consider whether one state change can be interesting for only one or several waiting threads. E.g. in a typical producer-consumer situation, adding one item to the buffer only needs to wake up one consumer thread.</p> <dl class="class"> <dt id="threading.Condition"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Condition</tt><big>(</big><span class="optional">[</span><em>lock</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Condition" title="Permalink to this definition">¶</a></dt> <dd><p>If the <em>lock</em> argument is given and not <tt class="docutils literal"><span class="pre">None</span></tt>, it must be a <a class="reference internal" href="#threading.Lock" title="threading.Lock"><tt class="xref py py-class docutils literal"><span class="pre">Lock</span></tt></a> or <a class="reference internal" href="#threading.RLock" title="threading.RLock"><tt class="xref py py-class docutils literal"><span class="pre">RLock</span></tt></a> object, and it is used as the underlying lock. Otherwise, a new <a class="reference internal" href="#threading.RLock" title="threading.RLock"><tt class="xref py py-class docutils literal"><span class="pre">RLock</span></tt></a> object is created and used as the underlying lock.</p> <dl class="method"> <dt id="threading.Condition.acquire"> <tt class="descname">acquire</tt><big>(</big><em>*args</em><big>)</big><a class="headerlink" href="#threading.Condition.acquire" title="Permalink to this definition">¶</a></dt> <dd><p>Acquire the underlying lock. This method calls the corresponding method on the underlying lock; the return value is whatever that method returns.</p> </dd></dl> <dl class="method"> <dt id="threading.Condition.release"> <tt class="descname">release</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Condition.release" title="Permalink to this definition">¶</a></dt> <dd><p>Release the underlying lock. This method calls the corresponding method on the underlying lock; there is no return value.</p> </dd></dl> <dl class="method"> <dt id="threading.Condition.wait"> <tt class="descname">wait</tt><big>(</big><span class="optional">[</span><em>timeout</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Condition.wait" title="Permalink to this definition">¶</a></dt> <dd><p>Wait until notified or until a timeout occurs. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised.</p> <p>This method releases the underlying lock, and then blocks until it is awakened by a <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt></a> or <a class="reference internal" href="#threading.Condition.notifyAll" title="threading.Condition.notifyAll"><tt class="xref py py-meth docutils literal"><span class="pre">notifyAll()</span></tt></a> 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.</p> <p>When the <em>timeout</em> argument is present and not <tt class="docutils literal"><span class="pre">None</span></tt>, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).</p> <p>When the underlying lock is an <a class="reference internal" href="#threading.RLock" title="threading.RLock"><tt class="xref py py-class docutils literal"><span class="pre">RLock</span></tt></a>, it is not released using its <a class="reference internal" href="#threading.Condition.release" title="threading.Condition.release"><tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt></a> method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the <a class="reference internal" href="#threading.RLock" title="threading.RLock"><tt class="xref py py-class docutils literal"><span class="pre">RLock</span></tt></a> 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.</p> </dd></dl> <dl class="method"> <dt id="threading.Condition.notify"> <tt class="descname">notify</tt><big>(</big><em>n=1</em><big>)</big><a class="headerlink" href="#threading.Condition.notify" title="Permalink to this definition">¶</a></dt> <dd><p>By default, wake up one thread waiting on this condition, if any. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised.</p> <p>This method wakes up at most <em>n</em> of the threads waiting for the condition variable; it is a no-op if no threads are waiting.</p> <p>The current implementation wakes up exactly <em>n</em> threads, if at least <em>n</em> threads are waiting. However, it’s not safe to rely on this behavior. A future, optimized implementation may occasionally wake up more than <em>n</em> threads.</p> <p>Note: an awakened thread does not actually return from its <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> call until it can reacquire the lock. Since <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt></a> does not release the lock, its caller should.</p> </dd></dl> <dl class="method"> <dt id="threading.Condition.notify_all"> <tt class="descname">notify_all</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Condition.notify_all" title="Permalink to this definition">¶</a></dt> <dt id="threading.Condition.notifyAll"> <tt class="descname">notifyAll</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Condition.notifyAll" title="Permalink to this definition">¶</a></dt> <dd><p>Wake up all threads waiting on this condition. This method acts like <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><tt class="xref py py-meth docutils literal"><span class="pre">notify()</span></tt></a>, but wakes up all waiting threads instead of one. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised.</p> </dd></dl> </dd></dl> </div> <div class="section" id="semaphore-objects"> <span id="id5"></span><h2>16.2.5. Semaphore Objects<a class="headerlink" href="#semaphore-objects" title="Permalink to this headline">¶</a></h2> <p>This is one of the oldest synchronization primitives in the history of computer science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he used <tt class="xref py py-meth docutils literal"><span class="pre">P()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">V()</span></tt> instead of <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt>).</p> <p>A semaphore manages an internal counter which is decremented by each <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> call and incremented by each <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> call. The counter can never go below zero; when <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> finds that it is zero, it blocks, waiting until some other thread calls <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt>.</p> <dl class="class"> <dt id="threading.Semaphore"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Semaphore</tt><big>(</big><span class="optional">[</span><em>value</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Semaphore" title="Permalink to this definition">¶</a></dt> <dd><p>The optional argument gives the initial <em>value</em> for the internal counter; it defaults to <tt class="docutils literal"><span class="pre">1</span></tt>. If the <em>value</em> given is less than 0, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p> <dl class="method"> <dt id="threading.Semaphore.acquire"> <tt class="descname">acquire</tt><big>(</big><span class="optional">[</span><em>blocking</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Semaphore.acquire" title="Permalink to this definition">¶</a></dt> <dd><p>Acquire a semaphore.</p> <p>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 <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt></a> to make it larger than zero. This is done with proper interlocking so that if multiple <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt></a> calls are blocked, <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt></a> 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.</p> <p>When invoked with <em>blocking</em> set to true, do the same thing as when called without arguments, and return true.</p> <p>When invoked with <em>blocking</em> 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.</p> </dd></dl> <dl class="method"> <dt id="threading.Semaphore.release"> <tt class="descname">release</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Semaphore.release" title="Permalink to this definition">¶</a></dt> <dd><p>Release a semaphore, incrementing the internal counter by one. When it was zero on entry and another thread is waiting for it to become larger than zero again, wake up that thread.</p> </dd></dl> </dd></dl> <div class="section" id="semaphore-example"> <span id="semaphore-examples"></span><h3>16.2.5.1. <a class="reference internal" href="#threading.Semaphore" title="threading.Semaphore"><tt class="xref py py-class docutils literal"><span class="pre">Semaphore</span></tt></a> Example<a class="headerlink" href="#semaphore-example" title="Permalink to this headline">¶</a></h3> <p>Semaphores are often used to guard resources with limited capacity, for example, a database server. In any situation where the size of the resource is fixed, you should use a bounded semaphore. Before spawning any worker threads, your main thread would initialize the semaphore:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">maxconnections</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">...</span> <span class="n">pool_sema</span> <span class="o">=</span> <span class="n">BoundedSemaphore</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">maxconnections</span><span class="p">)</span> </pre></div> </div> <p>Once spawned, worker threads call the semaphore’s acquire and release methods when they need to connect to the server:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">pool_sema</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span> <span class="n">conn</span> <span class="o">=</span> <span class="n">connectdb</span><span class="p">()</span> <span class="o">...</span> <span class="n">use</span> <span class="n">connection</span> <span class="o">...</span> <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="n">pool_sema</span><span class="o">.</span><span class="n">release</span><span class="p">()</span> </pre></div> </div> <p>The use of a bounded semaphore reduces the chance that a programming error which causes the semaphore to be released more than it’s acquired will go undetected.</p> </div> </div> <div class="section" id="event-objects"> <span id="id6"></span><h2>16.2.6. Event Objects<a class="headerlink" href="#event-objects" title="Permalink to this headline">¶</a></h2> <p>This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.</p> <p>An event object manages an internal flag that can be set to true with the <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> method and reset to false with the <tt class="xref py py-meth docutils literal"><span class="pre">clear()</span></tt> method. The <tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt> method blocks until the flag is true.</p> <dl class="class"> <dt id="threading.Event"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Event</tt><a class="headerlink" href="#threading.Event" title="Permalink to this definition">¶</a></dt> <dd><p>The internal flag is initially false.</p> <dl class="method"> <dt id="threading.Event.is_set"> <tt class="descname">is_set</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Event.is_set" title="Permalink to this definition">¶</a></dt> <dt id="threading.Event.isSet"> <tt class="descname">isSet</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Event.isSet" title="Permalink to this definition">¶</a></dt> <dd><p>Return true if and only if the internal flag is true.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>The <tt class="docutils literal"><span class="pre">is_set()</span></tt> syntax is new.</p> </dd></dl> <dl class="method"> <dt id="threading.Event.set"> <tt class="descname">set</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Event.set" title="Permalink to this definition">¶</a></dt> <dd><p>Set the internal flag to true. All threads waiting for it to become true are awakened. Threads that call <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> once the flag is true will not block at all.</p> </dd></dl> <dl class="method"> <dt id="threading.Event.clear"> <tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Event.clear" title="Permalink to this definition">¶</a></dt> <dd><p>Reset the internal flag to false. Subsequently, threads calling <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> will block until <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> is called to set the internal flag to true again.</p> </dd></dl> <dl class="method"> <dt id="threading.Event.wait"> <tt class="descname">wait</tt><big>(</big><span class="optional">[</span><em>timeout</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#threading.Event.wait" title="Permalink to this definition">¶</a></dt> <dd><p>Block until the internal flag is true. If the internal flag is true on entry, return immediately. Otherwise, block until another thread calls <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> to set the flag to true, or until the optional timeout occurs.</p> <p>When the timeout argument is present and not <tt class="docutils literal"><span class="pre">None</span></tt>, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).</p> <p>This method returns the internal flag on exit, so it will always return <tt class="docutils literal"><span class="pre">True</span></tt> except if a timeout is given and the operation times out.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.7: </span>Previously, the method always returned <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </dd></dl> </dd></dl> </div> <div class="section" id="timer-objects"> <span id="id7"></span><h2>16.2.7. Timer Objects<a class="headerlink" href="#timer-objects" title="Permalink to this headline">¶</a></h2> <p>This class represents an action that should be run only after a certain amount of time has passed — a timer. <a class="reference internal" href="#threading.Timer" title="threading.Timer"><tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt></a> is a subclass of <a class="reference internal" href="#threading.Thread" title="threading.Thread"><tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt></a> and as such also functions as an example of creating custom threads.</p> <p>Timers are started, as with threads, by calling their <tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt> method. The timer can be stopped (before its action has begun) by calling the <tt class="xref py py-meth docutils literal"><span class="pre">cancel()</span></tt> method. The interval the timer will wait before executing its action may not be exactly the same as the interval specified by the user.</p> <p>For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="k">print</span> <span class="s">"hello, world"</span> <span class="n">t</span> <span class="o">=</span> <span class="n">Timer</span><span class="p">(</span><span class="mf">30.0</span><span class="p">,</span> <span class="n">hello</span><span class="p">)</span> <span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c"># after 30 seconds, "hello, world" will be printed</span> </pre></div> </div> <dl class="class"> <dt id="threading.Timer"> <em class="property">class </em><tt class="descclassname">threading.</tt><tt class="descname">Timer</tt><big>(</big><em>interval</em>, <em>function</em>, <em>args=</em><span class="optional">[</span><span class="optional">]</span>, <em>kwargs={}</em><big>)</big><a class="headerlink" href="#threading.Timer" title="Permalink to this definition">¶</a></dt> <dd><p>Create a timer that will run <em>function</em> with arguments <em>args</em> and keyword arguments <em>kwargs</em>, after <em>interval</em> seconds have passed.</p> <dl class="method"> <dt id="threading.Timer.cancel"> <tt class="descname">cancel</tt><big>(</big><big>)</big><a class="headerlink" href="#threading.Timer.cancel" title="Permalink to this definition">¶</a></dt> <dd><p>Stop the timer, and cancel the execution of the timer’s action. This will only work if the timer is still in its waiting stage.</p> </dd></dl> </dd></dl> </div> <div class="section" id="using-locks-conditions-and-semaphores-in-the-with-statement"> <span id="with-locks"></span><h2>16.2.8. Using locks, conditions, and semaphores in the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement<a class="headerlink" href="#using-locks-conditions-and-semaphores-in-the-with-statement" title="Permalink to this headline">¶</a></h2> <p>All of the objects provided by this module that have <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> methods can be used as context managers for a <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement. The <tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt> method will be called when the block is entered, and <tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt> will be called when the block is exited.</p> <p>Currently, <a class="reference internal" href="#threading.Lock" title="threading.Lock"><tt class="xref py py-class docutils literal"><span class="pre">Lock</span></tt></a>, <a class="reference internal" href="#threading.RLock" title="threading.RLock"><tt class="xref py py-class docutils literal"><span class="pre">RLock</span></tt></a>, <a class="reference internal" href="#threading.Condition" title="threading.Condition"><tt class="xref py py-class docutils literal"><span class="pre">Condition</span></tt></a>, <a class="reference internal" href="#threading.Semaphore" title="threading.Semaphore"><tt class="xref py py-class docutils literal"><span class="pre">Semaphore</span></tt></a>, and <a class="reference internal" href="#threading.BoundedSemaphore" title="threading.BoundedSemaphore"><tt class="xref py py-class docutils literal"><span class="pre">BoundedSemaphore</span></tt></a> objects may be used as <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement context managers. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">threading</span> <span class="n">some_rlock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">RLock</span><span class="p">()</span> <span class="k">with</span> <span class="n">some_rlock</span><span class="p">:</span> <span class="k">print</span> <span class="s">"some_rlock is locked while this executes"</span> </pre></div> </div> </div> <div class="section" id="importing-in-threaded-code"> <span id="threaded-imports"></span><h2>16.2.9. Importing in threaded code<a class="headerlink" href="#importing-in-threaded-code" title="Permalink to this headline">¶</a></h2> <p>While the import machinery is thread-safe, there are two key restrictions on threaded imports due to inherent limitations in the way that thread-safety is provided:</p> <ul class="simple"> <li>Firstly, other than in the main module, an import should not have the side effect of spawning a new thread and then waiting for that thread in any way. Failing to abide by this restriction can lead to a deadlock if the spawned thread directly or indirectly attempts to import a module.</li> <li>Secondly, all import attempts must be completed before the interpreter starts shutting itself down. This can be most easily achieved by only performing imports from non-daemon threads created through the threading module. Daemon threads and threads created directly with the thread module will require some other form of synchronization to ensure they do not attempt imports after system shutdown has commenced. Failure to abide by this restriction will lead to intermittent exceptions and crashes during interpreter shutdown (as the late imports attempt to access machinery which is no longer in a valid state).</li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">16.2. <tt class="docutils literal"><span class="pre">threading</span></tt> — Higher-level threading interface</a><ul> <li><a class="reference internal" href="#thread-objects">16.2.1. Thread Objects</a></li> <li><a class="reference internal" href="#lock-objects">16.2.2. Lock Objects</a></li> <li><a class="reference internal" href="#rlock-objects">16.2.3. RLock Objects</a></li> <li><a class="reference internal" href="#condition-objects">16.2.4. Condition Objects</a></li> <li><a class="reference internal" href="#semaphore-objects">16.2.5. Semaphore Objects</a><ul> <li><a class="reference internal" href="#semaphore-example">16.2.5.1. <tt class="docutils literal"><span class="pre">Semaphore</span></tt> Example</a></li> </ul> </li> <li><a class="reference internal" href="#event-objects">16.2.6. Event Objects</a></li> <li><a class="reference internal" href="#timer-objects">16.2.7. Timer Objects</a></li> <li><a class="reference internal" href="#using-locks-conditions-and-semaphores-in-the-with-statement">16.2.8. Using locks, conditions, and semaphores in the <tt class="docutils literal"><span class="pre">with</span></tt> statement</a></li> <li><a class="reference internal" href="#importing-in-threaded-code">16.2.9. Importing in threaded code</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="select.html" title="previous chapter">16.1. <tt class="docutils literal"><span class="pre">select</span></tt> — Waiting for I/O completion</a></p> <h4>Next topic</h4> <p class="topless"><a href="thread.html" title="next chapter">16.3. <tt class="docutils literal"><span class="pre">thread</span></tt> — Multiple threads of control</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/threading.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="thread.html" title="16.3. thread — Multiple threads of control" >next</a> |</li> <li class="right" > <a href="select.html" title="16.1. select — Waiting for I/O completion" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="someos.html" >16. Optional Operating System Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close