Skip to content
Snippets Groups Projects
Commit 097e4f70 authored by Armin Rigo's avatar Armin Rigo
Browse files

Copy some documentation about ``-X track-resources`` here as well

parent db1924be
No related branches found
No related tags found
No related merge requests found
......@@ -116,6 +116,16 @@
<div class="syntax python"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span><br/><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;stuff&quot;</span><span class="p">)</span><br/><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span><br/></pre></div>
<p>or using the <tt class="docutils literal">with</tt> keyword</p>
<div class="syntax python"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span><br/> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;stuff&quot;</span><span class="p">)</span><br/></pre></div>
<p>The same problem&ndash;not closing your files&ndash;can also show up if your
program opens a large number of files without closing them explicitly.
In that case, you can easily hit the system limit on the number of file
descriptors that are allowed to be opened at the same time.</p>
<p>Since release 5.4, PyPy can be run with the command-line option <tt class="docutils literal"><span class="pre">-X</span>
<span class="pre">track-resources</span></tt> (as in, <tt class="docutils literal">pypy <span class="pre">-X</span> <span class="pre">track-resources</span> myprogram.py</tt>).
This produces a ResourceWarning when the GC closes a non-closed file or
socket. The traceback for the place where the file or socket was
allocated is given as well, which aids finding places where <tt class="docutils literal">close()</tt>
is missing.</p>
<p>Similarly, remember that you must <tt class="docutils literal">close()</tt> a non-exhausted
generator in order to have its pending <tt class="docutils literal">finally</tt> or <tt class="docutils literal">with</tt>
clauses executed immediately:</p>
......
......@@ -86,6 +86,18 @@
with open("filename", "w") as f:
f.write("stuff")
The same problem---not closing your files---can also show up if your
program opens a large number of files without closing them explicitly.
In that case, you can easily hit the system limit on the number of file
descriptors that are allowed to be opened at the same time.
Since release 5.4, PyPy can be run with the command-line option ``-X
track-resources`` (as in, ``pypy -X track-resources myprogram.py``).
This produces a ResourceWarning when the GC closes a non-closed file or
socket. The traceback for the place where the file or socket was
allocated is given as well, which aids finding places where ``close()``
is missing.
Similarly, remember that you must ``close()`` a non-exhausted
generator in order to have its pending ``finally`` or ``with``
clauses executed immediately:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment