~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/faq/library.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>Library and Extension FAQ &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 2.7.12 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
 
33
    <link rel="up" title="Python Frequently Asked Questions" href="index.html" />
 
34
    <link rel="next" title="Extending/Embedding FAQ" href="extending.html" />
 
35
    <link rel="prev" title="Design and History FAQ" href="design.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
38
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="extending.html" title="Extending/Embedding FAQ"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="design.html" title="Design and History FAQ"
 
58
             accesskey="P">previous</a> |</li>
 
59
        <li><img src="../_static/py.png" alt=""
 
60
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
61
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Frequently Asked Questions</a> &raquo;</li> 
 
68
      </ul>
 
69
    </div>    
 
70
 
 
71
    <div class="document">
 
72
      <div class="documentwrapper">
 
73
        <div class="bodywrapper">
 
74
          <div class="body" role="main">
 
75
            
 
76
  <div class="section" id="library-and-extension-faq">
 
77
<h1><a class="toc-backref" href="#id1">Library and Extension FAQ</a><a class="headerlink" href="#library-and-extension-faq" title="Permalink to this headline">¶</a></h1>
 
78
<div class="contents topic" id="contents">
 
79
<p class="topic-title first">Contents</p>
 
80
<ul class="simple">
 
81
<li><a class="reference internal" href="#library-and-extension-faq" id="id1">Library and Extension FAQ</a><ul>
 
82
<li><a class="reference internal" href="#general-library-questions" id="id2">General Library Questions</a><ul>
 
83
<li><a class="reference internal" href="#how-do-i-find-a-module-or-application-to-perform-task-x" id="id3">How do I find a module or application to perform task X?</a></li>
 
84
<li><a class="reference internal" href="#where-is-the-math-py-socket-py-regex-py-etc-source-file" id="id4">Where is the math.py (socket.py, regex.py, etc.) source file?</a></li>
 
85
<li><a class="reference internal" href="#how-do-i-make-a-python-script-executable-on-unix" id="id5">How do I make a Python script executable on Unix?</a></li>
 
86
<li><a class="reference internal" href="#is-there-a-curses-termcap-package-for-python" id="id6">Is there a curses/termcap package for Python?</a></li>
 
87
<li><a class="reference internal" href="#is-there-an-equivalent-to-c-s-onexit-in-python" id="id7">Is there an equivalent to C&#8217;s onexit() in Python?</a></li>
 
88
<li><a class="reference internal" href="#why-don-t-my-signal-handlers-work" id="id8">Why don&#8217;t my signal handlers work?</a></li>
 
89
</ul>
 
90
</li>
 
91
<li><a class="reference internal" href="#common-tasks" id="id9">Common tasks</a><ul>
 
92
<li><a class="reference internal" href="#how-do-i-test-a-python-program-or-component" id="id10">How do I test a Python program or component?</a></li>
 
93
<li><a class="reference internal" href="#how-do-i-create-documentation-from-doc-strings" id="id11">How do I create documentation from doc strings?</a></li>
 
94
<li><a class="reference internal" href="#how-do-i-get-a-single-keypress-at-a-time" id="id12">How do I get a single keypress at a time?</a></li>
 
95
</ul>
 
96
</li>
 
97
<li><a class="reference internal" href="#threads" id="id13">Threads</a><ul>
 
98
<li><a class="reference internal" href="#how-do-i-program-using-threads" id="id14">How do I program using threads?</a></li>
 
99
<li><a class="reference internal" href="#none-of-my-threads-seem-to-run-why" id="id15">None of my threads seem to run: why?</a></li>
 
100
<li><a class="reference internal" href="#how-do-i-parcel-out-work-among-a-bunch-of-worker-threads" id="id16">How do I parcel out work among a bunch of worker threads?</a></li>
 
101
<li><a class="reference internal" href="#what-kinds-of-global-value-mutation-are-thread-safe" id="id17">What kinds of global value mutation are thread-safe?</a></li>
 
102
<li><a class="reference internal" href="#can-t-we-get-rid-of-the-global-interpreter-lock" id="id18">Can&#8217;t we get rid of the Global Interpreter Lock?</a></li>
 
103
</ul>
 
104
</li>
 
105
<li><a class="reference internal" href="#input-and-output" id="id19">Input and Output</a><ul>
 
106
<li><a class="reference internal" href="#how-do-i-delete-a-file-and-other-file-questions" id="id20">How do I delete a file? (And other file questions...)</a></li>
 
107
<li><a class="reference internal" href="#how-do-i-copy-a-file" id="id21">How do I copy a file?</a></li>
 
108
<li><a class="reference internal" href="#how-do-i-read-or-write-binary-data" id="id22">How do I read (or write) binary data?</a></li>
 
109
<li><a class="reference internal" href="#i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why" id="id23">I can&#8217;t seem to use os.read() on a pipe created with os.popen(); why?</a></li>
 
110
<li><a class="reference internal" href="#how-do-i-run-a-subprocess-with-pipes-connected-to-both-input-and-output" id="id24">How do I run a subprocess with pipes connected to both input and output?</a></li>
 
111
<li><a class="reference internal" href="#how-do-i-access-the-serial-rs232-port" id="id25">How do I access the serial (RS232) port?</a></li>
 
112
<li><a class="reference internal" href="#why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it" id="id26">Why doesn&#8217;t closing sys.stdout (stdin, stderr) really close it?</a></li>
 
113
</ul>
 
114
</li>
 
115
<li><a class="reference internal" href="#network-internet-programming" id="id27">Network/Internet Programming</a><ul>
 
116
<li><a class="reference internal" href="#what-www-tools-are-there-for-python" id="id28">What WWW tools are there for Python?</a></li>
 
117
<li><a class="reference internal" href="#how-can-i-mimic-cgi-form-submission-method-post" id="id29">How can I mimic CGI form submission (METHOD=POST)?</a></li>
 
118
<li><a class="reference internal" href="#what-module-should-i-use-to-help-with-generating-html" id="id30">What module should I use to help with generating HTML?</a></li>
 
119
<li><a class="reference internal" href="#how-do-i-send-mail-from-a-python-script" id="id31">How do I send mail from a Python script?</a></li>
 
120
<li><a class="reference internal" href="#how-do-i-avoid-blocking-in-the-connect-method-of-a-socket" id="id32">How do I avoid blocking in the connect() method of a socket?</a></li>
 
121
</ul>
 
122
</li>
 
123
<li><a class="reference internal" href="#databases" id="id33">Databases</a><ul>
 
124
<li><a class="reference internal" href="#are-there-any-interfaces-to-database-packages-in-python" id="id34">Are there any interfaces to database packages in Python?</a></li>
 
125
<li><a class="reference internal" href="#how-do-you-implement-persistent-objects-in-python" id="id35">How do you implement persistent objects in Python?</a></li>
 
126
<li><a class="reference internal" href="#why-is-cpickle-so-slow" id="id36">Why is cPickle so slow?</a></li>
 
127
<li><a class="reference internal" href="#if-my-program-crashes-with-a-bsddb-or-anydbm-database-open-it-gets-corrupted-how-come" id="id37">If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</a></li>
 
128
<li><a class="reference internal" href="#i-tried-to-open-berkeley-db-file-but-bsddb-produces-bsddb-error-22-invalid-argument-help-how-can-i-restore-my-data" id="id38">I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, &#8216;Invalid argument&#8217;). Help! How can I restore my data?</a></li>
 
129
</ul>
 
130
</li>
 
131
<li><a class="reference internal" href="#mathematics-and-numerics" id="id39">Mathematics and Numerics</a><ul>
 
132
<li><a class="reference internal" href="#how-do-i-generate-random-numbers-in-python" id="id40">How do I generate random numbers in Python?</a></li>
 
133
</ul>
 
134
</li>
 
135
</ul>
 
136
</li>
 
137
</ul>
 
138
</div>
 
139
<div class="section" id="general-library-questions">
 
140
<h2><a class="toc-backref" href="#id2">General Library Questions</a><a class="headerlink" href="#general-library-questions" title="Permalink to this headline">¶</a></h2>
 
141
<div class="section" id="how-do-i-find-a-module-or-application-to-perform-task-x">
 
142
<h3><a class="toc-backref" href="#id3">How do I find a module or application to perform task X?</a><a class="headerlink" href="#how-do-i-find-a-module-or-application-to-perform-task-x" title="Permalink to this headline">¶</a></h3>
 
143
<p>Check <a class="reference internal" href="../library/index.html#library-index"><span>the Library Reference</span></a> to see if there&#8217;s a relevant
 
144
standard library module.  (Eventually you&#8217;ll learn what&#8217;s in the standard
 
145
library and will be able to skip this step.)</p>
 
146
<p>For third-party packages, search the <a class="reference external" href="https://pypi.python.org/pypi">Python Package Index</a> or try <a class="reference external" href="https://www.google.com">Google</a> or
 
147
another Web search engine.  Searching for &#8220;Python&#8221; plus a keyword or two for
 
148
your topic of interest will usually find something helpful.</p>
 
149
</div>
 
150
<div class="section" id="where-is-the-math-py-socket-py-regex-py-etc-source-file">
 
151
<h3><a class="toc-backref" href="#id4">Where is the math.py (socket.py, regex.py, etc.) source file?</a><a class="headerlink" href="#where-is-the-math-py-socket-py-regex-py-etc-source-file" title="Permalink to this headline">¶</a></h3>
 
152
<p>If you can&#8217;t find a source file for a module it may be a built-in or
 
153
dynamically loaded module implemented in C, C++ or other compiled language.
 
154
In this case you may not have the source file or it may be something like
 
155
<code class="file docutils literal"><span class="pre">mathmodule.c</span></code>, somewhere in a C source directory (not on the Python Path).</p>
 
156
<p>There are (at least) three kinds of modules in Python:</p>
 
157
<ol class="arabic">
 
158
<li><p class="first">modules written in Python (.py);</p>
 
159
</li>
 
160
<li><p class="first">modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);</p>
 
161
</li>
 
162
<li><p class="first">modules written in C and linked with the interpreter; to get a list of these,
 
163
type:</p>
 
164
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
 
165
<span class="k">print</span> <span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span>
 
166
</pre></div>
 
167
</div>
 
168
</li>
 
169
</ol>
 
170
</div>
 
171
<div class="section" id="how-do-i-make-a-python-script-executable-on-unix">
 
172
<h3><a class="toc-backref" href="#id5">How do I make a Python script executable on Unix?</a><a class="headerlink" href="#how-do-i-make-a-python-script-executable-on-unix" title="Permalink to this headline">¶</a></h3>
 
173
<p>You need to do two things: the script file&#8217;s mode must be executable and the
 
174
first line must begin with <code class="docutils literal"><span class="pre">#!</span></code> followed by the path of the Python
 
175
interpreter.</p>
 
176
<p>The first is done by executing <code class="docutils literal"><span class="pre">chmod</span> <span class="pre">+x</span> <span class="pre">scriptfile</span></code> or perhaps <code class="docutils literal"><span class="pre">chmod</span> <span class="pre">755</span>
 
177
<span class="pre">scriptfile</span></code>.</p>
 
178
<p>The second can be done in a number of ways.  The most straightforward way is to
 
179
write</p>
 
180
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="ch">#!/usr/local/bin/python</span>
 
181
</pre></div>
 
182
</div>
 
183
<p>as the very first line of your file, using the pathname for where the Python
 
184
interpreter is installed on your platform.</p>
 
185
<p>If you would like the script to be independent of where the Python interpreter
 
186
lives, you can use the <strong class="program">env</strong> program.  Almost all Unix variants support
 
187
the following, assuming the Python interpreter is in a directory on the user&#8217;s
 
188
<span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal"><span class="pre">PATH</span></code>:</p>
 
189
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
 
190
</pre></div>
 
191
</div>
 
192
<p><em>Don&#8217;t</em> do this for CGI scripts.  The <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal"><span class="pre">PATH</span></code> variable for CGI scripts is
 
193
often very minimal, so you need to use the actual absolute pathname of the
 
194
interpreter.</p>
 
195
<p>Occasionally, a user&#8217;s environment is so full that the <strong class="program">/usr/bin/env</strong>
 
196
program fails; or there&#8217;s no env program at all.  In that case, you can try the
 
197
following hack (due to Alex Rezinsky):</p>
 
198
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="ch">#! /bin/sh</span>
 
199
<span class="sd">&quot;&quot;&quot;:&quot;</span>
 
200
<span class="sd">exec python $0 ${1+&quot;$@&quot;}</span>
 
201
<span class="sd">&quot;&quot;&quot;</span>
 
202
</pre></div>
 
203
</div>
 
204
<p>The minor disadvantage is that this defines the script&#8217;s __doc__ string.
 
205
However, you can fix that by adding</p>
 
206
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">__doc__</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;...Whatever...&quot;&quot;&quot;</span>
 
207
</pre></div>
 
208
</div>
 
209
</div>
 
210
<div class="section" id="is-there-a-curses-termcap-package-for-python">
 
211
<h3><a class="toc-backref" href="#id6">Is there a curses/termcap package for Python?</a><a class="headerlink" href="#is-there-a-curses-termcap-package-for-python" title="Permalink to this headline">¶</a></h3>
 
212
<p>For Unix variants the standard Python source distribution comes with a curses
 
213
module in the <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Modules">Modules</a> subdirectory, though it&#8217;s not compiled by default.
 
214
(Note that this is not available in the Windows distribution &#8211; there is no
 
215
curses module for Windows.)</p>
 
216
<p>The <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">curses</span></code></a> module supports basic curses features as well as many additional
 
217
functions from ncurses and SYSV curses such as colour, alternative character set
 
218
support, pads, and mouse support. This means the module isn&#8217;t compatible with
 
219
operating systems that only have BSD curses, but there don&#8217;t seem to be any
 
220
currently maintained OSes that fall into this category.</p>
 
221
<p>For Windows: use <a class="reference external" href="http://effbot.org/zone/console-index.htm">the consolelib module</a>.</p>
 
222
</div>
 
223
<div class="section" id="is-there-an-equivalent-to-c-s-onexit-in-python">
 
224
<h3><a class="toc-backref" href="#id7">Is there an equivalent to C&#8217;s onexit() in Python?</a><a class="headerlink" href="#is-there-an-equivalent-to-c-s-onexit-in-python" title="Permalink to this headline">¶</a></h3>
 
225
<p>The <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal"><span class="pre">atexit</span></code></a> module provides a register function that is similar to C&#8217;s
 
226
<code class="xref c c-func docutils literal"><span class="pre">onexit()</span></code>.</p>
 
227
</div>
 
228
<div class="section" id="why-don-t-my-signal-handlers-work">
 
229
<h3><a class="toc-backref" href="#id8">Why don&#8217;t my signal handlers work?</a><a class="headerlink" href="#why-don-t-my-signal-handlers-work" title="Permalink to this headline">¶</a></h3>
 
230
<p>The most common problem is that the signal handler is declared with the wrong
 
231
argument list.  It is called as</p>
 
232
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">handler</span><span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">frame</span><span class="p">)</span>
 
233
</pre></div>
 
234
</div>
 
235
<p>so it should be declared with two arguments:</p>
 
236
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">signum</span><span class="p">,</span> <span class="n">frame</span><span class="p">):</span>
 
237
    <span class="o">...</span>
 
238
</pre></div>
 
239
</div>
 
240
</div>
 
241
</div>
 
242
<div class="section" id="common-tasks">
 
243
<h2><a class="toc-backref" href="#id9">Common tasks</a><a class="headerlink" href="#common-tasks" title="Permalink to this headline">¶</a></h2>
 
244
<div class="section" id="how-do-i-test-a-python-program-or-component">
 
245
<h3><a class="toc-backref" href="#id10">How do I test a Python program or component?</a><a class="headerlink" href="#how-do-i-test-a-python-program-or-component" title="Permalink to this headline">¶</a></h3>
 
246
<p>Python comes with two testing frameworks.  The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal"><span class="pre">doctest</span></code></a> module finds
 
247
examples in the docstrings for a module and runs them, comparing the output with
 
248
the expected output given in the docstring.</p>
 
249
<p>The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> module is a fancier testing framework modelled on Java and
 
250
Smalltalk testing frameworks.</p>
 
251
<p>To make testing easier, you should use good modular design in your program.
 
252
Your program should have almost all functionality
 
253
encapsulated in either functions or class methods &#8211; and this sometimes has the
 
254
surprising and delightful effect of making the program run faster (because local
 
255
variable accesses are faster than global accesses).  Furthermore the program
 
256
should avoid depending on mutating global variables, since this makes testing
 
257
much more difficult to do.</p>
 
258
<p>The &#8220;global main logic&#8221; of your program may be as simple as</p>
 
259
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
 
260
    <span class="n">main_logic</span><span class="p">()</span>
 
261
</pre></div>
 
262
</div>
 
263
<p>at the bottom of the main module of your program.</p>
 
264
<p>Once your program is organized as a tractable collection of functions and class
 
265
behaviours you should write test functions that exercise the behaviours.  A test
 
266
suite that automates a sequence of tests can be associated with each module.
 
267
This sounds like a lot of work, but since Python is so terse and flexible it&#8217;s
 
268
surprisingly easy.  You can make coding much more pleasant and fun by writing
 
269
your test functions in parallel with the &#8220;production code&#8221;, since this makes it
 
270
easy to find bugs and even design flaws earlier.</p>
 
271
<p>&#8220;Support modules&#8221; that are not intended to be the main module of a program may
 
272
include a self-test of the module.</p>
 
273
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
 
274
    <span class="n">self_test</span><span class="p">()</span>
 
275
</pre></div>
 
276
</div>
 
277
<p>Even programs that interact with complex external interfaces may be tested when
 
278
the external interfaces are unavailable by using &#8220;fake&#8221; interfaces implemented
 
279
in Python.</p>
 
280
</div>
 
281
<div class="section" id="how-do-i-create-documentation-from-doc-strings">
 
282
<h3><a class="toc-backref" href="#id11">How do I create documentation from doc strings?</a><a class="headerlink" href="#how-do-i-create-documentation-from-doc-strings" title="Permalink to this headline">¶</a></h3>
 
283
<p>The <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal"><span class="pre">pydoc</span></code></a> module can create HTML from the doc strings in your Python
 
284
source code.  An alternative for creating API documentation purely from
 
285
docstrings is <a class="reference external" href="http://epydoc.sourceforge.net/">epydoc</a>.  <a class="reference external" href="http://sphinx-doc.org">Sphinx</a> can also include docstring content.</p>
 
286
</div>
 
287
<div class="section" id="how-do-i-get-a-single-keypress-at-a-time">
 
288
<h3><a class="toc-backref" href="#id12">How do I get a single keypress at a time?</a><a class="headerlink" href="#how-do-i-get-a-single-keypress-at-a-time" title="Permalink to this headline">¶</a></h3>
 
289
<p>For Unix variants there are several solutions.  It&#8217;s straightforward to do this
 
290
using curses, but curses is a fairly large module to learn.  Here&#8217;s a solution
 
291
without curses:</p>
 
292
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">termios</span><span class="o">,</span> <span class="nn">fcntl</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
 
293
<span class="n">fd</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span>
 
294
 
 
295
<span class="n">oldterm</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
 
296
<span class="n">newattr</span> <span class="o">=</span> <span class="n">termios</span><span class="o">.</span><span class="n">tcgetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
 
297
<span class="n">newattr</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">newattr</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&amp;</span> <span class="o">~</span><span class="n">termios</span><span class="o">.</span><span class="n">ICANON</span> <span class="o">&amp;</span> <span class="o">~</span><span class="n">termios</span><span class="o">.</span><span class="n">ECHO</span>
 
298
<span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSANOW</span><span class="p">,</span> <span class="n">newattr</span><span class="p">)</span>
 
299
 
 
300
<span class="n">oldflags</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_GETFL</span><span class="p">)</span>
 
301
<span class="n">fcntl</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_SETFL</span><span class="p">,</span> <span class="n">oldflags</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">O_NONBLOCK</span><span class="p">)</span>
 
302
 
 
303
<span class="k">try</span><span class="p">:</span>
 
304
    <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
 
305
        <span class="k">try</span><span class="p">:</span>
 
306
            <span class="n">c</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
307
            <span class="k">print</span> <span class="s2">&quot;Got character&quot;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
 
308
        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span> <span class="k">pass</span>
 
309
<span class="k">finally</span><span class="p">:</span>
 
310
    <span class="n">termios</span><span class="o">.</span><span class="n">tcsetattr</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">termios</span><span class="o">.</span><span class="n">TCSAFLUSH</span><span class="p">,</span> <span class="n">oldterm</span><span class="p">)</span>
 
311
    <span class="n">fcntl</span><span class="o">.</span><span class="n">fcntl</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">F_SETFL</span><span class="p">,</span> <span class="n">oldflags</span><span class="p">)</span>
 
312
</pre></div>
 
313
</div>
 
314
<p>You need the <a class="reference internal" href="../library/termios.html#module-termios" title="termios: POSIX style tty control. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">termios</span></code></a> and the <a class="reference internal" href="../library/fcntl.html#module-fcntl" title="fcntl: The fcntl() and ioctl() system calls. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fcntl</span></code></a> module for any of this to work,
 
315
and I&#8217;ve only tried it on Linux, though it should work elsewhere.  In this code,
 
316
characters are read and printed one at a time.</p>
 
317
<p><a class="reference internal" href="../library/termios.html#termios.tcsetattr" title="termios.tcsetattr"><code class="xref py py-func docutils literal"><span class="pre">termios.tcsetattr()</span></code></a> turns off stdin&#8217;s echoing and disables canonical mode.
 
318
<code class="xref py py-func docutils literal"><span class="pre">fcntl.fnctl()</span></code> is used to obtain stdin&#8217;s file descriptor flags and modify
 
319
them for non-blocking mode.  Since reading stdin when it is empty results in an
 
320
<a class="reference internal" href="../library/exceptions.html#exceptions.IOError" title="exceptions.IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>, this error is caught and ignored.</p>
 
321
</div>
 
322
</div>
 
323
<div class="section" id="threads">
 
324
<h2><a class="toc-backref" href="#id13">Threads</a><a class="headerlink" href="#threads" title="Permalink to this headline">¶</a></h2>
 
325
<div class="section" id="how-do-i-program-using-threads">
 
326
<h3><a class="toc-backref" href="#id14">How do I program using threads?</a><a class="headerlink" href="#how-do-i-program-using-threads" title="Permalink to this headline">¶</a></h3>
 
327
<p>Be sure to use the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><code class="xref py py-mod docutils literal"><span class="pre">threading</span></code></a> module and not the <a class="reference internal" href="../library/thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><code class="xref py py-mod docutils literal"><span class="pre">thread</span></code></a> module.
 
328
The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><code class="xref py py-mod docutils literal"><span class="pre">threading</span></code></a> module builds convenient abstractions on top of the
 
329
low-level primitives provided by the <a class="reference internal" href="../library/thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><code class="xref py py-mod docutils literal"><span class="pre">thread</span></code></a> module.</p>
 
330
<p>Aahz has a set of slides from his threading tutorial that are helpful; see
 
331
<a class="reference external" href="http://www.pythoncraft.com/OSCON2001/">http://www.pythoncraft.com/OSCON2001/</a>.</p>
 
332
</div>
 
333
<div class="section" id="none-of-my-threads-seem-to-run-why">
 
334
<h3><a class="toc-backref" href="#id15">None of my threads seem to run: why?</a><a class="headerlink" href="#none-of-my-threads-seem-to-run-why" title="Permalink to this headline">¶</a></h3>
 
335
<p>As soon as the main thread exits, all threads are killed.  Your main thread is
 
336
running too quickly, giving the threads no time to do any work.</p>
 
337
<p>A simple fix is to add a sleep to the end of the program that&#8217;s long enough for
 
338
all the threads to finish:</p>
 
339
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">time</span>
 
340
 
 
341
<span class="k">def</span> <span class="nf">thread_task</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
 
342
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">i</span>
 
343
 
 
344
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
 
345
    <span class="n">T</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">thread_task</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">i</span><span class="p">))</span>
 
346
    <span class="n">T</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
 
347
 
 
348
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># &lt;----------------------------!</span>
 
349
</pre></div>
 
350
</div>
 
351
<p>But now (on many platforms) the threads don&#8217;t run in parallel, but appear to run
 
352
sequentially, one at a time!  The reason is that the OS thread scheduler doesn&#8217;t
 
353
start a new thread until the previous thread is blocked.</p>
 
354
<p>A simple fix is to add a tiny sleep to the start of the run function:</p>
 
355
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">thread_task</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
 
356
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">)</span> <span class="c1"># &lt;---------------------!</span>
 
357
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">i</span>
 
358
 
 
359
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
 
360
    <span class="n">T</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">thread_task</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">i</span><span class="p">))</span>
 
361
    <span class="n">T</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
 
362
 
 
363
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
 
364
</pre></div>
 
365
</div>
 
366
<p>Instead of trying to guess a good delay value for <a class="reference internal" href="../library/time.html#time.sleep" title="time.sleep"><code class="xref py py-func docutils literal"><span class="pre">time.sleep()</span></code></a>,
 
367
it&#8217;s better to use some kind of semaphore mechanism.  One idea is to use the
 
368
<a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><code class="xref py py-mod docutils literal"><span class="pre">Queue</span></code></a> module to create a queue object, let each thread append a token to
 
369
the queue when it finishes, and let the main thread read as many tokens from the
 
370
queue as there are threads.</p>
 
371
</div>
 
372
<div class="section" id="how-do-i-parcel-out-work-among-a-bunch-of-worker-threads">
 
373
<h3><a class="toc-backref" href="#id16">How do I parcel out work among a bunch of worker threads?</a><a class="headerlink" href="#how-do-i-parcel-out-work-among-a-bunch-of-worker-threads" title="Permalink to this headline">¶</a></h3>
 
374
<p>Use the <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><code class="xref py py-mod docutils literal"><span class="pre">Queue</span></code></a> module to create a queue containing a list of jobs.  The
 
375
<a class="reference internal" href="../library/queue.html#Queue.Queue" title="Queue.Queue"><code class="xref py py-class docutils literal"><span class="pre">Queue</span></code></a> class maintains a list of objects and has a <code class="docutils literal"><span class="pre">.put(obj)</span></code>
 
376
method that adds items to the queue and a <code class="docutils literal"><span class="pre">.get()</span></code> method to return them.
 
377
The class will take care of the locking necessary to ensure that each job is
 
378
handed out exactly once.</p>
 
379
<p>Here&#8217;s a trivial example:</p>
 
380
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">Queue</span><span class="o">,</span> <span class="nn">time</span>
 
381
 
 
382
<span class="c1"># The worker thread gets jobs off the queue.  When the queue is empty, it</span>
 
383
<span class="c1"># assumes there will be no more work and exits.</span>
 
384
<span class="c1"># (Realistically workers will run until terminated.)</span>
 
385
<span class="k">def</span> <span class="nf">worker</span><span class="p">():</span>
 
386
    <span class="k">print</span> <span class="s1">&#39;Running worker&#39;</span>
 
387
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
 
388
    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
 
389
        <span class="k">try</span><span class="p">:</span>
 
390
            <span class="n">arg</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
 
391
        <span class="k">except</span> <span class="n">Queue</span><span class="o">.</span><span class="n">Empty</span><span class="p">:</span>
 
392
            <span class="k">print</span> <span class="s1">&#39;Worker&#39;</span><span class="p">,</span> <span class="n">threading</span><span class="o">.</span><span class="n">currentThread</span><span class="p">(),</span>
 
393
            <span class="k">print</span> <span class="s1">&#39;queue empty&#39;</span>
 
394
            <span class="k">break</span>
 
395
        <span class="k">else</span><span class="p">:</span>
 
396
            <span class="k">print</span> <span class="s1">&#39;Worker&#39;</span><span class="p">,</span> <span class="n">threading</span><span class="o">.</span><span class="n">currentThread</span><span class="p">(),</span>
 
397
            <span class="k">print</span> <span class="s1">&#39;running with argument&#39;</span><span class="p">,</span> <span class="n">arg</span>
 
398
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
 
399
 
 
400
<span class="c1"># Create queue</span>
 
401
<span class="n">q</span> <span class="o">=</span> <span class="n">Queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
 
402
 
 
403
<span class="c1"># Start a pool of 5 workers</span>
 
404
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
 
405
    <span class="n">t</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;worker </span><span class="si">%i</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
 
406
    <span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
 
407
 
 
408
<span class="c1"># Begin adding work to the queue</span>
 
409
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">50</span><span class="p">):</span>
 
410
    <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
 
411
 
 
412
<span class="c1"># Give threads time to run</span>
 
413
<span class="k">print</span> <span class="s1">&#39;Main thread sleeping&#39;</span>
 
414
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
 
415
</pre></div>
 
416
</div>
 
417
<p>When run, this will produce the following output:</p>
 
418
<div class="highlight-none"><div class="highlight"><pre><span></span>Running worker
 
419
Running worker
 
420
Running worker
 
421
Running worker
 
422
Running worker
 
423
Main thread sleeping
 
424
Worker &lt;Thread(worker 1, started)&gt; running with argument 0
 
425
Worker &lt;Thread(worker 2, started)&gt; running with argument 1
 
426
Worker &lt;Thread(worker 3, started)&gt; running with argument 2
 
427
Worker &lt;Thread(worker 4, started)&gt; running with argument 3
 
428
Worker &lt;Thread(worker 5, started)&gt; running with argument 4
 
429
Worker &lt;Thread(worker 1, started)&gt; running with argument 5
 
430
...
 
431
</pre></div>
 
432
</div>
 
433
<p>Consult the module&#8217;s documentation for more details; the <a class="reference internal" href="../library/queue.html#Queue.Queue" title="Queue.Queue"><code class="xref py py-class docutils literal"><span class="pre">Queue</span></code></a>
 
434
class provides a featureful interface.</p>
 
435
</div>
 
436
<div class="section" id="what-kinds-of-global-value-mutation-are-thread-safe">
 
437
<h3><a class="toc-backref" href="#id17">What kinds of global value mutation are thread-safe?</a><a class="headerlink" href="#what-kinds-of-global-value-mutation-are-thread-safe" title="Permalink to this headline">¶</a></h3>
 
438
<p>A <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global interpreter lock</span></a> (GIL) is used internally to ensure that only
 
439
one thread runs in the Python VM at a time.  In general, Python offers to switch
 
440
among threads only between bytecode instructions; how frequently it switches can
 
441
be set via <a class="reference internal" href="../library/sys.html#sys.setcheckinterval" title="sys.setcheckinterval"><code class="xref py py-func docutils literal"><span class="pre">sys.setcheckinterval()</span></code></a>.  Each bytecode instruction and
 
442
therefore all the C implementation code reached from each instruction is
 
443
therefore atomic from the point of view of a Python program.</p>
 
444
<p>In theory, this means an exact accounting requires an exact understanding of the
 
445
PVM bytecode implementation.  In practice, it means that operations on shared
 
446
variables of built-in data types (ints, lists, dicts, etc) that &#8220;look atomic&#8221;
 
447
really are.</p>
 
448
<p>For example, the following operations are all atomic (L, L1, L2 are lists, D,
 
449
D1, D2 are dicts, x, y are objects, i, j are ints):</p>
 
450
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
 
451
<span class="n">L1</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">L2</span><span class="p">)</span>
 
452
<span class="n">x</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
 
453
<span class="n">x</span> <span class="o">=</span> <span class="n">L</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
 
454
<span class="n">L1</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">L2</span>
 
455
<span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
 
456
<span class="n">x</span> <span class="o">=</span> <span class="n">y</span>
 
457
<span class="n">x</span><span class="o">.</span><span class="n">field</span> <span class="o">=</span> <span class="n">y</span>
 
458
<span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span>
 
459
<span class="n">D1</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">D2</span><span class="p">)</span>
 
460
<span class="n">D</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
 
461
</pre></div>
 
462
</div>
 
463
<p>These aren&#8217;t:</p>
 
464
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="n">i</span><span class="o">+</span><span class="mi">1</span>
 
465
<span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">L</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
 
466
<span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
 
467
<span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">D</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
 
468
</pre></div>
 
469
</div>
 
470
<p>Operations that replace other objects may invoke those other objects&#8217;
 
471
<a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal"><span class="pre">__del__()</span></code></a> method when their reference count reaches zero, and that can
 
472
affect things.  This is especially true for the mass updates to dictionaries and
 
473
lists.  When in doubt, use a mutex!</p>
 
474
</div>
 
475
<div class="section" id="can-t-we-get-rid-of-the-global-interpreter-lock">
 
476
<h3><a class="toc-backref" href="#id18">Can&#8217;t we get rid of the Global Interpreter Lock?</a><a class="headerlink" href="#can-t-we-get-rid-of-the-global-interpreter-lock" title="Permalink to this headline">¶</a></h3>
 
477
<p>The <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global interpreter lock</span></a> (GIL) is often seen as a hindrance to Python&#8217;s
 
478
deployment on high-end multiprocessor server machines, because a multi-threaded
 
479
Python program effectively only uses one CPU, due to the insistence that
 
480
(almost) all Python code can only run while the GIL is held.</p>
 
481
<p>Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive
 
482
patch set (the &#8220;free threading&#8221; patches) that removed the GIL and replaced it
 
483
with fine-grained locking.  Unfortunately, even on Windows (where locks are very
 
484
efficient) this ran ordinary Python code about twice as slow as the interpreter
 
485
using the GIL.  On Linux the performance loss was even worse because pthread
 
486
locks aren&#8217;t as efficient.</p>
 
487
<p>Since then, the idea of getting rid of the GIL has occasionally come up but
 
488
nobody has found a way to deal with the expected slowdown, and users who don&#8217;t
 
489
use threads would not be happy if their code ran at half the speed.  Greg&#8217;s
 
490
free threading patch set has not been kept up-to-date for later Python versions.</p>
 
491
<p>This doesn&#8217;t mean that you can&#8217;t make good use of Python on multi-CPU machines!
 
492
You just have to be creative with dividing the work up between multiple
 
493
<em>processes</em> rather than multiple <em>threads</em>.  Judicious use of C extensions will
 
494
also help; if you use a C extension to perform a time-consuming task, the
 
495
extension can release the GIL while the thread of execution is in the C code and
 
496
allow other threads to get some work done.</p>
 
497
<p>It has been suggested that the GIL should be a per-interpreter-state lock rather
 
498
than truly global; interpreters then wouldn&#8217;t be able to share objects.
 
499
Unfortunately, this isn&#8217;t likely to happen either.  It would be a tremendous
 
500
amount of work, because many object implementations currently have global state.
 
501
For example, small integers and short strings are cached; these caches would
 
502
have to be moved to the interpreter state.  Other object types have their own
 
503
free list; these free lists would have to be moved to the interpreter state.
 
504
And so on.</p>
 
505
<p>And I doubt that it can even be done in finite time, because the same problem
 
506
exists for 3rd party extensions.  It is likely that 3rd party extensions are
 
507
being written at a faster rate than you can convert them to store all their
 
508
global state in the interpreter state.</p>
 
509
<p>And finally, once you have multiple interpreters not sharing any state, what
 
510
have you gained over running each interpreter in a separate process?</p>
 
511
</div>
 
512
</div>
 
513
<div class="section" id="input-and-output">
 
514
<h2><a class="toc-backref" href="#id19">Input and Output</a><a class="headerlink" href="#input-and-output" title="Permalink to this headline">¶</a></h2>
 
515
<div class="section" id="how-do-i-delete-a-file-and-other-file-questions">
 
516
<h3><a class="toc-backref" href="#id20">How do I delete a file? (And other file questions...)</a><a class="headerlink" href="#how-do-i-delete-a-file-and-other-file-questions" title="Permalink to this headline">¶</a></h3>
 
517
<p>Use <code class="docutils literal"><span class="pre">os.remove(filename)</span></code> or <code class="docutils literal"><span class="pre">os.unlink(filename)</span></code>; for documentation, see
 
518
the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a> module.  The two functions are identical; <code class="xref py py-func docutils literal"><span class="pre">unlink()</span></code> is simply
 
519
the name of the Unix system call for this function.</p>
 
520
<p>To remove a directory, use <a class="reference internal" href="../library/os.html#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal"><span class="pre">os.rmdir()</span></code></a>; use <a class="reference internal" href="../library/os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal"><span class="pre">os.mkdir()</span></code></a> to create one.
 
521
<code class="docutils literal"><span class="pre">os.makedirs(path)</span></code> will create any intermediate directories in <code class="docutils literal"><span class="pre">path</span></code> that
 
522
don&#8217;t exist. <code class="docutils literal"><span class="pre">os.removedirs(path)</span></code> will remove intermediate directories as
 
523
long as they&#8217;re empty; if you want to delete an entire directory tree and its
 
524
contents, use <a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal"><span class="pre">shutil.rmtree()</span></code></a>.</p>
 
525
<p>To rename a file, use <code class="docutils literal"><span class="pre">os.rename(old_path,</span> <span class="pre">new_path)</span></code>.</p>
 
526
<p>To truncate a file, open it using <code class="docutils literal"><span class="pre">f</span> <span class="pre">=</span> <span class="pre">open(filename,</span> <span class="pre">&quot;r+&quot;)</span></code>, and use
 
527
<code class="docutils literal"><span class="pre">f.truncate(offset)</span></code>; offset defaults to the current seek position.  There&#8217;s
 
528
also <code class="docutils literal"><span class="pre">os.ftruncate(fd,</span> <span class="pre">offset)</span></code> for files opened with <a class="reference internal" href="../library/os.html#os.open" title="os.open"><code class="xref py py-func docutils literal"><span class="pre">os.open()</span></code></a>, where
 
529
<em>fd</em> is the file descriptor (a small integer).</p>
 
530
<p>The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a> module also contains a number of functions to work on files
 
531
including <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal"><span class="pre">copyfile()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal"><span class="pre">copytree()</span></code></a>, and
 
532
<a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal"><span class="pre">rmtree()</span></code></a>.</p>
 
533
</div>
 
534
<div class="section" id="how-do-i-copy-a-file">
 
535
<h3><a class="toc-backref" href="#id21">How do I copy a file?</a><a class="headerlink" href="#how-do-i-copy-a-file" title="Permalink to this headline">¶</a></h3>
 
536
<p>The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a> module contains a <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal"><span class="pre">copyfile()</span></code></a> function.  Note
 
537
that on MacOS 9 it doesn&#8217;t copy the resource fork and Finder info.</p>
 
538
</div>
 
539
<div class="section" id="how-do-i-read-or-write-binary-data">
 
540
<h3><a class="toc-backref" href="#id22">How do I read (or write) binary data?</a><a class="headerlink" href="#how-do-i-read-or-write-binary-data" title="Permalink to this headline">¶</a></h3>
 
541
<p>To read or write complex binary data formats, it&#8217;s best to use the <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><code class="xref py py-mod docutils literal"><span class="pre">struct</span></code></a>
 
542
module.  It allows you to take a string containing binary data (usually numbers)
 
543
and convert it to Python objects; and vice versa.</p>
 
544
<p>For example, the following code reads two 2-byte integers and one 4-byte integer
 
545
in big-endian format from a file:</p>
 
546
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">struct</span>
 
547
 
 
548
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span>  <span class="c1"># Open in binary mode for portability</span>
 
549
<span class="n">s</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
 
550
<span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s2">&quot;&gt;hhl&quot;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
 
551
</pre></div>
 
552
</div>
 
553
<p>The &#8216;&gt;&#8217; in the format string forces big-endian data; the letter &#8216;h&#8217; reads one
 
554
&#8220;short integer&#8221; (2 bytes), and &#8216;l&#8217; reads one &#8220;long integer&#8221; (4 bytes) from the
 
555
string.</p>
 
556
<p>For data that is more regular (e.g. a homogeneous list of ints or floats),
 
557
you can also use the <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal"><span class="pre">array</span></code></a> module.</p>
 
558
</div>
 
559
<div class="section" id="i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why">
 
560
<h3><a class="toc-backref" href="#id23">I can&#8217;t seem to use os.read() on a pipe created with os.popen(); why?</a><a class="headerlink" href="#i-can-t-seem-to-use-os-read-on-a-pipe-created-with-os-popen-why" title="Permalink to this headline">¶</a></h3>
 
561
<p><a class="reference internal" href="../library/os.html#os.read" title="os.read"><code class="xref py py-func docutils literal"><span class="pre">os.read()</span></code></a> is a low-level function which takes a file descriptor, a small
 
562
integer representing the opened file.  <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal"><span class="pre">os.popen()</span></code></a> creates a high-level
 
563
file object, the same type returned by the built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function.
 
564
Thus, to read <em>n</em> bytes from a pipe <em>p</em> created with <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal"><span class="pre">os.popen()</span></code></a>, you need to
 
565
use <code class="docutils literal"><span class="pre">p.read(n)</span></code>.</p>
 
566
</div>
 
567
<div class="section" id="how-do-i-run-a-subprocess-with-pipes-connected-to-both-input-and-output">
 
568
<h3><a class="toc-backref" href="#id24">How do I run a subprocess with pipes connected to both input and output?</a><a class="headerlink" href="#how-do-i-run-a-subprocess-with-pipes-connected-to-both-input-and-output" title="Permalink to this headline">¶</a></h3>
 
569
<p>Use the <a class="reference internal" href="../library/popen2.html#module-popen2" title="popen2: Subprocesses with accessible standard I/O streams. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">popen2</span></code></a> module.  For example:</p>
 
570
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">popen2</span>
 
571
<span class="n">fromchild</span><span class="p">,</span> <span class="n">tochild</span> <span class="o">=</span> <span class="n">popen2</span><span class="o">.</span><span class="n">popen2</span><span class="p">(</span><span class="s2">&quot;command&quot;</span><span class="p">)</span>
 
572
<span class="n">tochild</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;input</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
573
<span class="n">tochild</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
 
574
<span class="n">output</span> <span class="o">=</span> <span class="n">fromchild</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 
575
</pre></div>
 
576
</div>
 
577
<p>Warning: in general it is unwise to do this because you can easily cause a
 
578
deadlock where your process is blocked waiting for output from the child while
 
579
the child is blocked waiting for input from you.  This can be caused by the
 
580
parent expecting the child to output more text than it does or by data being
 
581
stuck in stdio buffers due to lack of flushing.  The Python parent
 
582
can of course explicitly flush the data it sends to the child before it reads
 
583
any output, but if the child is a naive C program it may have been written to
 
584
never explicitly flush its output, even if it is interactive, since flushing is
 
585
normally automatic.</p>
 
586
<p>Note that a deadlock is also possible if you use <code class="xref py py-func docutils literal"><span class="pre">popen3()</span></code> to read stdout
 
587
and stderr. If one of the two is too large for the internal buffer (increasing
 
588
the buffer size does not help) and you <code class="docutils literal"><span class="pre">read()</span></code> the other one first, there is
 
589
a deadlock, too.</p>
 
590
<p>Note on a bug in popen2: unless your program calls <code class="docutils literal"><span class="pre">wait()</span></code> or <code class="docutils literal"><span class="pre">waitpid()</span></code>,
 
591
finished child processes are never removed, and eventually calls to popen2 will
 
592
fail because of a limit on the number of child processes.  Calling
 
593
<a class="reference internal" href="../library/os.html#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal"><span class="pre">os.waitpid()</span></code></a> with the <a class="reference internal" href="../library/os.html#os.WNOHANG" title="os.WNOHANG"><code class="xref py py-data docutils literal"><span class="pre">os.WNOHANG</span></code></a> option can prevent this; a good
 
594
place to insert such a call would be before calling <code class="docutils literal"><span class="pre">popen2</span></code> again.</p>
 
595
<p>In many cases, all you really need is to run some data through a command and get
 
596
the result back.  Unless the amount of data is very large, the easiest way to do
 
597
this is to write it to a temporary file and run the command with that temporary
 
598
file as input.  The standard module <a class="reference internal" href="../library/tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal"><span class="pre">tempfile</span></code></a> exports a
 
599
<a class="reference internal" href="../library/tempfile.html#tempfile.mktemp" title="tempfile.mktemp"><code class="xref py py-func docutils literal"><span class="pre">mktemp()</span></code></a> function to generate unique temporary file names.</p>
 
600
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tempfile</span>
 
601
<span class="kn">import</span> <span class="nn">os</span>
 
602
 
 
603
<span class="k">class</span> <span class="nc">Popen3</span><span class="p">:</span>
 
604
    <span class="sd">&quot;&quot;&quot;</span>
 
605
<span class="sd">    This is a deadlock-safe version of popen that returns</span>
 
606
<span class="sd">    an object with errorlevel, out (a string) and err (a string).</span>
 
607
<span class="sd">    (capturestderr may not work under windows.)</span>
 
608
<span class="sd">    Example: print Popen3(&#39;grep spam&#39;,&#39;\n\nhere spam\n\n&#39;).out</span>
 
609
<span class="sd">    &quot;&quot;&quot;</span>
 
610
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">command</span><span class="p">,</span><span class="nb">input</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">capturestderr</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
 
611
        <span class="n">outfile</span><span class="o">=</span><span class="n">tempfile</span><span class="o">.</span><span class="n">mktemp</span><span class="p">()</span>
 
612
        <span class="n">command</span><span class="o">=</span><span class="s2">&quot;( </span><span class="si">%s</span><span class="s2"> ) &gt; </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">command</span><span class="p">,</span><span class="n">outfile</span><span class="p">)</span>
 
613
        <span class="k">if</span> <span class="nb">input</span><span class="p">:</span>
 
614
            <span class="n">infile</span><span class="o">=</span><span class="n">tempfile</span><span class="o">.</span><span class="n">mktemp</span><span class="p">()</span>
 
615
            <span class="nb">open</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span><span class="s2">&quot;w&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
 
616
            <span class="n">command</span><span class="o">=</span><span class="n">command</span><span class="o">+</span><span class="s2">&quot; &lt;&quot;</span><span class="o">+</span><span class="n">infile</span>
 
617
        <span class="k">if</span> <span class="n">capturestderr</span><span class="p">:</span>
 
618
            <span class="n">errfile</span><span class="o">=</span><span class="n">tempfile</span><span class="o">.</span><span class="n">mktemp</span><span class="p">()</span>
 
619
            <span class="n">command</span><span class="o">=</span><span class="n">command</span><span class="o">+</span><span class="s2">&quot; 2&gt;&quot;</span><span class="o">+</span><span class="n">errfile</span>
 
620
        <span class="bp">self</span><span class="o">.</span><span class="n">errorlevel</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">command</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="mi">8</span>
 
621
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span><span class="s2">&quot;r&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
 
622
        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span>
 
623
        <span class="k">if</span> <span class="nb">input</span><span class="p">:</span>
 
624
            <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">infile</span><span class="p">)</span>
 
625
        <span class="k">if</span> <span class="n">capturestderr</span><span class="p">:</span>
 
626
            <span class="bp">self</span><span class="o">.</span><span class="n">err</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">errfile</span><span class="p">,</span><span class="s2">&quot;r&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
 
627
            <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">errfile</span><span class="p">)</span>
 
628
</pre></div>
 
629
</div>
 
630
<p>Note that many interactive programs (e.g. vi) don&#8217;t work well with pipes
 
631
substituted for standard input and output.  You will have to use pseudo ttys
 
632
(&#8220;ptys&#8221;) instead of pipes. Or you can use a Python interface to Don Libes&#8217;
 
633
&#8220;expect&#8221; library.  A Python extension that interfaces to expect is called &#8220;expy&#8221;
 
634
and available from <a class="reference external" href="http://expectpy.sourceforge.net">http://expectpy.sourceforge.net</a>.  A pure Python solution that
 
635
works like expect is <a class="reference external" href="https://pypi.python.org/pypi/pexpect/">pexpect</a>.</p>
 
636
</div>
 
637
<div class="section" id="how-do-i-access-the-serial-rs232-port">
 
638
<h3><a class="toc-backref" href="#id25">How do I access the serial (RS232) port?</a><a class="headerlink" href="#how-do-i-access-the-serial-rs232-port" title="Permalink to this headline">¶</a></h3>
 
639
<p>For Win32, POSIX (Linux, BSD, etc.), Jython:</p>
 
640
<blockquote>
 
641
<div><a class="reference external" href="http://pyserial.sourceforge.net">http://pyserial.sourceforge.net</a></div></blockquote>
 
642
<p>For Unix, see a Usenet post by Mitch Chapman:</p>
 
643
<blockquote>
 
644
<div><a class="reference external" href="https://groups.google.com/groups?selm=34A04430.CF9&#64;ohioee.com">https://groups.google.com/groups?selm=34A04430.CF9&#64;ohioee.com</a></div></blockquote>
 
645
</div>
 
646
<div class="section" id="why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it">
 
647
<h3><a class="toc-backref" href="#id26">Why doesn&#8217;t closing sys.stdout (stdin, stderr) really close it?</a><a class="headerlink" href="#why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it" title="Permalink to this headline">¶</a></h3>
 
648
<p>Python file objects are a high-level layer of abstraction on top of C streams,
 
649
which in turn are a medium-level layer of abstraction on top of (among other
 
650
things) low-level C file descriptors.</p>
 
651
<p>For most file objects you create in Python via the built-in <code class="docutils literal"><span class="pre">file</span></code>
 
652
constructor, <code class="docutils literal"><span class="pre">f.close()</span></code> marks the Python file object as being closed from
 
653
Python&#8217;s point of view, and also arranges to close the underlying C stream.
 
654
This also happens automatically in <code class="docutils literal"><span class="pre">f</span></code>&#8216;s destructor, when <code class="docutils literal"><span class="pre">f</span></code> becomes
 
655
garbage.</p>
 
656
<p>But stdin, stdout and stderr are treated specially by Python, because of the
 
657
special status also given to them by C.  Running <code class="docutils literal"><span class="pre">sys.stdout.close()</span></code> marks
 
658
the Python-level file object as being closed, but does <em>not</em> close the
 
659
associated C stream.</p>
 
660
<p>To close the underlying C stream for one of these three, you should first be
 
661
sure that&#8217;s what you really want to do (e.g., you may confuse extension modules
 
662
trying to do I/O).  If it is, use os.close:</p>
 
663
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>   <span class="c1"># close C&#39;s stdin stream</span>
 
664
<span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>   <span class="c1"># close C&#39;s stdout stream</span>
 
665
<span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>   <span class="c1"># close C&#39;s stderr stream</span>
 
666
</pre></div>
 
667
</div>
 
668
</div>
 
669
</div>
 
670
<div class="section" id="network-internet-programming">
 
671
<h2><a class="toc-backref" href="#id27">Network/Internet Programming</a><a class="headerlink" href="#network-internet-programming" title="Permalink to this headline">¶</a></h2>
 
672
<div class="section" id="what-www-tools-are-there-for-python">
 
673
<h3><a class="toc-backref" href="#id28">What WWW tools are there for Python?</a><a class="headerlink" href="#what-www-tools-are-there-for-python" title="Permalink to this headline">¶</a></h3>
 
674
<p>See the chapters titled <a class="reference internal" href="../library/internet.html#internet"><span>Internet Protocols and Support</span></a> and <a class="reference internal" href="../library/netdata.html#netdata"><span>Internet Data Handling</span></a> in the Library
 
675
Reference Manual.  Python has many modules that will help you build server-side
 
676
and client-side web systems.</p>
 
677
<p>A summary of available frameworks is maintained by Paul Boddie at
 
678
<a class="reference external" href="https://wiki.python.org/moin/WebProgramming">https://wiki.python.org/moin/WebProgramming</a>.</p>
 
679
<p>Cameron Laird maintains a useful set of pages about Python web technologies at
 
680
<a class="reference external" href="http://phaseit.net/claird/comp.lang.python/web_python">http://phaseit.net/claird/comp.lang.python/web_python</a>.</p>
 
681
</div>
 
682
<div class="section" id="how-can-i-mimic-cgi-form-submission-method-post">
 
683
<h3><a class="toc-backref" href="#id29">How can I mimic CGI form submission (METHOD=POST)?</a><a class="headerlink" href="#how-can-i-mimic-cgi-form-submission-method-post" title="Permalink to this headline">¶</a></h3>
 
684
<p>I would like to retrieve web pages that are the result of POSTing a form. Is
 
685
there existing code that would let me do this easily?</p>
 
686
<p>Yes. Here&#8217;s a simple example that uses httplib:</p>
 
687
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="ch">#!/usr/local/bin/python</span>
 
688
 
 
689
<span class="kn">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">time</span>
 
690
 
 
691
<span class="c1"># build the query string</span>
 
692
<span class="n">qs</span> <span class="o">=</span> <span class="s2">&quot;First=Josephine&amp;MI=Q&amp;Last=Public&quot;</span>
 
693
 
 
694
<span class="c1"># connect and send the server a path</span>
 
695
<span class="n">httpobj</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTP</span><span class="p">(</span><span class="s1">&#39;www.some-server.out-there&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
 
696
<span class="n">httpobj</span><span class="o">.</span><span class="n">putrequest</span><span class="p">(</span><span class="s1">&#39;POST&#39;</span><span class="p">,</span> <span class="s1">&#39;/cgi-bin/some-cgi-script&#39;</span><span class="p">)</span>
 
697
<span class="c1"># now generate the rest of the HTTP headers...</span>
 
698
<span class="n">httpobj</span><span class="o">.</span><span class="n">putheader</span><span class="p">(</span><span class="s1">&#39;Accept&#39;</span><span class="p">,</span> <span class="s1">&#39;*/*&#39;</span><span class="p">)</span>
 
699
<span class="n">httpobj</span><span class="o">.</span><span class="n">putheader</span><span class="p">(</span><span class="s1">&#39;Connection&#39;</span><span class="p">,</span> <span class="s1">&#39;Keep-Alive&#39;</span><span class="p">)</span>
 
700
<span class="n">httpobj</span><span class="o">.</span><span class="n">putheader</span><span class="p">(</span><span class="s1">&#39;Content-type&#39;</span><span class="p">,</span> <span class="s1">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">)</span>
 
701
<span class="n">httpobj</span><span class="o">.</span><span class="n">putheader</span><span class="p">(</span><span class="s1">&#39;Content-length&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">qs</span><span class="p">))</span>
 
702
<span class="n">httpobj</span><span class="o">.</span><span class="n">endheaders</span><span class="p">()</span>
 
703
<span class="n">httpobj</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">qs</span><span class="p">)</span>
 
704
<span class="c1"># find out what the server said in response...</span>
 
705
<span class="n">reply</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">hdrs</span> <span class="o">=</span> <span class="n">httpobj</span><span class="o">.</span><span class="n">getreply</span><span class="p">()</span>
 
706
<span class="k">if</span> <span class="n">reply</span> <span class="o">!=</span> <span class="mi">200</span><span class="p">:</span>
 
707
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">httpobj</span><span class="o">.</span><span class="n">getfile</span><span class="p">()</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
 
708
</pre></div>
 
709
</div>
 
710
<p>Note that in general for percent-encoded POST operations, query strings must be
 
711
quoted using <a class="reference internal" href="../library/urllib.html#urllib.urlencode" title="urllib.urlencode"><code class="xref py py-func docutils literal"><span class="pre">urllib.urlencode()</span></code></a>.  For example, to send
 
712
<code class="docutils literal"><span class="pre">name=Guy</span> <span class="pre">Steele,</span> <span class="pre">Jr.</span></code>:</p>
 
713
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib</span>
 
714
<span class="gp">&gt;&gt;&gt; </span><span class="n">urllib</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Guy Steele, Jr.&#39;</span><span class="p">})</span>
 
715
<span class="go">&#39;name=Guy+Steele%2C+Jr.&#39;</span>
 
716
</pre></div>
 
717
</div>
 
718
</div>
 
719
<div class="section" id="what-module-should-i-use-to-help-with-generating-html">
 
720
<h3><a class="toc-backref" href="#id30">What module should I use to help with generating HTML?</a><a class="headerlink" href="#what-module-should-i-use-to-help-with-generating-html" title="Permalink to this headline">¶</a></h3>
 
721
<p>You can find a collection of useful links on the <a class="reference external" href="https://wiki.python.org/moin/WebProgramming">Web Programming wiki page</a>.</p>
 
722
</div>
 
723
<div class="section" id="how-do-i-send-mail-from-a-python-script">
 
724
<h3><a class="toc-backref" href="#id31">How do I send mail from a Python script?</a><a class="headerlink" href="#how-do-i-send-mail-from-a-python-script" title="Permalink to this headline">¶</a></h3>
 
725
<p>Use the standard library module <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">smtplib</span></code></a>.</p>
 
726
<p>Here&#8217;s a very simple interactive mail sender that uses it.  This method will
 
727
work on any host that supports an SMTP listener.</p>
 
728
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">smtplib</span>
 
729
 
 
730
<span class="n">fromaddr</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s2">&quot;From: &quot;</span><span class="p">)</span>
 
731
<span class="n">toaddrs</span>  <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s2">&quot;To: &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>
 
732
<span class="k">print</span> <span class="s2">&quot;Enter message, end with ^D:&quot;</span>
 
733
<span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
 
734
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
 
735
    <span class="n">line</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 
736
    <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
 
737
        <span class="k">break</span>
 
738
    <span class="n">msg</span> <span class="o">+=</span> <span class="n">line</span>
 
739
 
 
740
<span class="c1"># The actual mail send</span>
 
741
<span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">)</span>
 
742
<span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">fromaddr</span><span class="p">,</span> <span class="n">toaddrs</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
 
743
<span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
 
744
</pre></div>
 
745
</div>
 
746
<p>A Unix-only alternative uses sendmail.  The location of the sendmail program
 
747
varies between systems; sometimes it is <code class="docutils literal"><span class="pre">/usr/lib/sendmail</span></code>, sometimes
 
748
<code class="docutils literal"><span class="pre">/usr/sbin/sendmail</span></code>.  The sendmail manual page will help you out.  Here&#8217;s
 
749
some sample code:</p>
 
750
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
 
751
 
 
752
<span class="n">SENDMAIL</span> <span class="o">=</span> <span class="s2">&quot;/usr/sbin/sendmail&quot;</span>  <span class="c1"># sendmail location</span>
 
753
<span class="n">p</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> -t -i&quot;</span> <span class="o">%</span> <span class="n">SENDMAIL</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span>
 
754
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;To: receiver@example.com</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
755
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Subject: test</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
756
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span> <span class="c1"># blank line separating headers from body</span>
 
757
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Some text</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
758
<span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;some more text</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
759
<span class="n">sts</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
760
<span class="k">if</span> <span class="n">sts</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
 
761
    <span class="k">print</span> <span class="s2">&quot;Sendmail exit status&quot;</span><span class="p">,</span> <span class="n">sts</span>
 
762
</pre></div>
 
763
</div>
 
764
</div>
 
765
<div class="section" id="how-do-i-avoid-blocking-in-the-connect-method-of-a-socket">
 
766
<h3><a class="toc-backref" href="#id32">How do I avoid blocking in the connect() method of a socket?</a><a class="headerlink" href="#how-do-i-avoid-blocking-in-the-connect-method-of-a-socket" title="Permalink to this headline">¶</a></h3>
 
767
<p>The select module is commonly used to help with asynchronous I/O on sockets.</p>
 
768
<p>To prevent the TCP connect from blocking, you can set the socket to non-blocking
 
769
mode.  Then when you do the <code class="docutils literal"><span class="pre">connect()</span></code>, you will either connect immediately
 
770
(unlikely) or get an exception that contains the error number as <code class="docutils literal"><span class="pre">.errno</span></code>.
 
771
<code class="docutils literal"><span class="pre">errno.EINPROGRESS</span></code> indicates that the connection is in progress, but hasn&#8217;t
 
772
finished yet.  Different OSes will return different values, so you&#8217;re going to
 
773
have to check what&#8217;s returned on your system.</p>
 
774
<p>You can use the <code class="docutils literal"><span class="pre">connect_ex()</span></code> method to avoid creating an exception.  It will
 
775
just return the errno value.  To poll, you can call <code class="docutils literal"><span class="pre">connect_ex()</span></code> again later
 
776
&#8211; 0 or <code class="docutils literal"><span class="pre">errno.EISCONN</span></code> indicate that you&#8217;re connected &#8211; or you can pass this
 
777
socket to select to check if it&#8217;s writable.</p>
 
778
</div>
 
779
</div>
 
780
<div class="section" id="databases">
 
781
<h2><a class="toc-backref" href="#id33">Databases</a><a class="headerlink" href="#databases" title="Permalink to this headline">¶</a></h2>
 
782
<div class="section" id="are-there-any-interfaces-to-database-packages-in-python">
 
783
<h3><a class="toc-backref" href="#id34">Are there any interfaces to database packages in Python?</a><a class="headerlink" href="#are-there-any-interfaces-to-database-packages-in-python" title="Permalink to this headline">¶</a></h3>
 
784
<p>Yes.</p>
 
785
<p>Python 2.3 includes the <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><code class="xref py py-mod docutils literal"><span class="pre">bsddb</span></code></a> package which provides an interface to the
 
786
BerkeleyDB library.  Interfaces to disk-based hashes such as <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: The standard &quot;database&quot; interface, based on ndbm. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">DBM</span></code></a>
 
787
and <a class="reference internal" href="../library/gdbm.html#module-gdbm" title="gdbm: GNU's reinterpretation of dbm. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">GDBM</span></code></a> are also included with standard Python.</p>
 
788
<p>Support for most relational databases is available.  See the
 
789
<a class="reference external" href="https://wiki.python.org/moin/DatabaseProgramming">DatabaseProgramming wiki page</a> for details.</p>
 
790
</div>
 
791
<div class="section" id="how-do-you-implement-persistent-objects-in-python">
 
792
<h3><a class="toc-backref" href="#id35">How do you implement persistent objects in Python?</a><a class="headerlink" href="#how-do-you-implement-persistent-objects-in-python" title="Permalink to this headline">¶</a></h3>
 
793
<p>The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> library module solves this in a very general way (though you
 
794
still can&#8217;t store things like open files, sockets or windows), and the
 
795
<a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal"><span class="pre">shelve</span></code></a> library module uses pickle and (g)dbm to create persistent
 
796
mappings containing arbitrary Python objects.  For better performance, you can
 
797
use the <a class="reference internal" href="../library/pickle.html#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><code class="xref py py-mod docutils literal"><span class="pre">cPickle</span></code></a> module.</p>
 
798
<p>A more awkward way of doing things is to use pickle&#8217;s little sister, marshal.
 
799
The <a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal"><span class="pre">marshal</span></code></a> module provides very fast ways to store noncircular basic
 
800
Python types to files and strings, and back again.  Although marshal does not do
 
801
fancy things like store instances or handle shared references properly, it does
 
802
run extremely fast.  For example, loading a half megabyte of data may take less
 
803
than a third of a second.  This often beats doing something more complex and
 
804
general such as using gdbm with pickle/shelve.</p>
 
805
</div>
 
806
<div class="section" id="why-is-cpickle-so-slow">
 
807
<h3><a class="toc-backref" href="#id36">Why is cPickle so slow?</a><a class="headerlink" href="#why-is-cpickle-so-slow" title="Permalink to this headline">¶</a></h3>
 
808
<p>By default <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> uses a relatively old and slow format for backward
 
809
compatibility.  You can however specify other protocol versions that are
 
810
faster:</p>
 
811
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">largeString</span> <span class="o">=</span> <span class="s1">&#39;z&#39;</span> <span class="o">*</span> <span class="p">(</span><span class="mi">100</span> <span class="o">*</span> <span class="mi">1024</span><span class="p">)</span>
 
812
<span class="n">myPickle</span> <span class="o">=</span> <span class="n">cPickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">largeString</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
 
813
</pre></div>
 
814
</div>
 
815
</div>
 
816
<div class="section" id="if-my-program-crashes-with-a-bsddb-or-anydbm-database-open-it-gets-corrupted-how-come">
 
817
<h3><a class="toc-backref" href="#id37">If my program crashes with a bsddb (or anydbm) database open, it gets corrupted. How come?</a><a class="headerlink" href="#if-my-program-crashes-with-a-bsddb-or-anydbm-database-open-it-gets-corrupted-how-come" title="Permalink to this headline">¶</a></h3>
 
818
<p>Databases opened for write access with the bsddb module (and often by the anydbm
 
819
module, since it will preferentially use bsddb) must explicitly be closed using
 
820
the <code class="docutils literal"><span class="pre">.close()</span></code> method of the database.  The underlying library caches database
 
821
contents which need to be converted to on-disk form and written.</p>
 
822
<p>If you have initialized a new bsddb database but not written anything to it
 
823
before the program crashes, you will often wind up with a zero-length file and
 
824
encounter an exception the next time the file is opened.</p>
 
825
</div>
 
826
<div class="section" id="i-tried-to-open-berkeley-db-file-but-bsddb-produces-bsddb-error-22-invalid-argument-help-how-can-i-restore-my-data">
 
827
<h3><a class="toc-backref" href="#id38">I tried to open Berkeley DB file, but bsddb produces bsddb.error: (22, &#8216;Invalid argument&#8217;). Help! How can I restore my data?</a><a class="headerlink" href="#i-tried-to-open-berkeley-db-file-but-bsddb-produces-bsddb-error-22-invalid-argument-help-how-can-i-restore-my-data" title="Permalink to this headline">¶</a></h3>
 
828
<p>Don&#8217;t panic! Your data is probably intact. The most frequent cause for the error
 
829
is that you tried to open an earlier Berkeley DB file with a later version of
 
830
the Berkeley DB library.</p>
 
831
<p>Many Linux systems now have all three versions of Berkeley DB available.  If you
 
832
are migrating from version 1 to a newer version use db_dump185 to dump a plain
 
833
text version of the database.  If you are migrating from version 2 to version 3
 
834
use db2_dump to create a plain text version of the database.  In either case,
 
835
use db_load to create a new native database for the latest version installed on
 
836
your computer.  If you have version 3 of Berkeley DB installed, you should be
 
837
able to use db2_load to create a native version 2 database.</p>
 
838
<p>You should move away from Berkeley DB version 1 files because the hash file code
 
839
contains known bugs that can corrupt your data.</p>
 
840
</div>
 
841
</div>
 
842
<div class="section" id="mathematics-and-numerics">
 
843
<h2><a class="toc-backref" href="#id39">Mathematics and Numerics</a><a class="headerlink" href="#mathematics-and-numerics" title="Permalink to this headline">¶</a></h2>
 
844
<div class="section" id="how-do-i-generate-random-numbers-in-python">
 
845
<h3><a class="toc-backref" href="#id40">How do I generate random numbers in Python?</a><a class="headerlink" href="#how-do-i-generate-random-numbers-in-python" title="Permalink to this headline">¶</a></h3>
 
846
<p>The standard module <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal"><span class="pre">random</span></code></a> implements a random number generator.  Usage
 
847
is simple:</p>
 
848
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
 
849
<span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span>
 
850
</pre></div>
 
851
</div>
 
852
<p>This returns a random floating point number in the range [0, 1).</p>
 
853
<p>There are also many other specialized generators in this module, such as:</p>
 
854
<ul class="simple">
 
855
<li><code class="docutils literal"><span class="pre">randrange(a,</span> <span class="pre">b)</span></code> chooses an integer in the range [a, b).</li>
 
856
<li><code class="docutils literal"><span class="pre">uniform(a,</span> <span class="pre">b)</span></code> chooses a floating point number in the range [a, b).</li>
 
857
<li><code class="docutils literal"><span class="pre">normalvariate(mean,</span> <span class="pre">sdev)</span></code> samples the normal (Gaussian) distribution.</li>
 
858
</ul>
 
859
<p>Some higher-level functions operate on sequences directly, such as:</p>
 
860
<ul class="simple">
 
861
<li><code class="docutils literal"><span class="pre">choice(S)</span></code> chooses random element from a given sequence</li>
 
862
<li><code class="docutils literal"><span class="pre">shuffle(L)</span></code> shuffles a list in-place, i.e. permutes it randomly</li>
 
863
</ul>
 
864
<p>There&#8217;s also a <code class="docutils literal"><span class="pre">Random</span></code> class you can instantiate to create independent
 
865
multiple random number generators.</p>
 
866
</div>
 
867
</div>
 
868
</div>
 
869
 
 
870
 
 
871
          </div>
 
872
        </div>
 
873
      </div>
 
874
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
875
        <div class="sphinxsidebarwrapper">
 
876
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
877
  <ul>
 
878
<li><a class="reference internal" href="#">Library and Extension FAQ</a><ul>
 
879
<li><a class="reference internal" href="#general-library-questions">General Library Questions</a></li>
 
880
<li><a class="reference internal" href="#common-tasks">Common tasks</a></li>
 
881
<li><a class="reference internal" href="#threads">Threads</a></li>
 
882
<li><a class="reference internal" href="#input-and-output">Input and Output</a></li>
 
883
<li><a class="reference internal" href="#network-internet-programming">Network/Internet Programming</a></li>
 
884
<li><a class="reference internal" href="#databases">Databases</a></li>
 
885
<li><a class="reference internal" href="#mathematics-and-numerics">Mathematics and Numerics</a></li>
 
886
</ul>
 
887
</li>
 
888
</ul>
 
889
 
 
890
  <h4>Previous topic</h4>
 
891
  <p class="topless"><a href="design.html"
 
892
                        title="previous chapter">Design and History FAQ</a></p>
 
893
  <h4>Next topic</h4>
 
894
  <p class="topless"><a href="extending.html"
 
895
                        title="next chapter">Extending/Embedding FAQ</a></p>
 
896
<h3>This Page</h3>
 
897
<ul class="this-page-menu">
 
898
  <li><a href="../bugs.html">Report a Bug</a></li>
 
899
  <li><a href="../_sources/faq/library.txt"
 
900
         rel="nofollow">Show Source</a></li>
 
901
</ul>
 
902
 
 
903
<div id="searchbox" style="display: none" role="search">
 
904
  <h3>Quick search</h3>
 
905
    <form class="search" action="../search.html" method="get">
 
906
      <input type="text" name="q" />
 
907
      <input type="submit" value="Go" />
 
908
      <input type="hidden" name="check_keywords" value="yes" />
 
909
      <input type="hidden" name="area" value="default" />
 
910
    </form>
 
911
    <p class="searchtip" style="font-size: 90%">
 
912
    Enter search terms or a module, class or function name.
 
913
    </p>
 
914
</div>
 
915
<script type="text/javascript">$('#searchbox').show(0);</script>
 
916
        </div>
 
917
      </div>
 
918
      <div class="clearer"></div>
 
919
    </div>  
 
920
    <div class="related" role="navigation" aria-label="related navigation">
 
921
      <h3>Navigation</h3>
 
922
      <ul>
 
923
        <li class="right" style="margin-right: 10px">
 
924
          <a href="../genindex.html" title="General Index"
 
925
             >index</a></li>
 
926
        <li class="right" >
 
927
          <a href="../py-modindex.html" title="Python Module Index"
 
928
             >modules</a> |</li>
 
929
        <li class="right" >
 
930
          <a href="extending.html" title="Extending/Embedding FAQ"
 
931
             >next</a> |</li>
 
932
        <li class="right" >
 
933
          <a href="design.html" title="Design and History FAQ"
 
934
             >previous</a> |</li>
 
935
        <li><img src="../_static/py.png" alt=""
 
936
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
937
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
938
        <li>
 
939
          <span class="version_switcher_placeholder">2.7.12</span>
 
940
          <a href="../index.html">Documentation</a> &raquo;
 
941
        </li>
 
942
 
 
943
          <li class="nav-item nav-item-1"><a href="index.html" >Python Frequently Asked Questions</a> &raquo;</li> 
 
944
      </ul>
 
945
    </div>  
 
946
    <div class="footer">
 
947
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
948
    <br />
 
949
    The Python Software Foundation is a non-profit corporation.
 
950
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
951
    <br />
 
952
    Last updated on Sep 20, 2016.
 
953
    <a href="../bugs.html">Found a bug</a>?
 
954
    <br />
 
955
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
956
    </div>
 
957
 
 
958
  </body>
 
959
</html>
 
 
b'\\ No newline at end of file'