<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Giampaolo Rodola</title><link href="https://gmpy.dev/" rel="alternate"/><link href="https://gmpy.dev/feeds/atom.all.xml" rel="self"/><id>https://gmpy.dev/</id><updated>2026-01-28T00:00:00+01:00</updated><subtitle>Python enthusiast, core developer, psutil author</subtitle><entry><title>From Python 3.3 to today: ending 15 years of subprocess polling</title><link href="https://gmpy.dev/blog/2026/event-driven-process-waiting" rel="alternate"/><published>2026-01-28T00:00:00+01:00</published><updated>2026-01-28T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2026-01-28:/blog/2026/event-driven-process-waiting</id><summary type="html">&lt;p&gt;One of the less fun aspects of process management on POSIX systems is waiting for a process to terminate. The standard library's &lt;tt class="docutils literal"&gt;subprocess&lt;/tt&gt; module has relied on a busy-loop polling approach since the &lt;em&gt;timeout&lt;/em&gt; parameter was added to &lt;tt class="docutils literal"&gt;subprocess.Popen.wait()&lt;/tt&gt; in Python 3.3, around 15 years ago (see …&lt;/p&gt;</summary><content type="html">&lt;p&gt;One of the less fun aspects of process management on POSIX systems is waiting for a process to terminate. The standard library's &lt;tt class="docutils literal"&gt;subprocess&lt;/tt&gt; module has relied on a busy-loop polling approach since the &lt;em&gt;timeout&lt;/em&gt; parameter was added to &lt;tt class="docutils literal"&gt;subprocess.Popen.wait()&lt;/tt&gt; in Python 3.3, around 15 years ago (see &lt;a class="reference external" href="https://github.com/python/cpython/blob/8def603d853c7f5e4ff57f95de289f99e1943669/Lib/subprocess.py#L2056-L2077"&gt;source&lt;/a&gt;). And psutil's &lt;tt class="docutils literal"&gt;Process.wait()&lt;/tt&gt; method uses exactly the same technique (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/700b7e6a/psutil/_psposix.py#L95-L160"&gt;source&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The logic is straightforward: check whether the process has exited using non-blocking &lt;tt class="docutils literal"&gt;waitpid(WNOHANG)&lt;/tt&gt;, sleep briefly, check again, sleep a bit longer, and so on.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wait_busy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;
    &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0001&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;pid_done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;waitpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WNOHANG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pid_done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.04&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;TimeoutError&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this blog post I'll show how I finally addressed this long-standing inefficiency, first in psutil, and most excitingly, directly in CPython's standard library &lt;tt class="docutils literal"&gt;subprocess&lt;/tt&gt; module.&lt;/p&gt;
&lt;div class="section" id="the-problem-with-busy-polling"&gt;
&lt;h2&gt;The problem with busy-polling&lt;a class="headerlink" href="#the-problem-with-busy-polling" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;CPU wake-ups: even with exponential backoff (starting at 0.1ms, capping at 40ms), the system constantly wakes up to check process status, wasting CPU cycles and draining batteries.&lt;/li&gt;
&lt;li&gt;Latency: there's always a gap between when a process actually terminates and when you detect it.&lt;/li&gt;
&lt;li&gt;Scalability: monitoring many processes simultaneously magnifies all of the above.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="event-driven-waiting"&gt;
&lt;h2&gt;Event-driven waiting&lt;a class="headerlink" href="#event-driven-waiting" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All POSIX systems provide at least one mechanism to be notified when a file descriptor becomes ready. These are &lt;a class="reference external" href="https://man7.org/linux/man-pages/man2/select.2.html"&gt;select()&lt;/a&gt;, &lt;a class="reference external" href="https://man7.org/linux/man-pages/man2/poll.2.html"&gt;poll()&lt;/a&gt;, &lt;a class="reference external" href="https://man7.org/linux/man-pages/man7/epoll.7.html"&gt;epoll()&lt;/a&gt; (Linux) and &lt;a class="reference external" href="https://man.freebsd.org/cgi/man.cgi?query=kqueue"&gt;kqueue()&lt;/a&gt; (BSD / macOS) system calls. Until recently, I believed they could only be used with file descriptors referencing sockets, pipes, etc., but it turns out they can also be used to wait for events on process PIDs!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="linux"&gt;
&lt;h2&gt;Linux&lt;a class="headerlink" href="#linux" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In 2019, Linux 5.3 introduced a new syscall, &lt;tt class="docutils literal"&gt;os.pidfd_open()&lt;/tt&gt;, which was added in Python 3.9. It returns a file descriptor referencing a process PID. The interesting thing is that &lt;tt class="docutils literal"&gt;pidfd_open()&lt;/tt&gt; can be used in conjunction with &lt;tt class="docutils literal"&gt;select()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;epoll()&lt;/tt&gt; to effectively wait until the process exits. E.g. by using &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;select&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wait_pidfd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;pidfd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pidfd_open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;poller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;poller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pidfd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;POLLIN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# block until process exits or timeout occurs&lt;/span&gt;
    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;poller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;TimeoutError&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This approach has zero busy-looping. The kernel wakes us up exactly when the process terminates or when the timeout expires if the PID is still alive.&lt;/p&gt;
&lt;p&gt;I chose &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; over &lt;tt class="docutils literal"&gt;select()&lt;/tt&gt; because &lt;tt class="docutils literal"&gt;select()&lt;/tt&gt; has a historical file descriptor limit (&lt;tt class="docutils literal"&gt;FD_SETSIZE&lt;/tt&gt;), which typically caps it at 1024 file descriptors per-process (reminded me of &lt;a class="reference external" href="https://bugs.python.org/issue1685000"&gt;BPO-1685000&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;I chose &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; over &lt;tt class="docutils literal"&gt;epoll()&lt;/tt&gt; because it does not require creating an additional file descriptor. It also needs only a single syscall, which should make it a bit more efficient when monitoring a single FD rather than many.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="macos-and-bsd"&gt;
&lt;h2&gt;macOS and BSD&lt;a class="headerlink" href="#macos-and-bsd" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;BSD-derived systems (including macOS) provide the &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt; syscall. It's conceptually similar to &lt;tt class="docutils literal"&gt;select()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;epoll()&lt;/tt&gt;, but more powerful (e.g. it can also handle regular files). &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt; can be passed a PID directly, and it will return once the PID disappears or the timeout expires:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;select&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wait_kqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;kq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kqueue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;kev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kevent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KQ_FILTER_PROC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KQ_EV_ADD&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KQ_EV_ONESHOT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;fflags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;KQ_NOTE_EXIT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# block until process exits or timeout occurs&lt;/span&gt;
    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kq&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;control&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;kev&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;TimeoutError&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="windows"&gt;
&lt;h2&gt;Windows&lt;a class="headerlink" href="#windows" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Windows does not busy-loop, both in psutil and &lt;tt class="docutils literal"&gt;subprocess&lt;/tt&gt; module, thanks to &lt;tt class="docutils literal"&gt;WaitForSingleObject&lt;/tt&gt;. This means Windows has effectively had event-driven process waiting from the start. So nothing to do on that front.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="graceful-fallbacks"&gt;
&lt;h2&gt;Graceful fallbacks&lt;a class="headerlink" href="#graceful-fallbacks" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Both &lt;tt class="docutils literal"&gt;pidfd_open()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt; can fail for different reasons. For example, with &lt;tt class="docutils literal"&gt;EMFILE&lt;/tt&gt; if the process runs out of file descriptors (usually 1024), or with &lt;tt class="docutils literal"&gt;EACCES&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;EPERM&lt;/tt&gt; if the syscall was explicitly blocked at the system level by the sysadmin (e.g. via SECCOMP). In all cases, psutil silently falls back to the traditional busy-loop polling approach rather than raising an exception.&lt;/p&gt;
&lt;p&gt;This fast-path-with-fallback approach is similar in spirit to &lt;a class="reference external" href="https://bugs.python.org/issue33671"&gt;BPO-33671&lt;/a&gt;, where I sped up &lt;tt class="docutils literal"&gt;shutil.copyfile()&lt;/tt&gt; by using zero-copy system calls back in 2018. In there, more efficient &lt;tt class="docutils literal"&gt;os.sendfile()&lt;/tt&gt; is attempted first, and if it fails (e.g. on network filesystems) we fall back to the traditional &lt;tt class="docutils literal"&gt;read()&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;write()&lt;/tt&gt; approach to copy regular files.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="measurement"&gt;
&lt;h2&gt;Measurement&lt;a class="headerlink" href="#measurement" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As a simple experiment, here's a simple program which waits on itself for 10 seconds without terminating:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# test.py&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TimeoutExpired&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can measure the CPU context switching using &lt;tt class="docutils literal"&gt;/usr/bin/time &lt;span class="pre"&gt;-v&lt;/span&gt;&lt;/tt&gt;. Before the patch (the busy-loop):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ /usr/bin/time -v python3 test.py 2&amp;gt;&amp;amp;1 | grep context
    Voluntary context switches: 258
    Involuntary context switches: 4
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After the patch (the event-driven approach):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ /usr/bin/time -v python3 test.py 2&amp;gt;&amp;amp;1 | grep context
    Voluntary context switches: 2
    Involuntary context switches: 1
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This shows that instead of spinning in userspace, the process blocks in &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt;, and is woken up only when the kernel notifies it, resulting in just a few CPU context switches.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="sleeping-state"&gt;
&lt;h2&gt;Sleeping state&lt;a class="headerlink" href="#sleeping-state" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It's also interesting to note that waiting via &lt;tt class="docutils literal"&gt;poll()&lt;/tt&gt; (or &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt;) puts the process into the exact same sleeping state as a plain &lt;tt class="docutils literal"&gt;time.sleep&lt;/tt&gt; call. From the kernel's perspective, both are interruptible sleeps: the process is de-scheduled, consumes zero CPU, and sits quietly in kernel space.&lt;/p&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;&amp;quot;S+&amp;quot;&lt;/tt&gt; state shown below by &lt;tt class="docutils literal"&gt;ps&lt;/tt&gt; means that the process &amp;quot;sleeps in foreground&amp;quot;.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;time.sleep&lt;/tt&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ (python3 -c &amp;#39;import time; time.sleep(10)&amp;#39; &amp;amp; pid=$!; sleep 0.3; ps -o pid,stat,comm -p $pid) &amp;amp;&amp;amp; fg &amp;amp;&amp;gt;/dev/null
    PID STAT COMMAND
 491573 S+   python3
&lt;/pre&gt;&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;select.poll&lt;/tt&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ (python3 -c &amp;#39;import os,select; fd = os.pidfd_open(os.getpid(),0); p = select.poll(); p.register(fd,select.POLLIN); p.poll(10_000)&amp;#39; &amp;amp; pid=$!; sleep 0.3; ps -o pid,stat,comm -p $pid) &amp;amp;&amp;amp; fg &amp;amp;&amp;gt;/dev/null
    PID STAT COMMAND
 491748 S+   python3
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="cpython-contribution"&gt;
&lt;h2&gt;CPython contribution&lt;a class="headerlink" href="#cpython-contribution" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After landing the psutil implementation (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2706"&gt;PR-2706&lt;/a&gt;), I took the extra step and submitted a matching pull request for CPython &lt;tt class="docutils literal"&gt;subprocess&lt;/tt&gt; module: &lt;a class="reference external" href="https://github.com/python/cpython/pull/144047"&gt;cpython/PR-144047&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I'm especially proud of this one: this is the &lt;strong&gt;third time&lt;/strong&gt; in psutil's 17+ year history that a feature developed in psutil made its way upstream into the Python standard library.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;The first was back in 2010, when &lt;tt class="docutils literal"&gt;Process.nice()&lt;/tt&gt; inspired &lt;tt class="docutils literal"&gt;os.getpriority()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;os.setpriority()&lt;/tt&gt;, see &lt;a class="reference external" href="https://bugs.python.org/issue10784"&gt;BPO-10784&lt;/a&gt;. Landed in Python 3.3.&lt;/li&gt;
&lt;li&gt;The second was back in 2011, when &lt;tt class="docutils literal"&gt;psutil.disk_usage()&lt;/tt&gt; inspired &lt;tt class="docutils literal"&gt;shutil.disk_usage()&lt;/tt&gt;, see &lt;a class="reference external" href="https://mail.python.org/archives/list/python-ideas&amp;#64;python.org/thread/67A7ML2TJ7MBS3WOL6IZKLD2C3B3VCQG"&gt;python-ideas ML proposal&lt;/a&gt;. Landed in Python 3.3.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Funny thing:&lt;/em&gt; 15 years ago, Python 3.3 added the &lt;em&gt;timeout&lt;/em&gt; parameter to &lt;tt class="docutils literal"&gt;subprocess.Popen.wait&lt;/tt&gt; (see &lt;a class="reference external" href="https://github.com/python/cpython/commit/31aa7dd1419"&gt;commit&lt;/a&gt;). That's probably where I took inspiration when I first added the &lt;em&gt;timeout&lt;/em&gt; parameter to psutil's &lt;tt class="docutils literal"&gt;Process.wait()&lt;/tt&gt; around the same time (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/commit/886710daf"&gt;commit&lt;/a&gt;). Now, 15 years later, I'm contributing back a similar improvement for that very same &lt;em&gt;timeout&lt;/em&gt; parameter. &lt;strong&gt;The circle is complete&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="links"&gt;
&lt;h2&gt;Links&lt;a class="headerlink" href="#links" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Topics related to this:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2712"&gt;#2712&lt;/a&gt;: proposal to extend this to multiple PIDs (&lt;tt class="docutils literal"&gt;psutil.wait_procs()&lt;/tt&gt;).&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2703"&gt;#2703&lt;/a&gt;: proposal for asynchronous &lt;tt class="docutils literal"&gt;Process.wait()&lt;/tt&gt; integration with &lt;tt class="docutils literal"&gt;asyncio&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/python/cpython/issues/144211"&gt;cpython/#144211&lt;/a&gt;: proposal to extend the &lt;tt class="docutils literal"&gt;selectors&lt;/tt&gt; module to enable &lt;tt class="docutils literal"&gt;asyncio&lt;/tt&gt; optimization on BSD / macOS via &lt;tt class="docutils literal"&gt;kqueue()&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/1qpp42k/from_python_33_to_today_ending_15_years_of/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=46802061"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://medium.com/&amp;#64;g.rodola/from-python-3-3-to-today-ending-15-years-of-subprocess-polling-e8a83ba17c86"&gt;Medium&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.linkedin.com/feed/update/urn:li:share:7422396026449391616/"&gt;Linkedin&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="python-core"/><category term="performance"/><category term="featured"/></entry><entry><title>Detecting memory leaks in C extensions with psutil and psleak</title><link href="https://gmpy.dev/blog/2025/psutil-heap-introspection-apis" rel="alternate"/><published>2025-12-23T00:00:00+01:00</published><updated>2025-12-23T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2025-12-23:/blog/2025/psutil-heap-introspection-apis</id><summary type="html">&lt;p&gt;Memory leaks in Python are usually straightforward to diagnose. Just look at RSS, track Python object counts, follow reference graphs, etc. But leaks inside C extension modules are another story. Traditional memory metrics such as RSS and VMS fail to reveal them because Python's memory allocator (&lt;a class="reference external" href="https://docs.python.org/3/c-api/memory.html#the-pymalloc-allocator"&gt;pymalloc&lt;/a&gt;) sits above the …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Memory leaks in Python are usually straightforward to diagnose. Just look at RSS, track Python object counts, follow reference graphs, etc. But leaks inside C extension modules are another story. Traditional memory metrics such as RSS and VMS fail to reveal them because Python's memory allocator (&lt;a class="reference external" href="https://docs.python.org/3/c-api/memory.html#the-pymalloc-allocator"&gt;pymalloc&lt;/a&gt;) sits above the platform's native heap. If something in an extension calls &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; without a corresponding &lt;tt class="docutils literal"&gt;free()&lt;/tt&gt;, that memory often won't show up in RSS / VMS. You have a leak, and you don't know.&lt;/p&gt;
&lt;p&gt;psutil 7.2.0 introduces two new APIs for &lt;strong&gt;C heap introspection&lt;/strong&gt;, designed specifically to catch these kinds of native leaks. They give you a window directly into the underlying platform allocator (e.g. glibc's malloc), letting you track how much memory the C layer actually allocates. If your RSS is flat but your C heap usage climbs, you now have a way to see it.&lt;/p&gt;
&lt;div class="section" id="why-native-heap-introspection-matters"&gt;
&lt;h2&gt;Why native heap introspection matters&lt;a class="headerlink" href="#why-native-heap-introspection-matters" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Many Python projects rely on C extensions: psutil, NumPy, pandas, PIL, lxml, psycopg, PyTorch, custom in-house modules, etc. And even CPython itself, which implements many of its standard library modules in C. If any of these components mishandle memory at the C level, you get a leak that doesn't show up in:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Python reference counts (&lt;tt class="docutils literal"&gt;sys.getrefcount&lt;/tt&gt;).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;tracemalloc&lt;/tt&gt; module.&lt;/li&gt;
&lt;li&gt;Python's &lt;tt class="docutils literal"&gt;gc&lt;/tt&gt; stats.&lt;/li&gt;
&lt;li&gt;RSS, VMS or USS due to allocator caching, especially for small objects. This can happen, for example, when you forget to &lt;tt class="docutils literal"&gt;Py_DECREF&lt;/tt&gt; a Python object.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;psutil's new functions let you query the allocator (e.g. glibc) directly, returning low-level metrics from the platform's native heap.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="heap-info-direct-allocator-statistics"&gt;
&lt;h2&gt;heap_info(): direct allocator statistics&lt;a class="headerlink" href="#heap-info-direct-allocator-statistics" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;psutil.heap_info()&lt;/tt&gt; exposes the following metrics:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;heap_used&lt;/tt&gt;: total number of bytes currently allocated via &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; (small allocations).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;mmap_used&lt;/tt&gt;: total number of bytes currently allocated via &lt;tt class="docutils literal"&gt;mmap()&lt;/tt&gt; or via large &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; allocations.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;heap_count&lt;/tt&gt;: (Windows only) number of private heaps created via &lt;tt class="docutils literal"&gt;HeapCreate()&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_info&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;pheap(heap_used=5177792, mmap_used=819200)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Reference for what contributes to each field:&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="33%" /&gt;
&lt;col width="33%" /&gt;
&lt;col width="33%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Platform&lt;/th&gt;
&lt;th class="head"&gt;Allocation type&lt;/th&gt;
&lt;th class="head"&gt;Field affected&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;UNIX / Windows&lt;/td&gt;
&lt;td&gt;small &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; ≤128 KB without &lt;tt class="docutils literal"&gt;free()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;heap_used&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;UNIX / Windows&lt;/td&gt;
&lt;td&gt;large &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; &amp;gt;128 KB without &lt;tt class="docutils literal"&gt;free()&lt;/tt&gt;, or &lt;tt class="docutils literal"&gt;mmap()&lt;/tt&gt; without &lt;tt class="docutils literal"&gt;munmap()&lt;/tt&gt; (UNIX)&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;mmap_used&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;HeapAlloc()&lt;/tt&gt; without &lt;tt class="docutils literal"&gt;HeapFree()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;heap_used&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;VirtualAlloc()&lt;/tt&gt; without &lt;tt class="docutils literal"&gt;VirtualFree()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;mmap_used&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;HeapCreate()&lt;/tt&gt; without &lt;tt class="docutils literal"&gt;HeapDestroy()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;heap_count&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section" id="heap-trim-returning-unused-heap-memory"&gt;
&lt;h2&gt;heap_trim(): returning unused heap memory&lt;a class="headerlink" href="#heap-trim-returning-unused-heap-memory" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;psutil.heap_trim()&lt;/tt&gt; provides a cross-platform way to request that the underlying allocator free any unused memory it's holding in the heap (typically small &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; allocations).&lt;/p&gt;
&lt;p&gt;In practice, modern allocators rarely comply, so this is not a general-purpose memory-reduction tool and won't meaningfully shrink RSS in real programs. Its primary value is in leak detection tools. Calling &lt;tt class="docutils literal"&gt;psutil.heap_trim()&lt;/tt&gt; before taking measurements helps reduce allocator noise, giving you a cleaner baseline so that changes in &lt;tt class="docutils literal"&gt;heap_used&lt;/tt&gt; come from the code you're testing, not from internal allocator caching or fragmentation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="real-world-use-finding-a-c-extension-leak"&gt;
&lt;h2&gt;Real-world use: finding a C extension leak&lt;a class="headerlink" href="#real-world-use-finding-a-c-extension-leak" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The workflow is simple:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Take a baseline snapshot of the heap.&lt;/li&gt;
&lt;li&gt;Call the C extension hundreds of times.&lt;/li&gt;
&lt;li&gt;Take another snapshot.&lt;/li&gt;
&lt;li&gt;Compare.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;

&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# reduce noise&lt;/span&gt;

&lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_info&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;my_cext_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_info&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;delta heap_used =&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_used&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;heap_used&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;delta mmap_used =&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mmap_used&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mmap_used&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If &lt;tt class="docutils literal"&gt;heap_used&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;mmap_used&lt;/tt&gt; values increase consistently, you've found a native leak.&lt;/p&gt;
&lt;p&gt;To reduce false positives, repeat the test multiple times, increasing the number of calls on each retry. This approach helps distinguish real leaks from random noise or transient allocations.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="a-new-tool-psleak"&gt;
&lt;h2&gt;A new tool: psleak&lt;a class="headerlink" href="#a-new-tool-psleak" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The strategy described above is exactly what I implemented in a new PyPI package, which I called &lt;a class="reference external" href="https://github.com/giampaolo/psleak"&gt;psleak&lt;/a&gt;. It runs the target function repeatedly, trims the allocator before each run, and tracks differences across retries. Memory that grows consistently after several runs is flagged as a leak.&lt;/p&gt;
&lt;p&gt;A minimal test suite looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psleak&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MemoryLeakTestCase&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;TestLeaks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MemoryLeakTestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;some_c_function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the function leaks memory, the test will fail with a descriptive exception:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;psleak.MemoryLeakError: memory kept increasing after 10 runs
Run # 1: heap=+388160  | uss=+356352  | rss=+327680  | (calls= 200, avg/call=+1940)
Run # 2: heap=+584848  | uss=+614400  | rss=+491520  | (calls= 300, avg/call=+1949)
Run # 3: heap=+778320  | uss=+782336  | rss=+819200  | (calls= 400, avg/call=+1945)
Run # 4: heap=+970512  | uss=+1032192 | rss=+1146880 | (calls= 500, avg/call=+1941)
Run # 5: heap=+1169024 | uss=+1171456 | rss=+1146880 | (calls= 600, avg/call=+1948)
Run # 6: heap=+1357360 | uss=+1413120 | rss=+1310720 | (calls= 700, avg/call=+1939)
Run # 7: heap=+1552336 | uss=+1634304 | rss=+1638400 | (calls= 800, avg/call=+1940)
Run # 8: heap=+1752032 | uss=+1781760 | rss=+1802240 | (calls= 900, avg/call=+1946)
Run # 9: heap=+1945056 | uss=+2031616 | rss=+2129920 | (calls=1000, avg/call=+1945)
Run #10: heap=+2140624 | uss=+2179072 | rss=+2293760 | (calls=1100, avg/call=+1946)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Psleak is now part of the psutil test suite. All psutil APIs are tested (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/1a946cfe738045cecf031222cd5078da21946af4/tests/test_memleaks.py"&gt;test_memleaks.py&lt;/a&gt;), making it a de facto &lt;strong&gt;regression-testing tool&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;It's worth noting that without inspecting heap metrics, missing calls in the C code such as &lt;tt class="docutils literal"&gt;Py_CLEAR&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Py_DECREF&lt;/tt&gt; often go unnoticed, because they don't affect RSS, VMS, and USS. I confirmed this by commenting them out. Monitoring the heap is therefore essential to reliably detect memory leaks in Python C extensions.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="under-the-hood"&gt;
&lt;h2&gt;Under the hood&lt;a class="headerlink" href="#under-the-hood" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For those interested in seeing how I did this in terms of code:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/d40164f1/psutil/arch/linux/heap.c"&gt;Linux&lt;/a&gt;: uses glibc's &lt;a class="reference external" href="https://man7.org/linux/man-pages/man3/mallinfo.3.html"&gt;mallinfo2()&lt;/a&gt; to report &lt;tt class="docutils literal"&gt;uordblks&lt;/tt&gt; (heap allocations) and &lt;tt class="docutils literal"&gt;hblkhd&lt;/tt&gt; (mmap-backed blocks).&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/d40164f1/psutil/arch/windows/heap.c"&gt;Windows&lt;/a&gt;: enumerates heaps and aggregates &lt;tt class="docutils literal"&gt;HeapAlloc&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;VirtualAlloc&lt;/tt&gt; usage.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/d40164f1/psutil/arch/osx/heap.c"&gt;macOS&lt;/a&gt;: uses malloc zone statistics.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/d40164f1/psutil/arch/bsd/heap.c"&gt;BSD&lt;/a&gt;: uses jemalloc's arena and stats interfaces.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="references"&gt;
&lt;h2&gt;References&lt;a class="headerlink" href="#references" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psleak"&gt;psleak&lt;/a&gt;, the new memory leak testing framework.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2692"&gt;PR-2692&lt;/a&gt;, the implementation.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1275"&gt;#1275&lt;/a&gt;, the original proposal from 8 years earlier.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/1puqgfg/detect_memory_leaks_of_c_extensions_with_psutil/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=46376608"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://medium.com/&amp;#64;g.rodola/detect-memory-leaks-of-c-extensions-with-psutil-and-psleak-a0521ba6315f"&gt;Medium&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="memory"/><category term="c"/><category term="new-api"/><category term="featured"/></entry><entry><title>Wheels for free-threaded Python now available</title><link href="https://gmpy.dev/blog/2025/wheels-for-free-threaded-python-now-available-in-psutil" rel="alternate"/><published>2025-10-25T00:00:00+02:00</published><updated>2025-10-25T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2025-10-25:/blog/2025/wheels-for-free-threaded-python-now-available-in-psutil</id><summary type="html">&lt;p&gt;With the release of psutil 7.1.2, wheels for free-threaded Python are now available. This milestone was achieved largely through a community effort, as several internal refactorings to the C code were required to make it possible (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2565"&gt;#2565&lt;/a&gt;). Many of these changes were contributed by &lt;a class="reference external" href="https://github.com/lysnikolaou"&gt;Lysandros Nikolaou&lt;/a&gt;. Thanks …&lt;/p&gt;</summary><content type="html">&lt;p&gt;With the release of psutil 7.1.2, wheels for free-threaded Python are now available. This milestone was achieved largely through a community effort, as several internal refactorings to the C code were required to make it possible (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2565"&gt;#2565&lt;/a&gt;). Many of these changes were contributed by &lt;a class="reference external" href="https://github.com/lysnikolaou"&gt;Lysandros Nikolaou&lt;/a&gt;. Thanks to him for the effort and for bearing with me in code reviews! ;-)&lt;/p&gt;
&lt;div class="section" id="what-is-free-threaded-python"&gt;
&lt;h2&gt;What is free-threaded Python?&lt;a class="headerlink" href="#what-is-free-threaded-python" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Free-threaded Python (available since Python 3.13) refers to Python builds that are compiled with the &lt;strong&gt;GIL (Global Interpreter Lock) disabled&lt;/strong&gt;, allowing true parallel execution of Python bytecodes across multiple threads. This is particularly beneficial for CPU-bound applications, as it enables better utilization of multi-core processors.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-state-of-free-threaded-wheels"&gt;
&lt;h2&gt;The state of free-threaded wheels&lt;a class="headerlink" href="#the-state-of-free-threaded-wheels" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;According to Hugo van Kemenade's &lt;a class="reference external" href="https://hugovk.github.io/free-threaded-wheels/"&gt;free-threaded wheels tracker&lt;/a&gt;, the adoption of free-threaded wheels among the top 360 most-downloaded PyPI packages with C extensions is still limited. Only 128 out of these 360 packages provide wheels compiled for free-threaded Python, meaning they can run on Python builds with the GIL disabled. This shows that, while progress has been made, most popular packages with C extensions still do not offer ready-made wheels for free-threaded Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-it-means-for-users"&gt;
&lt;h2&gt;What it means for users&lt;a class="headerlink" href="#what-it-means-for-users" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When a library author provides a wheel, users can install a pre-compiled binary package without having to build it from source. This is especially important for packages with C extensions, like psutil, which is largely written in C. Such packages often have complex build requirements and require installing a C compiler. On Windows, that means installing Visual Studio or the Build Tools, which can take several gigabytes and a &lt;em&gt;significant&lt;/em&gt; setup effort. Providing wheels spares users from this hassle, makes installation far simpler, and is effectively essential for the users of that package. You basically &lt;tt class="docutils literal"&gt;pip install psutil&lt;/tt&gt; and you're done.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-it-means-for-library-authors"&gt;
&lt;h2&gt;What it means for library authors&lt;a class="headerlink" href="#what-it-means-for-library-authors" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Currently, &lt;strong&gt;universal wheels for free-threaded Python do not exist&lt;/strong&gt;. Each wheel must be built specifically for a Python version. Right now authors must create separate wheels for Python 3.13 and 3.14. Which means distributing &lt;em&gt;a lot&lt;/em&gt; of files already:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;psutil-7.1.2-cp313-cp313t-macosx_10_13_x86_64.whl
psutil-7.1.2-cp313-cp313t-macosx_11_0_arm64.whl
psutil-7.1.2-cp313-cp313t-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl
psutil-7.1.2-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
psutil-7.1.2-cp313-cp313t-win_amd64.whl
psutil-7.1.2-cp313-cp313t-win_arm64.whl
psutil-7.1.2-cp314-cp314t-macosx_10_15_x86_64.whl
psutil-7.1.2-cp314-cp314t-macosx_11_0_arm64.whl
psutil-7.1.2-cp314-cp314t-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl
psutil-7.1.2-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
psutil-7.1.2-cp314-cp314t-win_amd64.whl
psutil-7.1.2-cp314-cp314t-win_arm64.whl
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This also multiplies CI jobs and slows down the test matrix (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/7dfd0ed34fe70ffd879ae62d21aabd4a8ed06d6f/.github/workflows/build.yml"&gt;build.yml&lt;/a&gt;). A true universal wheel would greatly reduce this overhead, allowing a single wheel to support multiple Python versions and platforms. Hopefully, Python 3.15 will simplify this process. Two competing proposals, &lt;a class="reference external" href="https://www.python.org/dev/peps/pep-0803/"&gt;PEP 803&lt;/a&gt; and &lt;a class="reference external" href="https://www.python.org/dev/peps/pep-0809/"&gt;PEP 809&lt;/a&gt;, aim to standardize wheel naming and metadata to allow producing a single wheel that covers multiple Python versions. That would drastically reduce distribution complexity for library authors, and it's fair to say it's essential for free-threaded CPython to truly succeed.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="how-to-install-free-threaded-psutil"&gt;
&lt;h2&gt;How to install free-threaded psutil&lt;a class="headerlink" href="#how-to-install-free-threaded-psutil" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can now install psutil for free-threaded Python directly via &lt;tt class="docutils literal"&gt;pip&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;pip&lt;span class="w"&gt; &lt;/span&gt;install&lt;span class="w"&gt; &lt;/span&gt;psutil&lt;span class="w"&gt; &lt;/span&gt;--only-binary&lt;span class="o"&gt;=&lt;/span&gt;:all:
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This ensures you get the pre-compiled wheels without triggering a source build.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/1ofrx8v/comment/nllztz6/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="wheels"/><category term="community"/><category term="python-core"/></entry><entry><title>Speeding up pytest startup</title><link href="https://gmpy.dev/blog/2025/speedup-pytest-startup" rel="alternate"/><published>2025-04-04T00:00:00+02:00</published><updated>2025-04-04T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2025-04-04:/blog/2025/speedup-pytest-startup</id><summary type="html">&lt;div class="section" id="preface-the-migration-to-pytest"&gt;
&lt;h2&gt;Preface: the migration to pytest&lt;a class="headerlink" href="#preface-the-migration-to-pytest" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Last year, after 17 years of &lt;tt class="docutils literal"&gt;unittest&lt;/tt&gt;, I started adopting pytest in psutil (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2446"&gt;#2446&lt;/a&gt;). The two advantages I cared about most were plain &lt;tt class="docutils literal"&gt;assert&lt;/tt&gt; statements and &lt;a class="reference external" href="https://pypi.org/project/pytest-xdist/"&gt;pytest-xdist&lt;/a&gt;'s free parallelism. Tests still inherit from &lt;tt class="docutils literal"&gt;unittest.TestCase&lt;/tt&gt; and there's no &lt;tt class="docutils literal"&gt;conftest.py&lt;/tt&gt; or fixture use …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;div class="section" id="preface-the-migration-to-pytest"&gt;
&lt;h2&gt;Preface: the migration to pytest&lt;a class="headerlink" href="#preface-the-migration-to-pytest" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Last year, after 17 years of &lt;tt class="docutils literal"&gt;unittest&lt;/tt&gt;, I started adopting pytest in psutil (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2446"&gt;#2446&lt;/a&gt;). The two advantages I cared about most were plain &lt;tt class="docutils literal"&gt;assert&lt;/tt&gt; statements and &lt;a class="reference external" href="https://pypi.org/project/pytest-xdist/"&gt;pytest-xdist&lt;/a&gt;'s free parallelism. Tests still inherit from &lt;tt class="docutils literal"&gt;unittest.TestCase&lt;/tt&gt; and there's no &lt;tt class="docutils literal"&gt;conftest.py&lt;/tt&gt; or fixture use (rationale in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2456"&gt;PR-2456&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;What I want to focus on here is one of pytest's most frustrating aspects: slow startup.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="pytest-invocation-is-slow"&gt;
&lt;h2&gt;pytest invocation is slow&lt;a class="headerlink" href="#pytest-invocation-is-slow" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To measure pytest's startup time, let's run a very &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/265fcf94a5da4260beb514653a2124915bf2a4f2/psutil/tests/test_misc.py#L232-L236"&gt;simple test&lt;/a&gt; where execution time itself is negligible:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time python3 -m pytest psutil/tests/test_misc.py::TestMisc::test_version
1 passed in 0.05s
real    0m0,427s
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Almost half a second, which is excessive for something I run repeatedly during development. For comparison, the same test under &lt;tt class="docutils literal"&gt;unittest&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time python3 -m unittest psutil.tests.test_misc.TestMisc.test_version
Ran 1 test in 0.000s
real    0m0,204s
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Roughly twice as fast. Why?&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="where-is-time-being-spent"&gt;
&lt;h2&gt;Where is time being spent?&lt;a class="headerlink" href="#where-is-time-being-spent" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A significant portion of pytest's overhead comes from import time, and there's not much one can do about it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time python3 -c &amp;quot;import pytest&amp;quot;
real    0m0,151s

$ time python3 -c &amp;quot;import unittest&amp;quot;
real    0m0,065s

$ time python3 -c &amp;quot;import psutil&amp;quot;
real    0m0,056s
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="disable-plugin-auto-loading"&gt;
&lt;h2&gt;Disable plugin auto loading&lt;a class="headerlink" href="#disable-plugin-auto-loading" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After some research, I discovered that pytest automatically loads all plugins installed on the system, even if they aren't used. Here's how to list them (output is cut):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ pytest --trace-config --collect-only
...
active plugins:
    ...
    setupplan           : ~/.local/lib/python3.12/site-packages/_pytest/setupplan.py
    stepwise            : ~/.local/lib/python3.12/site-packages/_pytest/stepwise.py
    warnings            : ~/.local/lib/python3.12/site-packages/_pytest/warnings.py
    logging             : ~/.local/lib/python3.12/site-packages/_pytest/logging.py
    reports             : ~/.local/lib/python3.12/site-packages/_pytest/reports.py
    python_path         : ~/.local/lib/python3.12/site-packages/_pytest/python_path.py
    unraisableexception : ~/.local/lib/python3.12/site-packages/_pytest/unraisableexception.py
    threadexception     : ~/.local/lib/python3.12/site-packages/_pytest/threadexception.py
    faulthandler        : ~/.local/lib/python3.12/site-packages/_pytest/faulthandler.py
    instafail           : ~/.local/lib/python3.12/site-packages/pytest_instafail.py
    anyio               : ~/.local/lib/python3.12/site-packages/anyio/pytest_plugin.py
    pytest_cov          : ~/.local/lib/python3.12/site-packages/pytest_cov/plugin.py
    subtests            : ~/.local/lib/python3.12/site-packages/pytest_subtests/plugin.py
    xdist               : ~/.local/lib/python3.12/site-packages/xdist/plugin.py
    xdist.looponfail    : ~/.local/lib/python3.12/site-packages/xdist/looponfail.py
    ...
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It turns out &lt;tt class="docutils literal"&gt;PYTEST_DISABLE_PLUGIN_AUTOLOAD&lt;/tt&gt; environment variable can be used to disable them. By running &lt;tt class="docutils literal"&gt;PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 pytest &lt;span class="pre"&gt;--trace-config&lt;/span&gt; &lt;span class="pre"&gt;--collect-only&lt;/span&gt;&lt;/tt&gt; again I can see that the following plugins disappeared:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;anyio
pytest_cov
pytest_instafail
pytest_subtests
xdist
xdist.looponfail
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now let's run the test again with &lt;tt class="docutils literal"&gt;PYTEST_DISABLE_PLUGIN_AUTOLOAD&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python3 -m pytest psutil/tests/test_misc.py::TestMisc::test_version
1 passed in 0.05s
real    0m0,285s
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We went from 0.427s to 0.285s, a ~40% improvement. Not bad. We now need to selectively enable only the plugins we actually use, via &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-p&lt;/span&gt;&lt;/tt&gt;. psutil uses &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-instafail&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-subtests&lt;/span&gt;&lt;/tt&gt; (we'll deal with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-xdist&lt;/span&gt;&lt;/tt&gt; later):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python3 -m pytest -p instafail -p subtests ...
real    0m0,320s
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Time went back up to 0.320s. Quite a slowdown, but still better than the original 0.427s. Adding &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-xdist&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ time PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python3 -m pytest -p instafail -p subtests -p xdist ...
real    0m0,369s
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;0.369s. Not much, but still a pity to pay the price when NOT running tests in parallel.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="handling-pytest-xdist"&gt;
&lt;h2&gt;Handling pytest-xdist&lt;a class="headerlink" href="#handling-pytest-xdist" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If we disable &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-xdist&lt;/span&gt;&lt;/tt&gt; psutil tests still run, but we get a warning:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;psutil/tests/test_testutils.py:367
  ~/svn/psutil/psutil/tests/test_testutils.py:367: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group(name=&amp;quot;serial&amp;quot;)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This warning appears for methods that are intended to run serially, those decorated with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;#64;pytest.mark.xdist_group(name=&amp;quot;serial&amp;quot;)&lt;/span&gt;&lt;/tt&gt;. However, since &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pytest-xdist&lt;/span&gt;&lt;/tt&gt; is now disabled, the decorator no longer exists. To address this, I implemented the following solution in &lt;tt class="docutils literal"&gt;psutil/tests/__init__.py&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pytest&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;functools&lt;/span&gt;

&lt;span class="n"&gt;PYTEST_PARALLEL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;PYTEST_XDIST_WORKER&amp;quot;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;  &lt;span class="c1"&gt;# True if running parallel tests&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;PYTEST_PARALLEL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;fake_xdist_group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;_kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Mimics `@pytest.mark.xdist_group` decorator. No-op: it just&lt;/span&gt;
&lt;span class="sd"&gt;        calls the test method or return the decorated class.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="nd"&gt;@functools&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;inner&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;

    &lt;span class="n"&gt;pytest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mark&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xdist_group&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fake_xdist_group&lt;/span&gt;  &lt;span class="c1"&gt;# monkey patch&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this in place the warning disappears when running tests serially. To run tests in parallel, we'll manually enable &lt;tt class="docutils literal"&gt;xdist&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ python3 -m pytest -p xdist -n auto --dist loadgroup
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="optimizing-test-collection-time"&gt;
&lt;h2&gt;Optimizing test collection time&lt;a class="headerlink" href="#optimizing-test-collection-time" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;By default, pytest searches the entire directory for tests, adding unnecessary overhead. In &lt;tt class="docutils literal"&gt;pyproject.toml&lt;/tt&gt; you can tell pytest where test files are located, and only to consider &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;test_*.py&lt;/span&gt;&lt;/tt&gt; files:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;[tool.pytest.ini_options]&lt;/span&gt;
&lt;span class="n"&gt;testpaths&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;psutil/tests/&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;python_files&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;test_*.py&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Collection time dropped from 0.20s to 0.17s, another ~0.03s shaved off.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="putting-it-all-together"&gt;
&lt;h2&gt;Putting it all together&lt;a class="headerlink" href="#putting-it-all-together" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;With these small optimizations, I managed to reduce &lt;tt class="docutils literal"&gt;pytest&lt;/tt&gt; startup time by ~0.12 seconds, bringing it down from 0.42 seconds. While this improvement is insignificant for full test runs, it makes a noticeable difference (~28% faster) when repeatedly running individual tests from the command line, which is something I do frequently during development. Final result is visible in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2538"&gt;PR-2538&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-links-which-may-be-useful"&gt;
&lt;h2&gt;Other links which may be useful&lt;a class="headerlink" href="#other-links-which-may-be-useful" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/zupo/awesome-pytest-speedup"&gt;https://github.com/zupo/awesome-pytest-speedup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://projects.gentoo.org/python/guide/pytest.html"&gt;https://projects.gentoo.org/python/guide/pytest.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="tests"/><category term="performance"/></entry><entry><title>A Brave / Chrome extension to reorder new tabs</title><link href="https://gmpy.dev/blog/2025/a-brave-chrome-extension-to-reorder-new-tabs" rel="alternate"/><published>2025-04-02T00:00:00+02:00</published><updated>2025-04-02T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2025-04-02:/blog/2025/a-brave-chrome-extension-to-reorder-new-tabs</id><summary type="html">&lt;p&gt;While browsing, I almost always keep three tabs open: Gmail, Slack, and
Microsoft Teams (for work). I find it convenient to have them always in the
same position (1, 2, and 3), so I can quickly switch to them using keyboard
shortcuts (&lt;code&gt;alt+1&lt;/code&gt;, &lt;code&gt;alt+2&lt;/code&gt;, &lt;code&gt;alt+3&lt;/code&gt;). While the …&lt;/p&gt;</summary><content type="html">&lt;p&gt;While browsing, I almost always keep three tabs open: Gmail, Slack, and
Microsoft Teams (for work). I find it convenient to have them always in the
same position (1, 2, and 3), so I can quickly switch to them using keyboard
shortcuts (&lt;code&gt;alt+1&lt;/code&gt;, &lt;code&gt;alt+2&lt;/code&gt;, &lt;code&gt;alt+3&lt;/code&gt;). While the Gmail tab remains in place,
Slack and Teams frequently move because I use them for work, closing them in
the evening and reopening them the next morning. And each time I have to
manually reposition them. To automate this process, I started looking for a
Brave (Chrome) extension but couldn't find one that fully met my needs. So, I
decided to spend a little time writing my own. It turns out it's incredibly
easy. Here's how you can create a simple Brave extension. The same process
should also work for Chrome.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Put these 2 files in a folder:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;manifest.json&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;manifest_version&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Tab repositioner&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;version&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;description&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Set position of new tabs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;permissions&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;tabs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;background&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;service_worker&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;background.js&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;background.js&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;move_tab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`moving tab &amp;#39;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;&amp;#39; to position &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;move_new_tabs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;changeInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;loading&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;changeInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;complete&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;mail.google.com&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;move_tab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;app.slack.com&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;move_tab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;teams.microsoft.com&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;move_tab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onUpdated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addListener&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;tabId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;changeInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nx"&gt;move_new_tabs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;code&gt;brave://extensions/&lt;/code&gt; (&lt;code&gt;chrome://extensions/&lt;/code&gt; for Chrome).&lt;/li&gt;
&lt;li&gt;Enable &lt;code&gt;Developer Mode&lt;/code&gt; (top right).&lt;/li&gt;
&lt;li&gt;Click &lt;code&gt;Load unpacked&lt;/code&gt; and select your folder.&lt;/li&gt;
&lt;li&gt;To observe debug messages printed in the console, click on &lt;code&gt;Service Worker&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After editing &lt;code&gt;background.js&lt;/code&gt;, click the refresh icon to apply the changes.&lt;/li&gt;
&lt;/ul&gt;</content><category term="js"/><category term="brave"/><category term="dotfiles"/></entry><entry><title>Letting go of Python 2.7</title><link href="https://gmpy.dev/blog/2025/psutil-drop-python-27-support" rel="alternate"/><published>2025-02-13T00:00:00+01:00</published><updated>2025-02-13T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2025-02-13:/blog/2025/psutil-drop-python-27-support</id><summary type="html">&lt;p&gt;About dropping Python 2.7 support in psutil, 3 years ago I stated (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2014"&gt;#2014&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
Not a chance, for many years to come. [Python 2.7] currently
represents 7-10% of total downloads, meaning around 70k / 100k
downloads per day.&lt;/blockquote&gt;
&lt;p&gt;Only 3 years later, and to my surprise, &lt;strong&gt;downloads for Python 2 …&lt;/strong&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;About dropping Python 2.7 support in psutil, 3 years ago I stated (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/2014"&gt;#2014&lt;/a&gt;):&lt;/p&gt;
&lt;blockquote&gt;
Not a chance, for many years to come. [Python 2.7] currently
represents 7-10% of total downloads, meaning around 70k / 100k
downloads per day.&lt;/blockquote&gt;
&lt;p&gt;Only 3 years later, and to my surprise, &lt;strong&gt;downloads for Python 2.7 dropped to 0.36%&lt;/strong&gt;! As such, as of psutil 7.0.0, I finally decided to drop support for Python 2.7!&lt;/p&gt;
&lt;div class="section" id="the-numbers"&gt;
&lt;h2&gt;The numbers&lt;a class="headerlink" href="#the-numbers" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;These are downloads per month:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ pypinfo --percent psutil pyversion
Served from cache: False
Data processed: 4.65 GiB
Data billed: 4.65 GiB
Estimated cost: $0.03

| python_version | percent | download_count |
| -------------- | ------- | -------------- |
| 3.10           |  23.84% |     26,354,506 |
| 3.8            |  18.87% |     20,862,015 |
| 3.7            |  17.38% |     19,217,960 |
| 3.9            |  17.00% |     18,798,843 |
| 3.11           |  13.63% |     15,066,706 |
| 3.12           |   7.01% |      7,754,751 |
| 3.13           |   1.15% |      1,267,008 |
| 3.6            |   0.73% |        803,189 |
| 2.7            |   0.36% |        402,111 |
| 3.5            |   0.03% |         28,656 |
| Total          |         |    110,555,745 |
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;According to &lt;a class="reference external" href="https://archive.is/wip/knzql"&gt;pypistats.org&lt;/a&gt; Python 2.7 downloads represent 0.28% of the total, around 15,000 downloads per day.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-pain"&gt;
&lt;h2&gt;The pain&lt;a class="headerlink" href="#the-pain" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Keeping 2.7 alive had become increasingly difficult, but still possible: tests ran via &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fbb6d9ce98f930d3d101b7df5a4f4d0f1d2b35a3/setup.py#L76-L85"&gt;old PyPI backports&lt;/a&gt; and a &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fbb6d9ce98f930d3d101b7df5a4f4d0f1d2b35a3/.github/workflows/build.yml#L77-L112"&gt;tweaked GitHub Actions workflow&lt;/a&gt; on Linux and macOS, plus a separate third-party service (Appveyor) for Windows. But the workarounds in the source kept piling up:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;A Python compatibility layer (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fbb6d9ce98f930d3d101b7df5a4f4d0f1d2b35a3/psutil/_compat.py"&gt;psutil/_compat.py&lt;/a&gt;) plus &lt;tt class="docutils literal"&gt;#if PY_MAJOR_VERSION &amp;lt;= 3&lt;/tt&gt; branches in C, with constant str-vs-unicode juggling on both sides.&lt;/li&gt;
&lt;li&gt;No f-strings, and no free use of &lt;tt class="docutils literal"&gt;enum&lt;/tt&gt; for constants (which ended up with a different API shape than on Python 3).&lt;/li&gt;
&lt;li&gt;An outdated &lt;tt class="docutils literal"&gt;pip&lt;/tt&gt; and &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fbb6d9ce98f930d3d101b7df5a4f4d0f1d2b35a3/setup.py#L76-L85"&gt;outdated deps&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;4 extra CI jobs per commit (Linux, macOS, Windows 32-bit and 64-bit), making the pipeline slower and flakier.&lt;/li&gt;
&lt;li&gt;7 wheels specific to Python 2.7 to ship on every release:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;psutil-6.1.1-cp27-cp27m-macosx_10_9_x86_64.whl
psutil-6.1.1-cp27-none-win32.whl
psutil-6.1.1-cp27-none-win_amd64.whl
psutil-6.1.1-cp27-cp27m-manylinux2010_i686.whl
psutil-6.1.1-cp27-cp27m-manylinux2010_x86_64.whl
psutil-6.1.1-cp27-cp27mu-manylinux2010_i686.whl
psutil-6.1.1-cp27-cp27mu-manylinux2010_x86_64.whl
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="the-removal"&gt;
&lt;h2&gt;The removal&lt;a class="headerlink" href="#the-removal" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The removal was done in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2481"&gt;PR-2481&lt;/a&gt;, which dropped around 1500 lines of code (nice!). &lt;strong&gt;It felt liberating&lt;/strong&gt;. In doing so, in the doc I still made the promise that the 6.1.* series will keep supporting Python 2.7 and will receive &lt;strong&gt;critical bug-fixes only&lt;/strong&gt; (no new features). It will be maintained in a specific &lt;a class="reference external" href="https://github.com/giampaolo/psutil/tree/python2"&gt;python2 branch&lt;/a&gt;. I explicitly kept the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fbb6d9ce98f930d3d101b7df5a4f4d0f1d2b35a3/setup.py"&gt;setup.py&lt;/a&gt; script compatible with Python 2.7 in terms of syntax, so that, when the tarball is fetched from PyPI, it will emit an informative error message on &lt;tt class="docutils literal"&gt;pip install psutil&lt;/tt&gt;. The user trying to install psutil on Python 2.7 will see:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ pip2 install psutil
As of version 7.0.0 psutil no longer supports Python 2.7.
Latest version supporting Python 2.7 is psutil 6.1.X.
Install it with: &amp;quot;pip2 install psutil==6.1.*&amp;quot;.
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="related"&gt;
&lt;h2&gt;Related&lt;a class="headerlink" href="#related" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;2017-06: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1053"&gt;#1053&lt;/a&gt; (2.7 ticket)&lt;/li&gt;
&lt;li&gt;2022-04: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2099"&gt;PR-2099&lt;/a&gt; (drop 2.6)&lt;/li&gt;
&lt;li&gt;2023-04: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2246"&gt;PR-2246&lt;/a&gt; (drop 3.4 &amp;amp; 3.5)&lt;/li&gt;
&lt;li&gt;2024-12: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/2481"&gt;PR-2481&lt;/a&gt; (drop 2.7)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="compatibility"/><category term="release"/><category term="featured"/></entry><entry><title>Recognize connection errors</title><link href="https://gmpy.dev/blog/2023/recognize-connection-errors" rel="alternate"/><published>2023-03-16T00:00:00+01:00</published><updated>2023-03-16T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2023-03-16:/blog/2023/recognize-connection-errors</id><summary type="html">&lt;p&gt;Lately I've been dealing with an asynchronous TCP client app which sends
messages to a remote server. Some of these messages are important, and cannot
get lost. Because the connection may drop at any time, I had to implement a
mechanism to resend the message once the client reconnects. As …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Lately I've been dealing with an asynchronous TCP client app which sends
messages to a remote server. Some of these messages are important, and cannot
get lost. Because the connection may drop at any time, I had to implement a
mechanism to resend the message once the client reconnects. As such, I needed
a way to identify what constitutes a &lt;strong&gt;connection error&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Python provides a builtin &lt;a class="reference external" href="https://docs.python.org/3/library/exceptions.html#ConnectionError"&gt;ConnectionError&lt;/a&gt; exception precisely for this
purpose, but it turns out it's not enough. After observing logs in production,
I found some errors that were not related to the socket connection per se, but
rather to the &lt;strong&gt;system connectivity&lt;/strong&gt;, like &lt;cite&gt;ENETUNREACH&lt;/cite&gt;
(&amp;quot;network unreachable&amp;quot;) or &lt;cite&gt;ENETDOWN&lt;/cite&gt; (&amp;quot;network down&amp;quot;). It's interesting to
note how this distinction is reflected in the UNIX &lt;a class="reference external" href="https://www.thegeekstuff.com/2010/10/linux-error-codes/"&gt;errno&lt;/a&gt; code prefixes:
&lt;cite&gt;ECONN*&lt;/cite&gt; (connection errors) vs. &lt;cite&gt;ENET*&lt;/cite&gt; (network errors). I've noticed
&lt;cite&gt;ENET*&lt;/cite&gt; errors usually occur on a DHCP renewal, or more in general when the
Wi-Fi signal is weak or absent. Because this code runs on a cleaning robot
which constantly moves around the house, connection can become unstable when
the robot gets far from the Wi-Fi Access Point, so it's pretty common to bump
into errors like these:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;File &amp;quot;/usr/lib/python3.7/ssl.py&amp;quot;, line 934, in send
    return self._sslobj.write(data)
OSError: [Errno 101] Network is unreachable

File &amp;quot;/usr/lib/python3.7/socket.py&amp;quot;, line 222, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

File &amp;quot;/usr/lib/python3.7/ssl.py&amp;quot;, line 934, in send
    return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe

File &amp;quot;/usr/lib/python3.7/ssl.py&amp;quot;, line 934, in send
    return self._sslobj.write(data)
socket.timeout: The write operation timed out
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Production logs also revealed a considerable amount of SSL-related errors. I
was uncertain what to do about those. The app is supposed to gracefully handle
them, so theoretically they should represent a bug. Still, they are
unequivocally related to the connection stream, and represent a failed attempt
to send data, so we want to retry it. Examples of logs I found:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;File &amp;quot;/usr/lib/python3.7/ssl.py&amp;quot;, line 934, in send
    return self._sslobj.write(data)
ssl.SSLZeroReturnError: TLS/SSL connection has been closed (EOF)

File &amp;quot;/usr/lib/python3.7/ssl.py&amp;quot;, line 934, in send
    return self._sslobj.write(data)
ssl.SSLError: [SSL: BAD_LENGTH] bad length
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Looking at production logs revealed what sort of brutal, rough and tumble place
the Internet is, and how a network app must be ready to handle all sorts of
unexpected error conditions which hardly show up during testing. To handle all
of these cases I came up with this solution which I think is worth sharing, as
it's generic enough to be reused in similar situations. If needed, this can
easily be extended to include specific exceptions of third party libraries, like
&lt;cite&gt;requests.exceptions.ConnectionError&lt;/cite&gt; (&lt;em&gt;EDIT: 2026-02: done for requests + botocore&lt;/em&gt;).&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="sd"&gt;Recognize a connection error from an exception object.&lt;/span&gt;
&lt;span class="sd"&gt;Author: Giampaolo Rodola&lt;/span&gt;
&lt;span class="sd"&gt;License: MIT&lt;/span&gt;
&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;ssl&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;botocore.exceptions&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;requests.exceptions&lt;/span&gt;

&lt;span class="c1"&gt;# Network errors, usually related to DHCP or wpa_supplicant (Wi-Fi).&lt;/span&gt;
&lt;span class="n"&gt;NETWORK_ERRNOS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;frozenset&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENETUNREACH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Network is unreachable&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENETDOWN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Network is down&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENETRESET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Network dropped connection on reset&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENONET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Machine is not on the network&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENOTCONN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Transport endpoint is not connected&amp;quot;&lt;/span&gt;
    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EBADF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;quot;Bad file descriptor&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# requests lib connection errors&lt;/span&gt;
&lt;span class="n"&gt;REQUESTS_EXCEPTIONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProxyError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSLError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectTimeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadTimeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ChunkedEncodingError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# botocore lib connection errors&lt;/span&gt;
&lt;span class="n"&gt;BOTOCORE_EXCEPTIONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionClosedError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectTimeoutError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EndpointConnectionError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadTimeoutError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSLError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Return True if an exception is connection-related.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ne"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# https://docs.python.org/3/library/exceptions.html#ConnectionError&lt;/span&gt;
        &lt;span class="c1"&gt;# ConnectionError includes:&lt;/span&gt;
        &lt;span class="c1"&gt;# * BrokenPipeError (EPIPE, ESHUTDOWN)&lt;/span&gt;
        &lt;span class="c1"&gt;# * ConnectionAbortedError (ECONNABORTED)&lt;/span&gt;
        &lt;span class="c1"&gt;# * ConnectionRefusedError (ECONNREFUSED)&lt;/span&gt;
        &lt;span class="c1"&gt;# * ConnectionResetError (ECONNRESET)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gaierror&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# failed DNS resolution on connect()&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ne"&gt;TimeoutError&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="c1"&gt;# Timeout on connect(), recv(), send().&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ne"&gt;OSError&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;NETWORK_ERRNOS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ssl&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSLError&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Let&amp;#39;s consider any SSL error a connection error. Usually this is:&lt;/span&gt;
        &lt;span class="c1"&gt;# * ssl.SSLZeroReturnError: &amp;quot;TLS/SSL connection has been closed&amp;quot;&lt;/span&gt;
        &lt;span class="c1"&gt;# * ssl.SSLError: [SSL: BAD_LENGTH]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;REQUESTS_EXCEPTIONS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Any indication that requests lib failed due to a connection&lt;/span&gt;
        &lt;span class="c1"&gt;# error.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BOTOCORE_EXCEPTIONS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Any indication that boto3 lib failed due to a connection&lt;/span&gt;
        &lt;span class="c1"&gt;# error.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;


&lt;span class="c1"&gt;# =====================================================================&lt;/span&gt;
&lt;span class="c1"&gt;# --- unit tests&lt;/span&gt;
&lt;span class="c1"&gt;# =====================================================================&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;unittest&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;TestIsConnectionErr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_connection_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="ne"&gt;BrokenPipeError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="ne"&gt;ConnectionAbortedError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="ne"&gt;ConnectionRefusedError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="ne"&gt;ConnectionResetError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_not_connection_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;OSError&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_requests_exceptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSLError&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;test_botocore_exceptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionClosedError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectTimeoutError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;botocore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadTimeoutError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vm"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To use it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;hello there&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;is_connection_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;schedule_on_reconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;hello there&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="external-links"&gt;
&lt;h2&gt;External Links&lt;a class="headerlink" href="#external-links" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Github &lt;a class="reference external" href="https://gist.github.com/giampaolo/905b38a5ea9d5179eb0138e2f37a01a8"&gt;Gist&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="python"/><category term="socket"/><category term="network"/></entry><entry><title>Sublime Text: remember cursor position plugin</title><link href="https://gmpy.dev/blog/2022/sublime-text-remember-cursor-position-plugin" rel="alternate"/><published>2022-03-18T00:00:00+01:00</published><updated>2022-03-18T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2022-03-18:/blog/2022/sublime-text-remember-cursor-position-plugin</id><summary type="html">&lt;p&gt;My editor of choice for Python development is Sublime Text.
It has been for a very long time (10 years).
It's fast, minimalist and straight to the point, which is why I always resisted
the temptation to use more advanced and modern IDEs such as PyCharm or VS code,
which …&lt;/p&gt;</summary><content type="html">&lt;p&gt;My editor of choice for Python development is Sublime Text.
It has been for a very long time (10 years).
It's fast, minimalist and straight to the point, which is why I always resisted
the temptation to use more advanced and modern IDEs such as PyCharm or VS code,
which admittedly have superior auto-completion and refactoring tools.&lt;/p&gt;
&lt;p&gt;There is a very simple feature I've always missed in ST: the
possibility to &amp;quot;remember&amp;quot; / save the cursor position when a file is closed.
The only plugin promising to do such a thing is called
&lt;a class="reference external" href="https://github.com/titoBouzout/BufferScroll"&gt;BufferScroll&lt;/a&gt;, but for some
reason it ceased working for me at some point.
I spent a considerable amount of time Googling for an alternative but, to my
surprise, I couldn't find any plugin which implements such a simple feature.
Therefore today I decided to bite the bullet and try to implement this myself,
by writing my first ST plugin, which I paste below.&lt;/p&gt;
&lt;p&gt;What it does is this:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;every time a file is closed, save the cursor position (x and y axes) to a JSON file&lt;/li&gt;
&lt;li&gt;if that same file is re-opened, restore the cursor at that position&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What's neat about ST plugins is that they are just Python files which you can
install by copying them into ST's config directory. On Linux you can copy the
script below to:&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;~/.config/sublime-text-3/Packages/User/cursor_positions.py&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;p&gt;...and it will work out of the box.
This is exactly the kind of minimalism which I love about ST, and which I've
always missed in other IDEs.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# cursor_positions.py&lt;/span&gt;

&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="sd"&gt;A plugin for SublimeText which saves (remembers) cursor position when&lt;/span&gt;
&lt;span class="sd"&gt;a file is closed.&lt;/span&gt;
&lt;span class="sd"&gt;Install it by copying this file in ~/.config/sublime-text-3/Packages/User/&lt;/span&gt;
&lt;span class="sd"&gt;directory (Linux).&lt;/span&gt;

&lt;span class="sd"&gt;Author: Giampaolo Rodola&amp;#39;&lt;/span&gt;
&lt;span class="sd"&gt;License: MIT&lt;/span&gt;
&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;datetime&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;tempfile&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;threading&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;sublime&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;sublime_plugin&lt;/span&gt;


&lt;span class="n"&gt;SUBLIME_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;realpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sublime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;packages_path&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;..&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;SESSION_FILE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;SUBLIME_ROOT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Local&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cursor_positions.session.json&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# when reading the session file on startup, we&amp;#39;ll remove entries&lt;/span&gt;
&lt;span class="c1"&gt;# older than X days&lt;/span&gt;
&lt;span class="n"&gt;RM_FILE_OLDER_THAN_DAYS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;180&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;    &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;: &amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vm"&gt;__file__&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;makedirs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SESSION_FILE&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;exist_ok&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prune_old_entries&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# --- file&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;read_session_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SESSION_FILE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;FileNotFoundError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONDecodeError&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;write_session_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Use the same FS so that the move operation is atomic:&lt;/span&gt;
        &lt;span class="c1"&gt;# https://stackoverflow.com/a/18706666&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;tempfile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NamedTemporaryFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s2"&gt;&amp;quot;wt&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SESSION_FILE&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sort_keys&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_lock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SESSION_FILE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;prune_old_entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_session_file&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="n"&gt;tstamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;last_update&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;last_update&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strptime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;tstamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;%Y-%m-&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s1"&gt; %H:%M:%S.&lt;/span&gt;&lt;span class="si"&gt;%f&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;delta_days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;last_update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;delta_days&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;RM_FILE_OLDER_THAN_DAYS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;removing old saved file &lt;/span&gt;&lt;span class="si"&gt;%r&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write_session_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# --- operations&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;add_entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_session_file&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;last_update&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write_session_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;load_entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_session_file&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;


&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Events&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sublime_plugin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EventListener&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="c1"&gt;# --- utils&lt;/span&gt;

    &lt;span class="nd"&gt;@staticmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;get_cursor_pos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowcol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

    &lt;span class="nd"&gt;@staticmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;set_cursor_pos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;pt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text_point&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sublime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Region&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pt&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;save_cursor_position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;file_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="c1"&gt;# non-existent file&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;saving cursor position for &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_cursor_pos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;load_cursor_position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load_entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_cursor_pos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;y&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="c1"&gt;# --- callbacks&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;on_close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# called when a file is closed&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save_cursor_position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;on_load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# called when a file is opened&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load_cursor_position&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
</content><category term="sublime"/><category term="python"/></entry><entry><title>New Pelican website</title><link href="https://gmpy.dev/blog/2020/new-pelican-website" rel="alternate"/><published>2020-06-26T00:00:00+02:00</published><updated>2020-06-26T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2020-06-26:/blog/2020/new-pelican-website</id><summary type="html">&lt;p&gt;Hello there. Here is my new blog / personal website!
This is something I've been wanting to do for a very long time, since the old blog hosted at &lt;a class="reference external" href="https://grodola.blogspot.com/"&gt;https://grodola.blogspot.com/&lt;/a&gt; was... well, too old. =)
This new site is based on &lt;a class="reference external" href="https://docs.getpelican.com/en/stable/"&gt;Pelican&lt;/a&gt;, a static website generator similar to &lt;a class="reference external" href="https://jekyllrb.com/"&gt;Jekyll …&lt;/a&gt;&lt;/p&gt;</summary><content type="html">&lt;p&gt;Hello there. Here is my new blog / personal website!
This is something I've been wanting to do for a very long time, since the old blog hosted at &lt;a class="reference external" href="https://grodola.blogspot.com/"&gt;https://grodola.blogspot.com/&lt;/a&gt; was... well, too old. =)
This new site is based on &lt;a class="reference external" href="https://docs.getpelican.com/en/stable/"&gt;Pelican&lt;/a&gt;, a static website generator similar to &lt;a class="reference external" href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;. Unlike Jekyll, it uses Python instead of Ruby, and that's why I chose it. It's minimal, straight to the point and I feel I have control of things. This is what Pelican gave me out of the box:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;blog functionality&lt;/li&gt;
&lt;li&gt;ability to write content by using reStructuredText&lt;/li&gt;
&lt;li&gt;RSS &amp;amp; Atom feed&lt;/li&gt;
&lt;li&gt;easy integration with GitHub pages&lt;/li&gt;
&lt;li&gt;ability to add comments via Disqus&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To this I added a mailing list (I used feedburner), so that users can subscribe and receive an email every time I make a new blog post. As you can see the website is very simple, but it's exactly what I wanted (minimalism). As for the domain name I opted for &lt;a class="reference external" href="https://gmpy.dev/"&gt;gmpy.dev&lt;/a&gt;, mostly because I know my name is hard to type and pronounce for non-English speakers. And also because I couldn't come up with a better name. ;)&lt;/p&gt;
&lt;div class="section" id="git-based-workflow"&gt;
&lt;h2&gt;GIT-based workflow&lt;a class="headerlink" href="#git-based-workflow" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The main reason I blogged so rarely over the years was that blogger.com provided me no way to edit content in reST or markdown, and lacked GIT integration. This made me lazy. With Pelican + GitHub pages the workflow to create and publish new content is very straightforward. I use 2 branches: &lt;a class="reference external" href="https://github.com/giampaolo/giampaolo.github.io/tree/gh-pages"&gt;gh-pages&lt;/a&gt;, which is the source code of this web-site, and &lt;a class="reference external" href="https://github.com/giampaolo/giampaolo.github.io/tree/master"&gt;master&lt;/a&gt;, which is where the generated HTML content lives and is being served by GitHub pages. This is what I do when I have to create a new blog post:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;under &lt;cite&gt;gh-pages&lt;/cite&gt; branch I create a new file, e.g. &lt;cite&gt;content/blog/2020/new-blog-post.rst&lt;/cite&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gh"&gt;New blog post&lt;/span&gt;
&lt;span class="gh"&gt;#############&lt;/span&gt;

&lt;span class="nc"&gt;:date:&lt;/span&gt; 2020-06-26
&lt;span class="nc"&gt;:tags:&lt;/span&gt; announce, python

Hello world!
&lt;/pre&gt;&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;commit it:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;git add content/blog/2020/new-blog-post.rst
git ci -am &amp;quot;new blog post&amp;quot;
git push
&lt;/pre&gt;&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;publish it:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;make github
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Within a minute or so, GitHub will automatically serve &lt;a class="reference external" href="https://gmpy.dev/"&gt;gmpy.dev&lt;/a&gt; with the updated content. And this is why I think I will start blogging more often. =)
The core of Pelican is &lt;a class="reference external" href="https://github.com/giampaolo/giampaolo.github.io/blob/gh-pages/pelicanconf.py"&gt;pelicanconf.py&lt;/a&gt;, which lets you customize a lot of things by remaining independent from the theme. I still ended up modifying the default theme though, writing a customized &amp;quot;archives&amp;quot; view and editing CSS to make the site look better on mobile phones. All in all, I am very satisfied with Pelican, and I'm keen on recommending it to anyone who doesn't need dynamic content.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="about-me"&gt;
&lt;h2&gt;About me&lt;a class="headerlink" href="#about-me" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I spent most of last year (2019) in China, dating my girlfriend, working remotely from a shared office space in Shenzhen, and I even taught some Python to a class of Chinese folks with no previous programming experience. The course was about the basics of the language plus basic filesystem operations, and the most difficult thing to explain was indentation. I guess that shows the difference between knowing a language and knowing how to teach it.&lt;/p&gt;
&lt;p&gt;I got back to Italy in December 2019, just before the pandemic occurred. Because of my connections with China, I knew about the incoming pandemic sooner than the rest of my friends, which for a while (until the lockdown) made them think I was crazy. =)&lt;/p&gt;
&lt;p&gt;Since I knew I would be stuck at home for a long time, I bought a quite nice acoustic guitar (Taylor) after many years, and resumed playing (and singing). I learned a bunch of new songs, mostly by Queen, including Bohemian Rhapsody, which is something I've been wanting to do since forever.&lt;/p&gt;
&lt;p&gt;I also spent some time working on a personal project that I'm keeping private for the moment, something to speed up file copies, which follows the experiments I made in &lt;a class="reference external" href="https://bugs.python.org/issue33671"&gt;BPO-33671&lt;/a&gt;. It's still very beta, but I managed to make file copies around 170% faster compared to the &lt;tt class="docutils literal"&gt;cp&lt;/tt&gt; command on Linux, which is pretty impressive (and I think I can push it even further). I will blog about that once I have something more solid / stable. Most likely it'll become my next OSS project, even though I have mixed feelings about that, since the amount of time I'm devoting to psutil is already a lot.&lt;/p&gt;
&lt;p&gt;Speaking of which, today I'm also releasing &lt;a class="reference external" href="https://groups.google.com/forum/#!topic/psutil/zT0jUE2IaE4"&gt;psutil 5.7.1&lt;/a&gt;, which adds support for &lt;strong&gt;Windows Nano&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I guess this is all. Cheers and subscribe!&lt;/p&gt;
&lt;/div&gt;
</content><category term="announce"/><category term="python"/><category term="pelican"/><category term="psutil"/></entry><entry><title>System load average on Windows in Python</title><link href="https://gmpy.dev/blog/2019/system-load-average-on-windows-in-python" rel="alternate"/><published>2019-05-29T00:00:00+02:00</published><updated>2019-05-29T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2019-05-29:/blog/2019/system-load-average-on-windows-in-python</id><summary type="html">&lt;p&gt;psutil 5.6.2 is out. It implements an emulation of &lt;a class="reference external" href="https://docs.python.org/3/library/os.html#os.getloadavg"&gt;os.getloadavg()&lt;/a&gt; on Windows, kindly contributed by &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1485"&gt;Ammar Askar&lt;/a&gt;, who originally implemented it for &lt;a class="reference external" href="https://github.com/python/cpython/pull/8357/files"&gt;CPython's test suite&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This idea has been floating around for quite a while. The first proposal dates back to &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/139"&gt;2010&lt;/a&gt;, when psutil was still hosted …&lt;/p&gt;</summary><content type="html">&lt;p&gt;psutil 5.6.2 is out. It implements an emulation of &lt;a class="reference external" href="https://docs.python.org/3/library/os.html#os.getloadavg"&gt;os.getloadavg()&lt;/a&gt; on Windows, kindly contributed by &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1485"&gt;Ammar Askar&lt;/a&gt;, who originally implemented it for &lt;a class="reference external" href="https://github.com/python/cpython/pull/8357/files"&gt;CPython's test suite&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This idea has been floating around for quite a while. The first proposal dates back to &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/139"&gt;2010&lt;/a&gt;, when psutil was still hosted on Google Code, and it popped up &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues?utf8=%E2%9C%93&amp;amp;q=getloadavg"&gt;multiple times&lt;/a&gt; over the years. There's a bunch of info online mentioning the pieces you'd theoretically use (the so-called &lt;tt class="docutils literal"&gt;System Processor Queue Length&lt;/tt&gt;), but I couldn't find any real implementation. A quick search suggests there's real demand for this, but very few tools provide it natively (the only ones I could find are &lt;a class="reference external" href="https://blog.sflow.com/2011/02/windows-load-average.html"&gt;sFlowTrend&lt;/a&gt; and &lt;a class="reference external" href="https://www.zabbix.com/forum/zabbix-help/50423-windows-cpu-load"&gt;Zabbix&lt;/a&gt;). So I'm glad this finally landed in psutil / Python.&lt;/p&gt;
&lt;div class="section" id="other-improvements-and-bugfixes-in-psutil-5-6-2"&gt;
&lt;h2&gt;Other improvements and bugfixes in psutil 5.6.2&lt;a class="headerlink" href="#other-improvements-and-bugfixes-in-psutil-5-6-2" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The full list is in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;. A couple worth mentioning:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1476"&gt;#1476&lt;/a&gt;: ability to set a process's high I/O priority on Windows.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1458"&gt;#1458&lt;/a&gt;: colorized test output. Nobody will use this directly, but it's nice and I'm porting it to other projects I maintain (e.g. pyftpdlib). Good candidate for a small PyPI module that could also include the unittest extensions I've been re-implementing piece by piece:&lt;ul&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1478"&gt;#1478&lt;/a&gt;: re-running failed tests.&lt;/li&gt;
&lt;li&gt;display test timings / durations. This is something I'm also contributing to CPython: &lt;a class="reference external" href="https://bugs.python.org/issue4080"&gt;BPO-4080&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/python/cpython/pull/12271/files"&gt;PR-12271&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="about-me"&gt;
&lt;h2&gt;About me&lt;a class="headerlink" href="#about-me" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I'm currently in China (Shenzhen) for a mix of vacation and work, and I will likely take a break from Open Source for a while (about 2.5 months), during which I'll also go to the Philippines and Japan.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="external"&gt;
&lt;h2&gt;External&lt;a class="headerlink" href="#external" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/bhji0m/new_psutil_562_with_load_average_emulation_on/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="windows"/><category term="new-api"/><category term="release"/><category term="personal"/></entry><entry><title>Announcing psutil 5.6.0</title><link href="https://gmpy.dev/blog/2019/psutil-560-and-process-parents" rel="alternate"/><published>2019-03-05T00:00:00+01:00</published><updated>2019-03-05T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2019-03-05:/blog/2019/psutil-560-and-process-parents</id><summary type="html">&lt;p&gt;psutil 5.6.0 is out. Highlights: a new &lt;tt class="docutils literal"&gt;Process.parents()&lt;/tt&gt; method, several important Windows improvements, and the removal of &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; on macOS.&lt;/p&gt;
&lt;div class="section" id="process-parents"&gt;
&lt;h2&gt;Process parents()&lt;a class="headerlink" href="#process-parents" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The new method returns the parents of a process as a list of &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; instances. If no parents are known, an empty list is …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;psutil 5.6.0 is out. Highlights: a new &lt;tt class="docutils literal"&gt;Process.parents()&lt;/tt&gt; method, several important Windows improvements, and the removal of &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; on macOS.&lt;/p&gt;
&lt;div class="section" id="process-parents"&gt;
&lt;h2&gt;Process parents()&lt;a class="headerlink" href="#process-parents" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The new method returns the parents of a process as a list of &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; instances. If no parents are known, an empty list is returned.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5312&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;[psutil.Process(pid=4699, name=&amp;#39;bash&amp;#39;, started=&amp;#39;09:06:44&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt; psutil.Process(pid=4689, name=&amp;#39;gnome-terminal-server&amp;#39;, started=&amp;#39;09:06:44&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt; psutil.Process(pid=1, name=&amp;#39;systemd&amp;#39;, started=&amp;#39;05:56:55&amp;#39;)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Nothing fundamentally new here, since this is a convenience wrapper around &lt;tt class="docutils literal"&gt;Process.parent()&lt;/tt&gt;, but it's still nice to have it built in. It pairs well with &lt;tt class="docutils literal"&gt;Process.children()&lt;/tt&gt; when working with process trees. The idea was proposed by Ghislain Le Meur.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="windows"&gt;
&lt;h2&gt;Windows&lt;a class="headerlink" href="#windows" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Certain Windows APIs that need to be dynamically loaded from DLLs are now loaded only once at startup, instead of on every function call. This makes some operations &lt;strong&gt;50% to 100% faster&lt;/strong&gt;; see benchmarks in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1422"&gt;PR-1422&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;Process.suspend()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.resume()&lt;/tt&gt; previously iterated over all process threads via &lt;tt class="docutils literal"&gt;CreateToolhelp32Snapshot()&lt;/tt&gt;, which was unorthodox and broke when the process had been suspended by Process Hacker. They now call the undocumented &lt;tt class="docutils literal"&gt;NtSuspendProcess()&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;NtResumeProcess()&lt;/tt&gt; NT APIs, same as Process Hacker and Sysinternals tools. Discussed in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1379"&gt;#1379&lt;/a&gt;, implemented in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1435"&gt;PR-1435&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;SE DEBUG&lt;/tt&gt; is a privilege bit set on the Python process at startup so psutil can query processes owned by other users (Administrator, Local System), meaning fewer &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt; exceptions for low-PID processes. The code setting it had presumably been broken for years and is now finally fixed in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1429"&gt;PR-1429&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="removal-of-process-memory-maps-on-macos"&gt;
&lt;h2&gt;Removal of Process.memory_maps() on macOS&lt;a class="headerlink" href="#removal-of-process-memory-maps-on-macos" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is gone on macOS (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1291"&gt;#1291&lt;/a&gt;). The underlying Apple API would randomly raise &lt;tt class="docutils literal"&gt;EINVAL&lt;/tt&gt; or segfault the host process, and no amount of reverse-engineering produced a safe fix. So I removed it. This is covered in a &lt;a class="reference external" href="removing-processmemory_maps-on-macos"&gt;separate post&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="improved-exceptions"&gt;
&lt;h2&gt;Improved exceptions&lt;a class="headerlink" href="#improved-exceptions" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One problem that affected psutil maintenance over the years was receiving bug reports whose tracebacks did not indicate which syscall had actually failed. This was especially painful on Windows, where a single routine may invoke multiple Windows APIs. Now the &lt;tt class="docutils literal"&gt;OSError&lt;/tt&gt; (or &lt;tt class="docutils literal"&gt;WindowsError&lt;/tt&gt;) exception includes the syscall from which the error originated. See &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1428"&gt;PR-1428&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-changes"&gt;
&lt;h2&gt;Other changes&lt;a class="headerlink" href="#other-changes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;See the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="windows"/><category term="macos"/><category term="new-api"/><category term="compatibility"/><category term="release"/></entry><entry><title>Removing Process.memory_maps() on macOS</title><link href="https://gmpy.dev/blog/2019/removing-processmemory_maps-on-macos" rel="alternate"/><published>2019-03-05T00:00:00+01:00</published><updated>2019-03-05T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2019-03-05:/blog/2019/removing-processmemory_maps-on-macos</id><summary type="html">&lt;p&gt;This is part of the psutil 5.6.0 release (see the full &lt;a class="reference external" href="psutil-560-and-process-parents"&gt;release notes&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;As of 5.6.0, &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is no longer defined on macOS.&lt;/p&gt;
&lt;div class="section" id="the-bug"&gt;
&lt;h2&gt;The bug&lt;a class="headerlink" href="#the-bug" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1291"&gt;#1291&lt;/a&gt;: on macOS, &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; would either raise &lt;tt class="docutils literal"&gt;OSError: [Errno 22] Invalid argument&lt;/tt&gt; or segfault the whole Python process! Both …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;This is part of the psutil 5.6.0 release (see the full &lt;a class="reference external" href="psutil-560-and-process-parents"&gt;release notes&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;As of 5.6.0, &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is no longer defined on macOS.&lt;/p&gt;
&lt;div class="section" id="the-bug"&gt;
&lt;h2&gt;The bug&lt;a class="headerlink" href="#the-bug" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1291"&gt;#1291&lt;/a&gt;: on macOS, &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; would either raise &lt;tt class="docutils literal"&gt;OSError: [Errno 22] Invalid argument&lt;/tt&gt; or segfault the whole Python process! Both triggered from code as simple as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;psutil.Process().as_dict()&lt;/span&gt;&lt;/tt&gt;, since &lt;tt class="docutils literal"&gt;Process.as_dict()&lt;/tt&gt; iterates every attribute, and &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is one of them.&lt;/p&gt;
&lt;p&gt;The root cause was inside Apple's undocumented &lt;tt class="docutils literal"&gt;proc_regionfilename()&lt;/tt&gt; syscall. On some memory regions it returns &lt;tt class="docutils literal"&gt;EINVAL&lt;/tt&gt;. On others it takes the process down. Which regions? Nobody figured out. Arnon Yaari (&lt;a class="reference external" href="https://github.com/wiggin15"&gt;&amp;#64;wiggin15&lt;/a&gt;) did most of the investigation: he wrote a &lt;a class="reference external" href="https://gist.github.com/wiggin15/0a4c51b5bc6c52e6e31e2234f88558ab"&gt;standalone C reproducer&lt;/a&gt; and walked me through what he'd tried.&lt;/p&gt;
&lt;p&gt;In &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1436"&gt;PR-1436&lt;/a&gt; I attempted a fix by reverse-engineering &lt;tt class="docutils literal"&gt;vmmap(1)&lt;/tt&gt; but it didn't work. The fundamental problem is that &lt;tt class="docutils literal"&gt;vmmap&lt;/tt&gt; is closed source and &lt;tt class="docutils literal"&gt;proc_regionfilename&lt;/tt&gt; is undocumented. Neither my virtualized macOS (10.11.6) nor Travis CI (10.12.1) could reproduce the bug, which reproduced reliably only on 10.14.3.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="why-remove-outright"&gt;
&lt;h2&gt;Why remove outright&lt;a class="headerlink" href="#why-remove-outright" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While removing the C code I noticed that the macOS unit test had been disabled long ago, presumably by me after recurring flaky Travis runs. Meaning that the method had been broken on some macOS versions far longer than the 2018 bug report suggested.&lt;/p&gt;
&lt;p&gt;Deprecating for a cycle didn't help either: raising &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt; breaks code that relied on a successful return, returning an empty list does the same silently, and leaving the method in place doesn't stop the segfault. Basically there was no sane solution, so since 5.6 is a major version I decided to just remove &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; for good.&lt;/p&gt;
&lt;p&gt;On macOS it never supported other processes anyway. Calling it on any PID other than the current one (or its children) raised &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt;, even as root.&lt;/p&gt;
&lt;p&gt;If someone finds a Mach API path that works, the method can return. Nobody has found one so far.&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="macos"/><category term="api-design"/><category term="compatibility"/><category term="release"/></entry><entry><title>AIX support</title><link href="https://gmpy.dev/blog/2017/psutil-540-and-aix-support" rel="alternate"/><published>2017-10-12T00:00:00+02:00</published><updated>2017-10-12T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2017-10-12:/blog/2017/psutil-540-and-aix-support</id><summary type="html">&lt;p&gt;After a long wait psutil finally supports a new exotic platform: AIX!&lt;/p&gt;
&lt;p&gt;Honestly I'm not sure how many AIX Python users are out there (probably not many), but here it is.&lt;/p&gt;
&lt;p&gt;For this we have to thank &lt;a class="reference external" href="https://github.com/wiggin15"&gt;Arnon Yaari&lt;/a&gt;, who started working on the port a couple of years ago …&lt;/p&gt;</summary><content type="html">&lt;p&gt;After a long wait psutil finally supports a new exotic platform: AIX!&lt;/p&gt;
&lt;p&gt;Honestly I'm not sure how many AIX Python users are out there (probably not many), but here it is.&lt;/p&gt;
&lt;p&gt;For this we have to thank &lt;a class="reference external" href="https://github.com/wiggin15"&gt;Arnon Yaari&lt;/a&gt;, who started working on the port a couple of years ago (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/605"&gt;#605&lt;/a&gt;). I was skeptical at first, because AIX is the only platform I can't virtualize and test on my laptop, so that made me a bit nervous. Arnon did a great job. The final &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1123"&gt;PR-1123&lt;/a&gt; is huge: it required a considerable amount of work on his part, and a review of more than 140 messages exchanged between us over about a month, during which I was travelling through China.&lt;/p&gt;
&lt;p&gt;The end result is very good: almost all original unit tests pass, and code quality is awesome, which (I must say) is fairly unusual for an external contribution like this. Kudos to you, Arnon! ;-)&lt;/p&gt;
&lt;div class="section" id="other-changes"&gt;
&lt;h2&gt;Other changes&lt;a class="headerlink" href="#other-changes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Besides AIX support, release 5.4.0 also includes a couple of important bug fixes for &lt;tt class="docutils literal"&gt;psutil.sensors_temperatures()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;psutil.sensors_fans()&lt;/tt&gt; on Linux, and a fix for a bug on macOS that could cause a segmentation fault when using &lt;tt class="docutils literal"&gt;Process.open_files()&lt;/tt&gt;. The complete list of bug fixes is in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-future"&gt;
&lt;h2&gt;The future&lt;a class="headerlink" href="#the-future" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Looking ahead at other exotic, still-unsupported platforms, two contributions are worth mentioning: a (still incomplete) PR for Cygwin which looks promising (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/998"&gt;PR-998&lt;/a&gt;), and Mingw32 compiler support on Windows (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/845"&gt;PR-845&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;psutil is gradually reaching a point where adding new features is becoming rarer, so it's a good moment to welcome new platforms while the API is mature and stable.&lt;/p&gt;
&lt;p&gt;Future work along these lines could also include Android and (hopefully) iOS support. Now &lt;em&gt;that&lt;/em&gt; would be really awesome to have.&lt;/p&gt;
&lt;p&gt;Stay tuned.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/75wsfu/psutil_540_with_aix_support_is_out/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="http://grodola.blogspot.com/2017/10/psutil-540-with-aix-support-is-out.html"&gt;Blogspot&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="new-platform"/><category term="community"/><category term="personal"/><category term="release"/></entry><entry><title>Announcing psutil 5.3.0</title><link href="https://gmpy.dev/blog/2017/announcing-psutil-530" rel="alternate"/><published>2017-09-03T00:00:00+02:00</published><updated>2017-09-03T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2017-09-03:/blog/2017/announcing-psutil-530</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 5.3.0 is finally out. This release is a major one, bigger than any release before it in terms of improvements and bugfixes. It is interesting to notice how huge the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/compare/release-5.2.2...release-5.3.0#files_bucket"&gt;diff&lt;/a&gt; between 5.2.2 and 5.3.0 is. This is because I've been travelling quite …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 5.3.0 is finally out. This release is a major one, bigger than any release before it in terms of improvements and bugfixes. It is interesting to notice how huge the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/compare/release-5.2.2...release-5.3.0#files_bucket"&gt;diff&lt;/a&gt; between 5.2.2 and 5.3.0 is. This is because I've been travelling quite a lot this year, so I kept postponing it. It may sound weird but I consider publishing a new release and writing a blog post about it more stressful than working on the release itself. =). Anyway, here goes.&lt;/p&gt;
&lt;div class="section" id="full-unicode-support"&gt;
&lt;h2&gt;Full Unicode support&lt;a class="headerlink" href="#full-unicode-support" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;String-returning APIs (&lt;tt class="docutils literal"&gt;Process.exe()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.cwd()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.username()&lt;/tt&gt;, etc.) are now Unicode-correct on both Python 2 and 3 (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1040"&gt;#1040&lt;/a&gt;), see &lt;a class="reference external" href="/blog/2017/psutil-530-fixing-unicode.html"&gt;detailed separate blog post&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="improved-process-iter"&gt;
&lt;h2&gt;Improved process_iter()&lt;a class="headerlink" href="#improved-process-iter" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.process_iter"&gt;process_iter()&lt;/a&gt; now accepts &lt;tt class="docutils literal"&gt;attrs&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;ad_value&lt;/tt&gt; parameters, letting you pre-fetch process attributes in one shot and skip the &lt;tt class="docutils literal"&gt;try/except NoSuchProcess&lt;/tt&gt; boilerplate, see &lt;a class="reference external" href="/blog/2017/psutil-530-improved-process-iter.html"&gt;detailed separate blog post&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="automatic-overflow-handling-of-numbers"&gt;
&lt;h2&gt;Automatic overflow handling of numbers&lt;a class="headerlink" href="#automatic-overflow-handling-of-numbers" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On very busy or long-lived systems, numbers returned by &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.disk_io_counters"&gt;disk_io_counters()&lt;/a&gt; and &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.net_io_counters"&gt;net_io_counters()&lt;/a&gt; functions may wrap (restart from zero). Up to version 5.2.x you had to take this into account, while now this is automatically handled by psutil (see: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/802"&gt;#802&lt;/a&gt;). If a &amp;quot;counter&amp;quot; restarts from 0 psutil will add the value from the previous call for you so that numbers will never decrease. This is crucial for applications monitoring disk or network I/O in real time. Old behavior can be resumed by passing the &lt;cite&gt;nowrap=True&lt;/cite&gt; argument.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="sunos-process-environ"&gt;
&lt;h2&gt;SunOS Process environ()&lt;a class="headerlink" href="#sunos-process-environ" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.environ"&gt;Process.environ()&lt;/a&gt; is now available also on SunOS (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1091"&gt;#1091&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-improvements-and-bug-fixes"&gt;
&lt;h2&gt;Other improvements and bug fixes&lt;a class="headerlink" href="#other-improvements-and-bug-fixes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Amongst others, here are a couple of important bug fixes I'd like to mention:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1044"&gt;#1044&lt;/a&gt;: on OSX different &lt;cite&gt;Process&lt;/cite&gt; methods could incorrectly raise &lt;cite&gt;AccessDenied&lt;/cite&gt; for zombie processes. This was due to the poor proc_pidpath OSX API.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1094"&gt;#1094&lt;/a&gt;: on Windows, &lt;cite&gt;pid_exists()&lt;/cite&gt; may lie due to the poor &lt;cite&gt;OpenProcess&lt;/cite&gt; Windows API which can return a handle even when a process PID no longer exists. This had repercussions for many Process methods such as &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.cmdline"&gt;cmdline()&lt;/a&gt;, &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.environ"&gt;environ()&lt;/a&gt;, &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.cwd"&gt;cwd()&lt;/a&gt;, &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.connections"&gt;connections()&lt;/a&gt; and others which could have unpredictable behaviors such as returning empty data or erroneously raising &lt;cite&gt;NoSuchProcess&lt;/cite&gt; exceptions. For the same reason (broken &lt;cite&gt;OpenProcess&lt;/cite&gt; API), processes could unexpectedly stick around after using &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.terminate"&gt;terminate()&lt;/a&gt; and &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.wait"&gt;wait()&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;BSD systems also received some love (NetBSD and OpenBSD in particular). Different memory leaks were fixed and functions returning connected sockets were partially rewritten. The full list of enhancements and bug fixes can be seen &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="about-me"&gt;
&lt;h2&gt;About me&lt;a class="headerlink" href="#about-me" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I would like to spend a couple more words about my current situation. Last year (2016) I relocated to Prague and remote worked from there the whole year (it's been cool - great city!). This year I have mainly been resting in Turin (Italy) due to some health issues and travelling across Asia once I started to recover. I am currently in Shenzhen, China, and unless the current situation with North Korea gets worse I'm planning to continue my trip until November and visit Taiwan, South Korea and Japan. Once I'm finished, the plan is to briefly return to Turin (Italy) and finally return to Prague. By then I will probably be looking for a new (remote) gig again, so if you have anything for me by November feel free to send me a message. ;-)&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="release"/><category term="personal"/></entry><entry><title>Fixing Unicode across Python 2 and 3</title><link href="https://gmpy.dev/blog/2017/fixing-unicode-in-psutil-across-python-2-and-3" rel="alternate"/><published>2017-09-03T00:00:00+02:00</published><updated>2017-09-03T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2017-09-03:/blog/2017/fixing-unicode-in-psutil-across-python-2-and-3</id><summary type="html">&lt;p&gt;This one took a while. Adding proper Unicode support to &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; took four months of auditing, design decisions, and rewriting nearly every API that returned a string. The full journey is documented in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1040"&gt;#1040&lt;/a&gt;, and what follows is a summary.&lt;/p&gt;
&lt;p&gt;This can serve as a case study for any Python …&lt;/p&gt;</summary><content type="html">&lt;p&gt;This one took a while. Adding proper Unicode support to &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; took four months of auditing, design decisions, and rewriting nearly every API that returned a string. The full journey is documented in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1040"&gt;#1040&lt;/a&gt;, and what follows is a summary.&lt;/p&gt;
&lt;p&gt;This can serve as a case study for any Python library with a C extension that needs to support both Python 2 &lt;em&gt;and&lt;/em&gt; Python 3, as it will encounter the exact same set of problems.&lt;/p&gt;
&lt;div class="section" id="what-was-broken"&gt;
&lt;h2&gt;What was broken&lt;a class="headerlink" href="#what-was-broken" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;psutil has different APIs returning a string, many of which misbehaved when it came to unicode. There were three distinctive problems (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/1040"&gt;#1040&lt;/a&gt;). Each API could:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;strong&gt;A&lt;/strong&gt;: raise a decoding error for non-ASCII strings (Python 3).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;B&lt;/strong&gt;: return &lt;tt class="docutils literal"&gt;unicode&lt;/tt&gt; instead of &lt;tt class="docutils literal"&gt;str&lt;/tt&gt; (Python 2).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;C&lt;/strong&gt;: return incorrect / invalid encoded data for non-ASCII strings (both).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; hit all three on various OSes. &lt;tt class="docutils literal"&gt;disk_partitions()&lt;/tt&gt; raised decoding errors on every UNIX except Linux. Windows service methods leaked &lt;tt class="docutils literal"&gt;unicode&lt;/tt&gt; into Python 2 return values. The C extension had accumulated years of ad-hoc encode/decode decisions, with no single rule covering all of them.&lt;/p&gt;
&lt;p&gt;It was a mess.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="filesystem-or-locale-encoding"&gt;
&lt;h2&gt;Filesystem or locale encoding?&lt;a class="headerlink" href="#filesystem-or-locale-encoding" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First problem was that the C extension was using 2 approaches when it came to decoding and returning a string: &lt;a class="reference external" href="https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeFSDefault"&gt;PyUnicode_DecodeFSDefault&lt;/a&gt; (filesystem encoding) for path-like APIs, and &lt;a class="reference external" href="https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeLocale"&gt;PyUnicode_DecodeLocale&lt;/a&gt; (user locale) for non-path strings like &lt;tt class="docutils literal"&gt;Process.username()&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;It appeared clear that I had to use &lt;tt class="docutils literal"&gt;PyUnicode_DecodeFSDefault&lt;/tt&gt; for all filesystem-related APIs like &lt;tt class="docutils literal"&gt;Process.exe()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.open_files()&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;It was less clear, though, when to use &lt;tt class="docutils literal"&gt;PyUnicode_DecodeLocale&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;After some back and forth, I decided to use a single encoding for all APIs: the &lt;strong&gt;filesystem encoding&lt;/strong&gt; (&lt;tt class="docutils literal"&gt;PyUnicode_DecodeFSDefault&lt;/tt&gt;). This makes the encoding choice an implementation detail of psutil, not something the user has to care about.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="error-handling"&gt;
&lt;h2&gt;Error handling&lt;a class="headerlink" href="#error-handling" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Second question was what to do in case the string cannot be correctly decoded (because invalid, corrupted or whatever). On Python 3 + UNIX the natural choice was &lt;tt class="docutils literal"&gt;'surrogateescape'&lt;/tt&gt;, which is also the default for &lt;tt class="docutils literal"&gt;PyUnicode_DecodeFSDefault&lt;/tt&gt;. On Windows the default is &lt;tt class="docutils literal"&gt;'surrogatepass'&lt;/tt&gt; (Python 3.6) or &lt;tt class="docutils literal"&gt;'replace'&lt;/tt&gt; as per &lt;a class="reference external" href="https://www.python.org/dev/peps/pep-0529/"&gt;PEP 529&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And here come the troubles: Python 2 is different. To correctly handle all kinds of strings on Python 2 we should return &lt;tt class="docutils literal"&gt;unicode&lt;/tt&gt; instead of &lt;tt class="docutils literal"&gt;str&lt;/tt&gt;, but I didn't want to do that, nor have APIs which return two different types depending on the circumstance.&lt;/p&gt;
&lt;p&gt;Since unicode support is already broken in Python 2 and its stdlib (see &lt;a class="reference external" href="https://bugs.python.org/issue18695"&gt;bpo-18695&lt;/a&gt;), I was happy to always return &lt;tt class="docutils literal"&gt;str&lt;/tt&gt;, use &lt;tt class="docutils literal"&gt;'replace'&lt;/tt&gt; as the error handler, and simply consider unicode support in psutil + Python 2 broken.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="final-behavior"&gt;
&lt;h2&gt;Final behavior&lt;a class="headerlink" href="#final-behavior" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Starting from 5.3.0, psutil behaves consistently across all APIs that return a string. The rules are intentionally simple, even if the underlying implementation is not.&lt;/p&gt;
&lt;p&gt;The notes below apply to &lt;em&gt;any&lt;/em&gt; method returning a string such as &lt;tt class="docutils literal"&gt;Process.exe()&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;Process.cwd()&lt;/tt&gt;, including non-filesystem-related methods such as &lt;tt class="docutils literal"&gt;Process.username()&lt;/tt&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;all strings are encoded using the OS filesystem encoding (&lt;tt class="docutils literal"&gt;PyUnicode_DecodeFSDefault&lt;/tt&gt;), which varies depending on the platform you're on (e.g. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;'UTF-8'&lt;/span&gt;&lt;/tt&gt; on Linux, &lt;tt class="docutils literal"&gt;'mbcs'&lt;/tt&gt; on Windows).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;no API call is supposed to crash with &lt;tt class="docutils literal"&gt;UnicodeDecodeError&lt;/tt&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;in case of badly encoded data returned by the OS, the following error handlers are used to replace the bad characters in the string:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Python 2: &lt;tt class="docutils literal"&gt;'replace'&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;Python 3: &lt;tt class="docutils literal"&gt;'surrogateescape'&lt;/tt&gt; on POSIX, &lt;tt class="docutils literal"&gt;'replace'&lt;/tt&gt; on Windows.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;on Python 2 all APIs return bytes (&lt;tt class="docutils literal"&gt;str&lt;/tt&gt; type), never &lt;tt class="docutils literal"&gt;unicode&lt;/tt&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;on Python 2 you can go back to unicode by doing:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;unicode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getdefaultencoding&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;replace&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The full journey was implemented in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/1052"&gt;PR-1052&lt;/a&gt;, and shipped in 5.3.0 (see the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="api-design"/><category term="compatibility"/><category term="python-core"/></entry><entry><title>Improved process_iter()</title><link href="https://gmpy.dev/blog/2017/improved-process_iter" rel="alternate"/><published>2017-09-03T00:00:00+02:00</published><updated>2017-09-03T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2017-09-03:/blog/2017/improved-process_iter</id><summary type="html">&lt;p&gt;This is part of the psutil 5.3.0 release (see the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt; for the full list of changes).&lt;/p&gt;
&lt;div class="section" id="the-old-pattern"&gt;
&lt;h2&gt;The old pattern&lt;a class="headerlink" href="#the-old-pattern" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Iterating over processes and collecting attributes requires more boilerplate than it should. A process returned by &lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt; may disappear before you access it, or require elevated privileges …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;This is part of the psutil 5.3.0 release (see the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt; for the full list of changes).&lt;/p&gt;
&lt;div class="section" id="the-old-pattern"&gt;
&lt;h2&gt;The old pattern&lt;a class="headerlink" href="#the-old-pattern" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Iterating over processes and collecting attributes requires more boilerplate than it should. A process returned by &lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt; may disappear before you access it, or require elevated privileges, so every lookup has to be guarded with a &lt;tt class="docutils literal"&gt;try / except&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="n"&gt;pinfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;pid&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NoSuchProcess&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AccessDenied&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pinfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 1, &amp;#39;name&amp;#39;: &amp;#39;systemd&amp;#39;}&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 2, &amp;#39;name&amp;#39;: &amp;#39;kthreadd&amp;#39;}&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 3, &amp;#39;name&amp;#39;: &amp;#39;ksoftirqd/0&amp;#39;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is not decorative. It's necessary to avoid the race condition.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-new-pattern"&gt;
&lt;h2&gt;The new pattern&lt;a class="headerlink" href="#the-new-pattern" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;5.3.0 adds &lt;tt class="docutils literal"&gt;attrs&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;ad_value&lt;/tt&gt; parameters to &lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt;. With these, the loop body becomes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;pid&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 1, &amp;#39;name&amp;#39;: &amp;#39;systemd&amp;#39;}&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 2, &amp;#39;name&amp;#39;: &amp;#39;kthreadd&amp;#39;}&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;pid&amp;#39;: 3, &amp;#39;name&amp;#39;: &amp;#39;ksoftirqd/0&amp;#39;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Internally, &lt;tt class="docutils literal"&gt;process_iter()&lt;/tt&gt; attaches an &lt;tt class="docutils literal"&gt;info&lt;/tt&gt; dict to the &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; instance. The attributes are pre-fetched in one shot. Processes that disappear during iteration are silently skipped, and attributes that would raise &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt; get assigned &lt;tt class="docutils literal"&gt;ad_value&lt;/tt&gt;, which defaults to &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ad_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;N/A&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="performance"&gt;
&lt;h2&gt;Performance&lt;a class="headerlink" href="#performance" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Beyond the syntactic win, the new syntax is also faster than calling individual methods in a loop. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;process_iter(attrs=[...])&lt;/span&gt;&lt;/tt&gt; is equivalent to using &lt;tt class="docutils literal"&gt;Process.oneshot()&lt;/tt&gt; on each process (see &lt;a class="reference external" href="/blog/2016/psutil-550-is-twice-as-fast"&gt;Making psutil twice as fast&lt;/a&gt; for how that works): attributes that share a syscall or a &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt; file are fetched together instead of re-read on every method call, which is a lot faster.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="comprehensions"&gt;
&lt;h2&gt;Comprehensions&lt;a class="headerlink" href="#comprehensions" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;With the exception boilerplate out of the way, comprehensions finally work cleanly. E.g. getting processes owned by the current user can be written as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;getpass&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pprint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pp&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;([(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;getpass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getuser&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt;
&lt;span class="go"&gt;[(16832, &amp;#39;bash&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt; (19772, &amp;#39;ssh&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt; (20492, &amp;#39;python&amp;#39;)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="api-design"/><category term="release"/><category term="performance"/><category term="new-api"/></entry><entry><title>Sensors: temperatures, battery, CPU frequency</title><link href="https://gmpy.dev/blog/2017/psutil-510-sensors-temperatures-battery-and-cpu-frequency" rel="alternate"/><published>2017-02-01T00:00:00+01:00</published><updated>2017-02-01T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2017-02-01:/blog/2017/psutil-510-sensors-temperatures-battery-and-cpu-frequency</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 5.1.0 is out. This release introduces new APIs to retrieve hardware temperatures, battery status, and CPU frequency information.&lt;/p&gt;
&lt;div class="section" id="temperatures"&gt;
&lt;h2&gt;Temperatures&lt;a class="headerlink" href="#temperatures" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can now retrieve hardware temperatures (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/962"&gt;PR-962&lt;/a&gt;). This is currently available on Linux only.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;On Windows it's hard to do in a hardware-agnostic way. I ran into 3 …&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 5.1.0 is out. This release introduces new APIs to retrieve hardware temperatures, battery status, and CPU frequency information.&lt;/p&gt;
&lt;div class="section" id="temperatures"&gt;
&lt;h2&gt;Temperatures&lt;a class="headerlink" href="#temperatures" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can now retrieve hardware temperatures (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/962"&gt;PR-962&lt;/a&gt;). This is currently available on Linux only.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;On Windows it's hard to do in a hardware-agnostic way. I ran into 3 WMI-based approaches, none of which worked with my hardware, so I gave up.&lt;/li&gt;
&lt;li&gt;On macOS it seems relatively easy, but my virtualized macOS box doesn't support sensors, so I gave up for lack of hardware. If someone wants to give it a try, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/371"&gt;be my guest&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sensors_temperatures&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;acpitz&amp;#39;: [shwtemp(label=&amp;#39;&amp;#39;, current=47.0, high=103.0, critical=103.0)],&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;asus&amp;#39;: [shwtemp(label=&amp;#39;&amp;#39;, current=47.0, high=None, critical=None)],&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;coretemp&amp;#39;: [shwtemp(label=&amp;#39;Physical id 0&amp;#39;, current=52.0, high=100.0, critical=100.0),&lt;/span&gt;
&lt;span class="go"&gt;              shwtemp(label=&amp;#39;Core 0&amp;#39;, current=45.0, high=100.0, critical=100.0),&lt;/span&gt;
&lt;span class="go"&gt;              shwtemp(label=&amp;#39;Core 1&amp;#39;, current=52.0, high=100.0, critical=100.0),&lt;/span&gt;
&lt;span class="go"&gt;              shwtemp(label=&amp;#39;Core 2&amp;#39;, current=45.0, high=100.0, critical=100.0),&lt;/span&gt;
&lt;span class="go"&gt;              shwtemp(label=&amp;#39;Core 3&amp;#39;, current=47.0, high=100.0, critical=100.0)]}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="battery-status"&gt;
&lt;h2&gt;Battery status&lt;a class="headerlink" href="#battery-status" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Battery status information is now available on Linux, Windows and FreeBSD (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/963"&gt;PR-963&lt;/a&gt;).&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;secs2hours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;divmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;hh&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;divmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="si"&gt;%02d&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="si"&gt;%02d&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hh&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;...&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;battery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sensors_battery&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;battery&lt;/span&gt;
&lt;span class="go"&gt;sbattery(percent=93, secsleft=16628, power_plugged=False)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;charge = &lt;/span&gt;&lt;span class="si"&gt;%s%%&lt;/span&gt;&lt;span class="s2"&gt;, time left = &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;battery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;percent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secs2hours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;battery&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secsleft&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="go"&gt;charge = 93%, time left = 4:37:08&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="cpu-frequency"&gt;
&lt;h2&gt;CPU frequency&lt;a class="headerlink" href="#cpu-frequency" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Available on Linux, Windows and macOS (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/952"&gt;PR-952&lt;/a&gt;). Only Linux reports the real-time value (always changing); other platforms return the nominal &amp;quot;fixed&amp;quot; value.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_freq&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;scpufreq(current=931.42925, min=800.0, max=3500.0)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_freq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percpu&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;[scpufreq(current=2394.945, min=800.0, max=3500.0),&lt;/span&gt;
&lt;span class="go"&gt; scpufreq(current=2236.812, min=800.0, max=3500.0),&lt;/span&gt;
&lt;span class="go"&gt; scpufreq(current=1703.609, min=800.0, max=3500.0),&lt;/span&gt;
&lt;span class="go"&gt; scpufreq(current=1754.289, min=800.0, max=3500.0)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="what-cpu-a-process-is-on"&gt;
&lt;h2&gt;What CPU a process is on&lt;a class="headerlink" href="#what-cpu-a-process-is-on" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Tells you which CPU a process is currently running on, somewhat related to &lt;tt class="docutils literal"&gt;Process.cpu_affinity()&lt;/tt&gt; (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/954"&gt;PR-954&lt;/a&gt;). It's interesting for visualizing how the OS scheduler keeps evenly reassigning processes across CPUs (see the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-5.1.0/scripts/cpu_distribution.py"&gt;cpu_distribution.py&lt;/a&gt; script).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="cpu-affinity"&gt;
&lt;h2&gt;CPU affinity&lt;a class="headerlink" href="#cpu-affinity" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A new shorthand is available to set affinity against all eligible CPUs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_affinity&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This was added because on Linux (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/956"&gt;#956&lt;/a&gt;) it is not always possible to set affinity against all CPUs directly. It is equivalent to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_affinity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;())))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="other-bug-fixes"&gt;
&lt;h2&gt;Other bug fixes&lt;a class="headerlink" href="#other-bug-fixes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;See the full list in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="new-api"/><category term="release"/><category term="linux"/></entry><entry><title>Making psutil twice as fast</title><link href="https://gmpy.dev/blog/2016/psutil-550-is-twice-as-fast" rel="alternate"/><published>2016-11-06T00:00:00+01:00</published><updated>2016-11-06T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-11-06:/blog/2016/psutil-550-is-twice-as-fast</id><summary type="html">&lt;p&gt;Starting from psutil 5.0.0 you can query multiple &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; fields around twice as fast as before (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/799"&gt;#799&lt;/a&gt; and &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.oneshot"&gt;Process.oneshot() doc&lt;/a&gt;). It took 7 months, 108 commits, and a massive refactoring of psutil internals (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/937"&gt;PR-937&lt;/a&gt;), and I think it's one of the best improvements ever shipped in …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Starting from psutil 5.0.0 you can query multiple &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; fields around twice as fast as before (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/799"&gt;#799&lt;/a&gt; and &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.Process.oneshot"&gt;Process.oneshot() doc&lt;/a&gt;). It took 7 months, 108 commits, and a massive refactoring of psutil internals (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/937"&gt;PR-937&lt;/a&gt;), and I think it's one of the best improvements ever shipped in a psutil release.&lt;/p&gt;
&lt;div class="section" id="the-problem"&gt;
&lt;h2&gt;The problem&lt;a class="headerlink" href="#the-problem" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How process information is retrieved varies by OS. Sometimes it means reading a file in &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt; (Linux), other times calling C (Windows, BSD, macOS, SunOS), but it's always done differently. Psutil abstracts this away: you call &lt;tt class="docutils literal"&gt;Process.name()&lt;/tt&gt; without worrying about what happens under the hood or which OS you're on.&lt;/p&gt;
&lt;p&gt;Internally, multiple pieces of process info (e.g. &lt;tt class="docutils literal"&gt;Process.name()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.ppid()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.uids()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.create_time()&lt;/tt&gt;) are fetched by the same syscall. On Linux we read &lt;tt class="docutils literal"&gt;/proc/PID/stat&lt;/tt&gt; to get the process name, terminal, CPU times, creation time, status and parent PID, but only one value is returned: the others are discarded. On Linux this code reads &lt;tt class="docutils literal"&gt;/proc/PID/stat&lt;/tt&gt; 6 times:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_times&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ppid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;On BSD most process metrics can be fetched with a single &lt;tt class="docutils literal"&gt;sysctl()&lt;/tt&gt;, yet psutil was invoking it for each process method (e.g. see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/2fe3f456321ca1605aaa2b71a7193de59d93075c/psutil/_psutil_bsd.c#L242-L258"&gt;here&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/2fe3f456321ca1605aaa2b71a7193de59d93075c/psutil/_psutil_bsd.c#L261-L277"&gt;here&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="do-it-in-one-shot"&gt;
&lt;h2&gt;Do it in one shot&lt;a class="headerlink" href="#do-it-in-one-shot" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It's clear that this approach is inefficient, especially in tools like top or htop, where process info is continuously fetched in a loop. psutil 5.0.0 introduces a new &lt;tt class="docutils literal"&gt;Process.oneshot()&lt;/tt&gt; context manager. Inside it, the internal routine runs once (in the example, on the first &lt;tt class="docutils literal"&gt;Process.name()&lt;/tt&gt; call) and the other values are cached. Subsequent calls sharing the same internal routine (read &lt;tt class="docutils literal"&gt;/proc/PID/stat&lt;/tt&gt;, call &lt;tt class="docutils literal"&gt;sysctl()&lt;/tt&gt; or whatever) return the cached value. The code above can now be rewritten like this, and on Linux it runs 2.4 times faster:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oneshot&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_times&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ppid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;... &lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="implementation"&gt;
&lt;h2&gt;Implementation&lt;a class="headerlink" href="#implementation" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One great thing about psutil's design is its abstraction. It is divided into 3 &amp;quot;layers&amp;quot;. Layer 1 is represented by the main &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/__init__.py#L340"&gt;Process class&lt;/a&gt; (Python), which exposes the high-level API. Layer 2 is the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/_pslinux.py#L1097"&gt;OS-specific Python module&lt;/a&gt;, which is a thin wrapper on top of the OS-specific &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/_psutil_linux.c"&gt;C extension module&lt;/a&gt; (layer 3).&lt;/p&gt;
&lt;p&gt;Because the code was organized this way (modular), the refactoring was reasonably smooth. I first refactored those C functions that collect multiple pieces of info and grouped them into a single function (e.g. see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/_psutil_bsd.c#L198-L338"&gt;BSD implementation&lt;/a&gt;). Then I wrote a &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/_common.py#L264-L314"&gt;decorator&lt;/a&gt; that enables the cache only when requested (when entering the context manager), and decorated the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/88ea5e0b2cc15c37fdeb3e38857f6dab6fd87d12/psutil/_psbsd.py#L491"&gt;&amp;quot;grouped functions&amp;quot;&lt;/a&gt; with it. The caching mechanism is controlled by the &lt;tt class="docutils literal"&gt;Process.oneshot()&lt;/tt&gt; context manager, which is the only thing exposed to the end user. Here's the decorator:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;memoize_when_activated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;A memoize decorator which is disabled by default. It can be&lt;/span&gt;
&lt;span class="sd"&gt;    activated and deactivated on request.&lt;/span&gt;
&lt;span class="sd"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="nd"&gt;@functools&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_activated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;cache_activate&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Activate cache.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_activated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;cache_deactivate&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Deactivate and clear cache.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_activated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
        &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_activated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
    &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_activate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache_activate&lt;/span&gt;
    &lt;span class="n"&gt;wrapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_deactivate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache_deactivate&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To measure the speedup I wrote a &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/b5582380ac70ca8c180344d9b22aacdff73b1e0b/scripts/internal/bench_oneshot.py"&gt;benchmark script&lt;/a&gt; (well, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/b5582380ac70ca8c180344d9b22aacdff73b1e0b/scripts/internal/bench_oneshot_2.py"&gt;two&lt;/a&gt; actually), and kept tuning until I was sure the change actually made psutil faster. The scripts report the speedup for calling all the &amp;quot;grouped&amp;quot; methods together (best-case scenario).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="linux-2-56x-speedup"&gt;
&lt;h2&gt;Linux: +2.56x speedup&lt;a class="headerlink" href="#linux-2-56x-speedup" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Linux implementation is mostly Python, reading files in &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt;. These files typically expose multiple pieces of info per process; &lt;tt class="docutils literal"&gt;/proc/PID/stat&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;/proc/PID/status&lt;/tt&gt; are the perfect example. We aggregate them into three groups. See the relevant code &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/b5582380ac70ca8c180344d9b22aacdff73b1e0b/psutil/_pslinux.py#L1108-L1153"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="windows-from-1-9x-to-6-5x-speedup"&gt;
&lt;h2&gt;Windows: from +1.9x to +6.5x speedup&lt;a class="headerlink" href="#windows-from-1-9x-to-6-5x-speedup" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Windows is an interesting one. For a process owned by our user, we group only &lt;tt class="docutils literal"&gt;Process.num_threads()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Process.num_ctx_switches()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.num_handles()&lt;/tt&gt;, for a +1.9x speedup if we access those methods in one shot.&lt;/p&gt;
&lt;p&gt;Windows is special though, because certain methods have a dual implementation (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/304"&gt;#304&lt;/a&gt;): a &amp;quot;fast method&amp;quot; is tried first, but if the process is owned by another user it fails with &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt;. psutil then falls back to a second, &amp;quot;slower&amp;quot; method (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/_pswindows.py#L681"&gt;here&lt;/a&gt; for example).&lt;/p&gt;
&lt;p&gt;It's slower because it &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/arch/windows/process_info.c#L790"&gt;iterates over all PIDs&lt;/a&gt;, but unlike the &amp;quot;plain&amp;quot; Windows APIs it can still &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/_psutil_windows.c#L2789"&gt;retrieve multiple pieces of information in one shot&lt;/a&gt;: number of threads, context switches, handles, CPU times, create time, and I/O counters.&lt;/p&gt;
&lt;p&gt;That's why querying processes owned by other users results in an impressive +6.5x speedup.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="macos-1-92x-speedup"&gt;
&lt;h2&gt;macOS: +1.92x speedup&lt;a class="headerlink" href="#macos-1-92x-speedup" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On macOS we can get 2 groups of information. With &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/_psutil_osx.c#L129"&gt;sysctl()&lt;/a&gt; we get process parent PID, uids, gids, terminal, create time, name. With &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/_psutil_osx.c#L183"&gt;proc_info()&lt;/a&gt; we get CPU times (for PIDs owned by another user), memory metrics and ctx switches. Not bad.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="bsd-2-18x-speedup"&gt;
&lt;h2&gt;BSD: +2.18x speedup&lt;a class="headerlink" href="#bsd-2-18x-speedup" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On BSD we gather tons of process info just by calling &lt;tt class="docutils literal"&gt;sysctl()&lt;/tt&gt; (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/0ccd1373c6e7a189e095df5c436568fb1e8b4d14/psutil/_psutil_bsd.c#L199"&gt;implementation&lt;/a&gt;): process name, ppid, status, uids, gids, IO counters, CPU and create times, terminal and ctx switches.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="sunos-1-37x-speedup"&gt;
&lt;h2&gt;SunOS: +1.37x speedup&lt;a class="headerlink" href="#sunos-1-37x-speedup" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;SunOS is like Linux (it reads files in &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt;), but the code is in C. Here too, we group different metrics together (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/b5582380ac70ca8c180344d9b22aacdff73b1e0b/psutil/_psutil_sunos.c#L83-L142"&gt;here&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/b5582380ac70ca8c180344d9b22aacdff73b1e0b/psutil/_psutil_sunos.c#L171-L189"&gt;here&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/5bhn4q/psutil_500_is_around_twice_as_fast/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="performance"/><category term="new-api"/><category term="featured"/><category term="release"/></entry><entry><title>Improved Linux memory metrics</title><link href="https://gmpy.dev/blog/2016/psutil-440-improved-linux-memory-metrics" rel="alternate"/><published>2016-10-23T00:00:00+02:00</published><updated>2016-10-23T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-10-23:/blog/2016/psutil-440-improved-linux-memory-metrics</id><summary type="html">&lt;p&gt;OK, another psutil release. The headline of 4.4.0 is more accurate memory metrics on Linux, plus a pile of macOS fixes I'd been sitting on for years.&lt;/p&gt;
&lt;div class="section" id="linux-virtual-memory"&gt;
&lt;h2&gt;Linux virtual memory&lt;a class="headerlink" href="#linux-virtual-memory" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;People had been complaining for a while that &lt;tt class="docutils literal"&gt;virtual_memory()&lt;/tt&gt; didn't match what &lt;tt class="docutils literal"&gt;free&lt;/tt&gt; reported on Linux (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/862"&gt;#862&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/685"&gt;#685 …&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;OK, another psutil release. The headline of 4.4.0 is more accurate memory metrics on Linux, plus a pile of macOS fixes I'd been sitting on for years.&lt;/p&gt;
&lt;div class="section" id="linux-virtual-memory"&gt;
&lt;h2&gt;Linux virtual memory&lt;a class="headerlink" href="#linux-virtual-memory" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;People had been complaining for a while that &lt;tt class="docutils literal"&gt;virtual_memory()&lt;/tt&gt; didn't match what &lt;tt class="docutils literal"&gt;free&lt;/tt&gt; reported on Linux (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/862"&gt;#862&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/685"&gt;#685&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/538"&gt;#538&lt;/a&gt;). I finally dug into it (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/887"&gt;#887&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/890"&gt;PR-890&lt;/a&gt;) and, funny enough, it turns out &lt;tt class="docutils literal"&gt;free&lt;/tt&gt; itself was doing it wrong until &lt;a class="reference external" href="https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773"&gt;about two years ago&lt;/a&gt;, when somebody got tired of everyone guessing and moved the calculation into the kernel.&lt;/p&gt;
&lt;p&gt;Starting with Linux 3.14, &lt;tt class="docutils literal"&gt;/proc/meminfo&lt;/tt&gt; has a &lt;tt class="docutils literal"&gt;MemAvailable&lt;/tt&gt; column. That's what psutil now uses, and the &lt;tt class="docutils literal"&gt;available&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;used&lt;/tt&gt; fields match &lt;tt class="docutils literal"&gt;free&lt;/tt&gt; exactly. On older kernels (&amp;lt; 3.14) psutil falls back to the &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/a5beb29488fe75c858d30a00044cbd29d3ed3d8b/psutil/_pslinux.py#L291"&gt;same formula&lt;/a&gt; that kernel commit introduced.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;free&lt;/tt&gt;'s source code also inspired a fix that prevents available memory from overflowing total memory on &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/a5beb29488fe75c858d30a00044cbd29d3ed3d8b/psutil/_pslinux.py#L435"&gt;LXC containers&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="macos-fixes"&gt;
&lt;h2&gt;macOS fixes&lt;a class="headerlink" href="#macos-fixes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For years I did psutil's macOS development on an old 10.5 install emulated via VirtualBox, running iDeneb (a hacked macOS). I finally got access to a more recent version (El Capitan) via VirtualBox + Vagrant, and could address a pile of long-standing bugs:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/514"&gt;#514&lt;/a&gt;: &lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; segfault (critical).&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/783"&gt;#783&lt;/a&gt;: &lt;tt class="docutils literal"&gt;Process.status()&lt;/tt&gt; could return &lt;tt class="docutils literal"&gt;&amp;quot;running&amp;quot;&lt;/tt&gt; for zombie processes.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/908"&gt;#908&lt;/a&gt;: several methods could mask the real error for high-privileged PIDs, raising &lt;tt class="docutils literal"&gt;NoSuchProcess&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt; instead of &lt;tt class="docutils literal"&gt;OSError&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;RuntimeError&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/909"&gt;#909&lt;/a&gt;: &lt;tt class="docutils literal"&gt;Process.open_files()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.connections()&lt;/tt&gt; could raise &lt;tt class="docutils literal"&gt;OSError&lt;/tt&gt; with no exception set when the process was gone.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/916"&gt;#916&lt;/a&gt;: fixed many compilation warnings.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="nic-netmask-on-windows"&gt;
&lt;h2&gt;NIC netmask on Windows&lt;a class="headerlink" href="#nic-netmask-on-windows" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Small but nice: &lt;tt class="docutils literal"&gt;net_if_addrs()&lt;/tt&gt; on Windows now returns the netmask too.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="improved-procinfo-py"&gt;
&lt;h2&gt;Improved procinfo.py&lt;a class="headerlink" href="#improved-procinfo-py" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-4.4.0/scripts/procinfo.py"&gt;scripts/procinfo.py&lt;/a&gt; is my kitchen-sink demo script. I taught it a bunch of new tricks, so it now dumps pretty much everything psutil knows about a process:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ python scripts/procinfo.py
pid           4600
name          chrome
parent        4554 (bash)
exe           /opt/google/chrome/chrome
cwd           /home/giampaolo
cmdline       /opt/google/chrome/chrome
started       2016-09-19 11:12
cpu-tspent    27:27.68
cpu-times     user=8914.32, system=3530.59,
              children_user=1.46, children_system=1.31
cpu-affinity  [0, 1, 2, 3, 4, 5, 6, 7]
memory        rss=520.5M, vms=1.9G, shared=132.6M, text=95.0M, lib=0B,
              data=816.5M, dirty=0B
memory %      3.26
user          giampaolo
uids          real=1000, effective=1000, saved=1000
terminal      /dev/pts/2
status        sleeping
nice          0
ionice        class=IOPriority.IOPRIO_CLASS_NONE, value=0
num-threads   47
num-fds       379
I/O           read_count=96.6M, write_count=80.7M,
              read_bytes=293.2M, write_bytes=24.5G
ctx-switches  voluntary=30426463, involuntary=460108
children      PID    NAME
              4605   cat
              4606   cat
              4609   chrome
              4669   chrome
open-files    PATH
              /opt/google/chrome/icudtl.dat
              /opt/google/chrome/snapshot_blob.bin
              /opt/google/chrome/natives_blob.bin
              /opt/google/chrome/chrome_100_percent.pak
              [...]
connections   PROTO LOCAL ADDR            REMOTE ADDR               STATUS
              UDP   10.0.0.3:3693         *:*                       NONE
              TCP   10.0.0.3:55102        172.217.22.14:443         ESTABLISHED
              UDP   10.0.0.3:35172        *:*                       NONE
              TCP   10.0.0.3:32922        172.217.16.163:443        ESTABLISHED
              UDP   :::5353               *:*                       NONE
              UDP   10.0.0.3:59925        *:*                       NONE
threads       TID              USER          SYSTEM
              11795             0.7            1.35
              11796            0.68            1.37
              15887            0.74            0.03
              19055            0.77            0.01
              [...]
              total=47
res-limits    RLIMIT                     SOFT       HARD
              virtualmem             infinity   infinity
              coredumpsize                  0   infinity
              cputime                infinity   infinity
              datasize               infinity   infinity
              filesize               infinity   infinity
              locks                  infinity   infinity
              memlock                   65536      65536
              msgqueue                 819200     819200
              nice                          0          0
              openfiles                  8192      65536
              maxprocesses              63304      63304
              rss                    infinity   infinity
              realtimeprio                  0          0
              rtimesched             infinity   infinity
              sigspending               63304      63304
              stack                   8388608   infinity
mem-maps      RSS      PATH
              381.4M   [anon]
              62.8M    /opt/google/chrome/chrome
              15.8M    /home/giampaolo/.config/google-chrome/Default/History
              6.6M     /home/giampaolo/.config/google-chrome/Default/Favicons
              [...]
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="other-changes"&gt;
&lt;h2&gt;Other changes&lt;a class="headerlink" href="#other-changes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The full list is in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="memory"/><category term="linux"/><category term="release"/></entry><entry><title>Windows services support</title><link href="https://gmpy.dev/blog/2016/psutil-420-windows-services-in-python" rel="alternate"/><published>2016-05-15T00:00:00+02:00</published><updated>2016-05-15T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-05-15:/blog/2016/psutil-420-windows-services-in-python</id><summary type="html">&lt;p&gt;New &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; 4.2.0 is out. The highlight of this release is the support for Windows services (executables that run at system startup, similar to UNIX init scripts):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;win_service_iter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="go"&gt;[&amp;lt;WindowsService(name=&amp;#39;AeLookupSvc&amp;#39;, display_name=&amp;#39;Application Experience&amp;#39;) at 38850096&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;lt;WindowsService(name=&amp;#39;ALG&amp;#39;, display_name=&amp;#39;Application Layer Gateway Service …&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;New &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; 4.2.0 is out. The highlight of this release is the support for Windows services (executables that run at system startup, similar to UNIX init scripts):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;win_service_iter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="go"&gt;[&amp;lt;WindowsService(name=&amp;#39;AeLookupSvc&amp;#39;, display_name=&amp;#39;Application Experience&amp;#39;) at 38850096&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;lt;WindowsService(name=&amp;#39;ALG&amp;#39;, display_name=&amp;#39;Application Layer Gateway Service&amp;#39;) at 38850128&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;lt;WindowsService(name=&amp;#39;APNMCP&amp;#39;, display_name=&amp;#39;Ask Update Service&amp;#39;) at 38850160&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;lt;WindowsService(name=&amp;#39;AppIDSvc&amp;#39;, display_name=&amp;#39;Application Identity&amp;#39;) at 38850192&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt; ...]&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;win_service_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;alg&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_dict&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;binpath&amp;#39;: &amp;#39;C:\\Windows\\System32\\alg.exe&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;description&amp;#39;: &amp;#39;Provides support for 3rd party protocol plug-ins for Internet Connection Sharing&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;display_name&amp;#39;: &amp;#39;Application Layer Gateway Service&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;name&amp;#39;: &amp;#39;alg&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;pid&amp;#39;: None,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;start_type&amp;#39;: &amp;#39;manual&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;status&amp;#39;: &amp;#39;stopped&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;username&amp;#39;: &amp;#39;NT AUTHORITY\\LocalService&amp;#39;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I decided to do this mainly because I find pywin32 APIs too low levelish. Having something like this in psutil can be useful to discover and monitor services more easily. The code was implemented in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/803"&gt;PR-803&lt;/a&gt;. The API for querying a service is similar to &lt;tt class="docutils literal"&gt;psutil.Process&lt;/tt&gt;. You can get a reference to a service object by using its name (which is unique for every service) and then use methods like &lt;tt class="docutils literal"&gt;WindowsService.name()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;WindowsService.status()&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;win_service_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;alg&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;alg&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;stopped&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Initially I thought about providing a full set of APIs to handle all aspects of service management, including &lt;tt class="docutils literal"&gt;start()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;stop()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;restart()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;install()&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;uninstall()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;modify()&lt;/tt&gt;. However, I soon realized I would have ended up reimplementing what pywin32 already provides, at the cost of overcrowding the psutil API (see my reasoning &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/d28de253a2e6d7f368e5260d7a4ab14b285c5083/psutil/_pswindows.py#L426"&gt;here&lt;/a&gt;). I think psutil really focuses on monitoring, not on installing and modifying system components, especially something as critical as a Windows service.&lt;/p&gt;
&lt;div class="section" id="considerations-about-windows-services"&gt;
&lt;h2&gt;Considerations about Windows services&lt;a class="headerlink" href="#considerations-about-windows-services" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Typically, a Windows service is an executable (.exe) that runs at system startup and continues running in the background. It is roughly the equivalent of a UNIX init script. All services are controlled by a &amp;quot;manager&amp;quot;, which keeps track of their status and metadata (e.g. description, startup type). It is interesting to note that since (most) services are bound to an executable (and hence a process) you can reference them via their process PID:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;win_service_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;sshd&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;WindowsService(name=&amp;#39;sshd&amp;#39;, display_name=&amp;#39;Open SSH server&amp;#39;) at 38853046&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;1865&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1865&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;psutil.Process(pid=19547, name=&amp;#39;sshd.exe&amp;#39;) at 140461487781328&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;C:\CygWin\bin\sshd&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="other-improvements"&gt;
&lt;h2&gt;Other improvements&lt;a class="headerlink" href="#other-improvements" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;psutil 4.2.0 comes with 2 other enhancements for Linux:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.virtual_memory()&lt;/tt&gt; returns a new &lt;tt class="docutils literal"&gt;shared&lt;/tt&gt; memory field. This is the same value reported by &lt;tt class="docutils literal"&gt;free&lt;/tt&gt; cmdline utility.&lt;/li&gt;
&lt;li&gt;I changed how &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt; was parsed. Instead of reading &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;/proc/{pid}/status&lt;/span&gt;&lt;/tt&gt; line by line I used a regular expression. Here's the speedups:&lt;ul&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.ppid()&lt;/tt&gt; ~20% faster.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.status()&lt;/tt&gt; ~28% faster.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.name()&lt;/tt&gt; ~25% faster.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.num_threads()&lt;/tt&gt; ~20% faster (on Python 3 only; on Python 2 it's a bit slower; I suppose the &lt;tt class="docutils literal"&gt;re&lt;/tt&gt; module received some improvements).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/4jf8tz/psutil_420_windows_services_and_python/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=11700002"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="windows"/><category term="new-api"/><category term="release"/></entry><entry><title>NetBSD support</title><link href="https://gmpy.dev/blog/2016/psutil-netbsd-support" rel="alternate"/><published>2016-02-25T00:00:00+01:00</published><updated>2016-02-25T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-02-25:/blog/2016/psutil-netbsd-support</id><summary type="html">&lt;p&gt;Roughly two months have passed since I last &lt;a class="reference external" href="../2015/psutil-openbsd-support"&gt;announced&lt;/a&gt; that &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; added support for OpenBSD. Today I'm happy to announce that it's the turn of NetBSD! This was contributed by &lt;a class="reference external" href="https://github.com/0-wiz-0"&gt;Thomas Klausner&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/ryoon"&gt;Ryo Onodera&lt;/a&gt; and myself in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/557"&gt;PR-557&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="differences-with-freebsd-and-openbsd"&gt;
&lt;h2&gt;Differences with FreeBSD (and OpenBSD)&lt;a class="headerlink" href="#differences-with-freebsd-and-openbsd" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The NetBSD implementation has limitations similar to …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;Roughly two months have passed since I last &lt;a class="reference external" href="../2015/psutil-openbsd-support"&gt;announced&lt;/a&gt; that &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; added support for OpenBSD. Today I'm happy to announce that it's the turn of NetBSD! This was contributed by &lt;a class="reference external" href="https://github.com/0-wiz-0"&gt;Thomas Klausner&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/ryoon"&gt;Ryo Onodera&lt;/a&gt; and myself in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/557"&gt;PR-557&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="differences-with-freebsd-and-openbsd"&gt;
&lt;h2&gt;Differences with FreeBSD (and OpenBSD)&lt;a class="headerlink" href="#differences-with-freebsd-and-openbsd" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The NetBSD implementation has limitations similar to the ones I encountered with OpenBSD. Again, FreeBSD remains the BSD variant with the best support in terms of kernel functionality.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is not implemented. The kernel provides the necessary pieces, but I haven't done this yet (hopefully later).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.num_ctx_switches()&lt;/tt&gt;'s &lt;tt class="docutils literal"&gt;involuntary&lt;/tt&gt; field is always 0. The &lt;tt class="docutils literal"&gt;kinfo_proc()&lt;/tt&gt; syscall provides this info, but it's always set to 0.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.cpu_affinity()&lt;/tt&gt; (get and set) is not supported.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.cpu_count(logical=False)&lt;/tt&gt; always returns &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the rest: it is all there. All memory, disk, network and process APIs are fully supported and functioning.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-enhancements-available-in-this-psutil-release"&gt;
&lt;h2&gt;Other enhancements available in this psutil release&lt;a class="headerlink" href="#other-enhancements-available-in-this-psutil-release" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Besides NetBSD support, this release has a couple of interesting enhancements:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/708"&gt;#708&lt;/a&gt;: [Linux] &lt;tt class="docutils literal"&gt;psutil.net_connections()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.connections()&lt;/tt&gt; can be up to 3x faster when there are many connections.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/718"&gt;#718&lt;/a&gt;: &lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt; is now thread safe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can read the rest in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#id23"&gt;changelog&lt;/a&gt;, as usual.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="move-to-prague"&gt;
&lt;h2&gt;Move to Prague&lt;a class="headerlink" href="#move-to-prague" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As a personal note I'd like to add that I'm currently in Prague (Czech Republic) and I'm thinking about moving down here for a while.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/4131q2/netbsd_support_for_psutil/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=10909101"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="bsd"/><category term="new-platform"/><category term="community"/><category term="release"/></entry><entry><title>Real process memory and environ in Python</title><link href="https://gmpy.dev/blog/2016/real-process-memory-and-environ-in-python" rel="alternate"/><published>2016-02-17T00:00:00+01:00</published><updated>2016-02-17T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-02-17:/blog/2016/real-process-memory-and-environ-in-python</id><summary type="html">&lt;p&gt;psutil 4.0.0 is out, with some interesting news about process memory metrics. I'll get straight to the point and describe what's new.&lt;/p&gt;
&lt;div class="section" id="real-process-memory-info"&gt;
&lt;h2&gt;&amp;quot;Real&amp;quot; process memory info&lt;a class="headerlink" href="#real-process-memory-info" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Determining how much memory a process &lt;strong&gt;really&lt;/strong&gt; uses is not an easy matter (see &lt;a class="reference external" href="https://lwn.net/Articles/230975/"&gt;this&lt;/a&gt; and &lt;a class="reference external" href="http://bmaurer.blogspot.it/2006/03/memory-usage-with-smaps.html"&gt;this&lt;/a&gt;). RSS (Resident Set Size), which …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;psutil 4.0.0 is out, with some interesting news about process memory metrics. I'll get straight to the point and describe what's new.&lt;/p&gt;
&lt;div class="section" id="real-process-memory-info"&gt;
&lt;h2&gt;&amp;quot;Real&amp;quot; process memory info&lt;a class="headerlink" href="#real-process-memory-info" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Determining how much memory a process &lt;strong&gt;really&lt;/strong&gt; uses is not an easy matter (see &lt;a class="reference external" href="https://lwn.net/Articles/230975/"&gt;this&lt;/a&gt; and &lt;a class="reference external" href="http://bmaurer.blogspot.it/2006/03/memory-usage-with-smaps.html"&gt;this&lt;/a&gt;). RSS (Resident Set Size), which most people rely on, is misleading because it includes both memory unique to the process and memory shared with others. What's more interesting for profiling is the memory that would be freed if the process were terminated &lt;strong&gt;right now&lt;/strong&gt;. In the Linux world this is called USS (Unique Set Size), the major feature introduced in psutil 4.0.0 (not only for Linux but also for Windows and macOS).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="uss-memory"&gt;
&lt;h2&gt;USS memory&lt;a class="headerlink" href="#uss-memory" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The USS (Unique Set Size) is the memory unique to a process, that would be freed if the process were terminated right now. On Linux it can be determined by parsing the &amp;quot;private&amp;quot; blocks in &lt;tt class="docutils literal"&gt;/proc/PID/smaps&lt;/tt&gt;. The Firefox team pushed this further and got it working on &lt;a class="reference external" href="https://dxr.mozilla.org/mozilla-central/rev/aa90f482e16db77cdb7dea84564ea1cbd8f7f6b3/xpcom/base/nsMemoryReporterManager.cpp"&gt;macOS and Windows&lt;/a&gt; too.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_full_info&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;pfullmem(rss=101990, vms=521888, shared=38804, text=28200, lib=0, data=59672, dirty=0, uss=81623, pss=91788, swap=0)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="pss-and-swap"&gt;
&lt;h2&gt;PSS and swap&lt;a class="headerlink" href="#pss-and-swap" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On Linux there are two additional metrics that can also be determined via &lt;tt class="docutils literal"&gt;/proc/PID/smaps&lt;/tt&gt;: PSS and swap.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;pss&lt;/tt&gt;, aka &amp;quot;Proportional Set Size&amp;quot;, represents the amount of memory shared with other processes, accounted so that the amount is divided evenly between the processes that share it. I.e. if a process has 10 MBs all to itself (USS) and 10 MBs shared with another process, its PSS will be 15 MBs.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;swap&lt;/tt&gt; is simply the amount of memory that has been swapped out to disk. With &lt;tt class="docutils literal"&gt;Process.memory_full_info()&lt;/tt&gt; it is possible to implement a tool like &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-4.0.0/scripts/procsmem.py"&gt;procsmem.py&lt;/a&gt;, similar to &lt;a class="reference external" href="https://www.selenic.com/smem/"&gt;smem&lt;/a&gt; on Linux, which provides a list of processes sorted by &lt;tt class="docutils literal"&gt;uss&lt;/tt&gt;. It's interesting to see how &lt;tt class="docutils literal"&gt;rss&lt;/tt&gt; differs from &lt;tt class="docutils literal"&gt;uss&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;~/svn/psutil$ ./scripts/procsmem.py
PID     User    Cmdline                            USS     PSS    Swap     RSS
==============================================================================
...
3986    giampao /usr/bin/python3 /usr/bin/indi   15.3M   16.6M      0B   25.6M
3906    giampao /usr/lib/ibus/ibus-ui-gtk3       17.6M   18.1M      0B   26.7M
3991    giampao python /usr/bin/hp-systray -x    19.0M   23.3M      0B   40.7M
3830    giampao /usr/bin/ibus-daemon --daemoni   19.0M   19.0M      0B   21.4M
20529   giampao /opt/sublime_text/plugin_host    19.9M   20.1M      0B   22.0M
3990    giampao nautilus -n                      20.6M   29.9M      0B   50.2M
3898    giampao /usr/lib/unity/unity-panel-ser   27.1M   27.9M      0B   37.7M
4176    giampao /usr/lib/evolution/evolution-c   35.7M   36.2M      0B   41.5M
20712   giampao /usr/bin/python -B /home/giamp   45.6M   45.9M      0B   49.4M
3880    giampao /usr/lib/x86_64-linux-gnu/hud/   51.6M   52.7M      0B   61.3M
20513   giampao /opt/sublime_text/sublime_text   65.8M   73.0M      0B   87.9M
3976    giampao compiz                          115.0M  117.0M      0B  130.9M
32486   giampao skype                           145.1M  147.5M      0B  149.6M
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="implementation"&gt;
&lt;h2&gt;Implementation&lt;a class="headerlink" href="#implementation" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To get these values (&lt;tt class="docutils literal"&gt;uss&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;pss&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;swap&lt;/tt&gt;) we need to walk the whole process address space. This usually requires higher privileges and is considerably slower than &lt;tt class="docutils literal"&gt;Process.memory_info()&lt;/tt&gt;, which is probably why tools like &lt;tt class="docutils literal"&gt;ps&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;top&lt;/tt&gt; show RSS/VMS instead of USS. A big thanks goes to the Mozilla team for figuring this out on Windows and macOS, and to &lt;a class="reference external" href="https://github.com/EricRahm"&gt;Eric Rahm&lt;/a&gt; who put the psutil PRs together (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/744"&gt;PR-744&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/745"&gt;PR-745&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/746"&gt;PR-746&lt;/a&gt;). If you don't use Python and want to port the code to another language, here are the interesting parts:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/42b34049cf96e845b6423db91f991849a2f87578/psutil/_pslinux.py#L1026"&gt;Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/50fd31a4eaca3e24905b96d587fd08bcf313fc6b/psutil/_psutil_osx.c#L568"&gt;macOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/50fd31a4eaca3e24905b96d587fd08bcf313fc6b/psutil/_psutil_windows.c#L811"&gt;Windows&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="memory-type-percent"&gt;
&lt;h2&gt;Memory type percent&lt;a class="headerlink" href="#memory-type-percent" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After reorganizing the process memory APIs (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/744"&gt;PR-744&lt;/a&gt;), I added a new &lt;tt class="docutils literal"&gt;memtype&lt;/tt&gt; parameter to &lt;tt class="docutils literal"&gt;Process.memory_percent()&lt;/tt&gt;. You can now compare a specific memory type (not only RSS) against the total physical memory. E.g.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_percent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;pss&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;0.06877466326787016&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="process-environ"&gt;
&lt;h2&gt;Process environ&lt;a class="headerlink" href="#process-environ" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The second biggest improvement in psutil 4.0.0 is the ability to read a process's environment variables. This opens up interesting possibilities for process recognition and monitoring. For instance, you can start a process with a custom environment variable, then iterate over all processes to find the one of interest:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;MYAPP&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Process environ was a long-standing issue (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/52"&gt;#52&lt;/a&gt;, from 2009) that I gave up on because the Windows implementation only worked for the current process. &lt;a class="reference external" href="https://github.com/fbenkstein"&gt;Frank Benkstein&lt;/a&gt; solved that (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/747"&gt;PR-747&lt;/a&gt;), and it now works on Linux, Windows and macOS for all processes (you may still hit &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt; for processes owned by another user):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pprint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pp&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="go"&gt;{...&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;CLUTTER_IM_MODULE&amp;#39;: &amp;#39;xim&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;COLORTERM&amp;#39;: &amp;#39;gnome-terminal&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;COMPIZ_BIN_PATH&amp;#39;: &amp;#39;/usr/bin/&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;HOME&amp;#39;: &amp;#39;/home/giampaolo&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;PWD&amp;#39;: &amp;#39;/home/giampaolo/svn/psutil&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;  }&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that the resulting dict usually doesn't reflect changes made after the process started (e.g. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;os.environ['MYAPP']&lt;/span&gt; = '1'&lt;/tt&gt;). Again, for anyone porting this to other languages, here are the interesting parts:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/50fd31a4eaca3e24905b96d587fd08bcf313fc6b/psutil/_pslinux.py#L928"&gt;Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/50fd31a4eaca3e24905b96d587fd08bcf313fc6b/psutil/arch/osx/process_info.c#L241"&gt;macOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/747"&gt;Windows&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="extended-disk-io-stats"&gt;
&lt;h2&gt;Extended disk IO stats&lt;a class="headerlink" href="#extended-disk-io-stats" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;psutil.disk_io_counters()&lt;/tt&gt; now reports additional metrics on Linux and FreeBSD:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;busy_time&lt;/tt&gt;: the time spent doing actual I/Os (in milliseconds).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;read_merged_count&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;write_merged_count&lt;/tt&gt; (Linux only): the number of merged reads and writes (see the &lt;a class="reference external" href="https://www.kernel.org/doc/Documentation/iostats.txt"&gt;iostats&lt;/a&gt; doc).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These give a better picture of actual disk utilization (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/756"&gt;#756&lt;/a&gt;), similar to the &lt;tt class="docutils literal"&gt;iostat&lt;/tt&gt; command on Linux.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="os-constants"&gt;
&lt;h2&gt;OS constants&lt;a class="headerlink" href="#os-constants" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Given the growing number of platform-specific metrics, I added a set of constants to tell which platform you're on: &lt;tt class="docutils literal"&gt;psutil.LINUX&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;psutil.WINDOWS&lt;/tt&gt;, etc.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-fixes"&gt;
&lt;h2&gt;Other fixes&lt;a class="headerlink" href="#other-fixes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The complete list of changes is available in the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#id22"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="porting-code"&gt;
&lt;h2&gt;Porting code&lt;a class="headerlink" href="#porting-code" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Since 4.0.0 is a major version, I took the chance to (lightly) change / break some APIs.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.memory_info()&lt;/tt&gt; no longer returns just an (&lt;tt class="docutils literal"&gt;rss&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;vms&lt;/tt&gt;) namedtuple. It returns a variable-length namedtuple that varies by platform (&lt;tt class="docutils literal"&gt;rss&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;vms&lt;/tt&gt; are always present, even on Windows). Essentially the same result as the old &lt;tt class="docutils literal"&gt;Process.memory_info_ex()&lt;/tt&gt;. This shouldn't break your code unless you were doing &lt;tt class="docutils literal"&gt;rss, vms = p.memory_info()&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.memory_info_ex()&lt;/tt&gt; is deprecated. It still works as an alias for &lt;tt class="docutils literal"&gt;Process.memory_info()&lt;/tt&gt;, issuing a &lt;tt class="docutils literal"&gt;DeprecationWarning&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.disk_io_counters()&lt;/tt&gt; on NetBSD and OpenBSD no longer returns &lt;tt class="docutils literal"&gt;write_count&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;read_count&lt;/tt&gt; because the kernel doesn't provide them (we were returning the busy time instead). Should be a small issue given NetBSD and OpenBSD support is very recent.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/469p2c/psutil_400_real_process_memory_info_and_process/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=11119298"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="memory"/><category term="new-api"/><category term="compatibility"/><category term="release"/><category term="community"/><category term="linux"/></entry><entry><title>How to always execute exit functions in Python</title><link href="https://gmpy.dev/blog/2016/how-to-always-execute-exit-functions-in-python" rel="alternate"/><published>2016-02-12T00:00:00+01:00</published><updated>2016-02-13T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2016-02-12:/blog/2016/how-to-always-execute-exit-functions-in-python</id><summary type="html">&lt;p&gt;&lt;em&gt;...or why atexit.register() and signal.signal() are evil&lt;/em&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;strong&gt;UPDATE (2016-02-13)&lt;/strong&gt;: this recipe no longer handles &lt;tt class="docutils literal"&gt;SIGINT&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;SIGQUIT&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;SIGABRT&lt;/tt&gt; as aliases for &amp;quot;application exit&amp;quot; because it was a &lt;a class="reference external" href="https://mail.python.org/pipermail/python-ideas/2016-February/038471.html"&gt;bad idea&lt;/a&gt;. It only handles &lt;tt class="docutils literal"&gt;SIGTERM&lt;/tt&gt;. Also it no longer supports Windows because &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt; implementation is &lt;a class="reference external" href="http://bugs.python.org/issue26350"&gt;too different&lt;/a&gt; from POSIX …&lt;/li&gt;&lt;/ul&gt;</summary><content type="html">&lt;p&gt;&lt;em&gt;...or why atexit.register() and signal.signal() are evil&lt;/em&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;strong&gt;UPDATE (2016-02-13)&lt;/strong&gt;: this recipe no longer handles &lt;tt class="docutils literal"&gt;SIGINT&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;SIGQUIT&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;SIGABRT&lt;/tt&gt; as aliases for &amp;quot;application exit&amp;quot; because it was a &lt;a class="reference external" href="https://mail.python.org/pipermail/python-ideas/2016-February/038471.html"&gt;bad idea&lt;/a&gt;. It only handles &lt;tt class="docutils literal"&gt;SIGTERM&lt;/tt&gt;. Also it no longer supports Windows because &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt; implementation is &lt;a class="reference external" href="http://bugs.python.org/issue26350"&gt;too different&lt;/a&gt; from POSIX.*&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Many people erroneously think that any function registered via the &lt;a class="reference external" href="https://docs.python.org/3/library/atexit.html"&gt;atexit module&lt;/a&gt; is guaranteed to always be executed when the program terminates. You may have noticed this is not the case when, for example, you daemonize your app in production then try to stop it or restart it: the cleanup functions will not be executed. This is because functions registered with the atexit module are &lt;strong&gt;not called&lt;/strong&gt; when the program is killed by a signal:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;atexit&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;signal&lt;/span&gt;

&lt;span class="nd"&gt;@atexit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;on exit&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# XXX this never gets printed&lt;/span&gt;

&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that the same thing happens if instead of &lt;a class="reference external" href="https://docs.python.org/3/library/atexit.html#atexit.register"&gt;atexit.register()&lt;/a&gt; we used a &amp;quot;finally&amp;quot; clause. It turns out the correct way to make sure the exit function is always called in case a signal is received is to register it via &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt;. That has a drawback though: in case a third-party module has already registered a function for that signal (&lt;tt class="docutils literal"&gt;SIGTERM&lt;/tt&gt; or whatever), your new function will &lt;strong&gt;overwrite&lt;/strong&gt; the old one:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;signal&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;old&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;old&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# XXX this never gets printed&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;new&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Also, we would still have to use &lt;a class="reference external" href="https://docs.python.org/3/library/atexit.html#atexit.register"&gt;atexit.register()&lt;/a&gt; so that the function is called also on &amp;quot;clean&amp;quot; interpreter exit &lt;span class="strike"&gt;and take into account other signals other than SIGTERM which would cause the process to terminate&lt;/span&gt;. This recipe attempts to address all these issues so that:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;the exit function is always executed &lt;span class="strike"&gt;for all exit signals (SIGTERM, SIGINT, SIGQUIT, SIGABRT)&lt;/span&gt; on &lt;tt class="docutils literal"&gt;SIGTERM&lt;/tt&gt; and on &amp;quot;clean&amp;quot; interpreter exit.&lt;/li&gt;
&lt;li&gt;any exit function(s) previously registered via &lt;a class="reference external" href="https://docs.python.org/3/library/atexit.html#atexit.register"&gt;atexit.register()&lt;/a&gt; or &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt; will be executed as well (after the new one).&lt;/li&gt;
&lt;li&gt;It must be noted that the exit function will never be executed in case of &lt;tt class="docutils literal"&gt;SIGKILL&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;SIGSTOP&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;os._exit()&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="section" id="the-code"&gt;
&lt;h2&gt;The code&lt;a class="headerlink" href="#the-code" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="sd"&gt;Function / decorator which tries very hard to register a function to&lt;/span&gt;
&lt;span class="sd"&gt;be executed at interpreter exit.&lt;/span&gt;

&lt;span class="sd"&gt;Author: Giampaolo Rodola&amp;#39;&lt;/span&gt;
&lt;span class="sd"&gt;License: MIT&lt;/span&gt;
&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;__future__&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;print_function&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;atexit&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;functools&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;signal&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;sys&lt;/span&gt;


&lt;span class="n"&gt;_registered_exit_funs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;_executed_exit_funs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;register_exit_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                      &lt;span class="n"&gt;logfun&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Register a function which will be executed on &amp;quot;normal&amp;quot;&lt;/span&gt;
&lt;span class="sd"&gt;    interpreter exit or in case one of the `signals` is received&lt;/span&gt;
&lt;span class="sd"&gt;    by this process (differently from `atexit.register() &amp;lt;https://docs.python.org/3/library/atexit.html#atexit.register&amp;gt;`__).&lt;/span&gt;
&lt;span class="sd"&gt;    Also, it makes sure to execute any other function which was&lt;/span&gt;
&lt;span class="sd"&gt;    previously registered via signal.signal(). If any, it will be&lt;/span&gt;
&lt;span class="sd"&gt;    executed after our own `fun`.&lt;/span&gt;

&lt;span class="sd"&gt;    Functions which were already registered or executed via this&lt;/span&gt;
&lt;span class="sd"&gt;    function will be ignored.&lt;/span&gt;

&lt;span class="sd"&gt;    Note: there&amp;#39;s no way to escape SIGKILL, SIGSTOP or os._exit(0)&lt;/span&gt;
&lt;span class="sd"&gt;    so don&amp;#39;t bother trying.&lt;/span&gt;

&lt;span class="sd"&gt;    You can use this either as a function or as a decorator:&lt;/span&gt;

&lt;span class="sd"&gt;        @register_exit_fun&lt;/span&gt;
&lt;span class="sd"&gt;        def cleanup():&lt;/span&gt;
&lt;span class="sd"&gt;            pass&lt;/span&gt;

&lt;span class="sd"&gt;        # ...or&lt;/span&gt;

&lt;span class="sd"&gt;        register_exit_fun(cleanup)&lt;/span&gt;

&lt;span class="sd"&gt;    Note about Windows: I tested this some time ago and didn&amp;#39;t work&lt;/span&gt;
&lt;span class="sd"&gt;    exactly the same as on UNIX, then I didn&amp;#39;t care about it&lt;/span&gt;
&lt;span class="sd"&gt;    anymore and didn&amp;#39;t test since then so may not work on Windows.&lt;/span&gt;

&lt;span class="sd"&gt;    Parameters:&lt;/span&gt;

&lt;span class="sd"&gt;    - fun: a callable&lt;/span&gt;
&lt;span class="sd"&gt;    - signals: a list of signals for which this function will be&lt;/span&gt;
&lt;span class="sd"&gt;      executed (default SIGTERM)&lt;/span&gt;
&lt;span class="sd"&gt;    - logfun: a logging function which is called when a signal is&lt;/span&gt;
&lt;span class="sd"&gt;      received. Default: print to standard error. May be set to&lt;/span&gt;
&lt;span class="sd"&gt;      None if no logging is desired.&lt;/span&gt;
&lt;span class="sd"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;stringify_sig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version_info&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;smap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;([(&lt;/span&gt;&lt;span class="nb"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;SIG&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;smap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;fun_wrapper&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_executed_exit_funs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;_executed_exit_funs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;signal_wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;logfun&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;logfun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;signal &lt;/span&gt;&lt;span class="si"&gt;{}&lt;/span&gt;&lt;span class="s2"&gt; received by process with PID &lt;/span&gt;&lt;span class="si"&gt;{}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;stringify_sig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
        &lt;span class="n"&gt;fun_wrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="c1"&gt;# Only return the original signal this process was hit with&lt;/span&gt;
        &lt;span class="c1"&gt;# in case fun returns with no errors, otherwise process will&lt;/span&gt;
        &lt;span class="c1"&gt;# return with sig 1.&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGINT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;KeyboardInterrupt&lt;/span&gt;
            &lt;span class="c1"&gt;# XXX - should we do the same for SIGTERM / SystemExit?&lt;/span&gt;
            &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;register_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;callable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;{!r}&lt;/span&gt;&lt;span class="s2"&gt; is not callable&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# raise exc if obj is not hash-able&lt;/span&gt;

        &lt;span class="n"&gt;signals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Register function for this signal and pop() the previously&lt;/span&gt;
            &lt;span class="c1"&gt;# registered one (if any). This can either be a callable,&lt;/span&gt;
            &lt;span class="c1"&gt;# SIG_IGN (ignore signal) or SIG_DFL (perform default action&lt;/span&gt;
            &lt;span class="c1"&gt;# for signal).&lt;/span&gt;
            &lt;span class="n"&gt;old_handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signal_wrapper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;old_handler&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIG_DFL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIG_IGN&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="c1"&gt;# ...just for extra safety.&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;callable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_handler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                    &lt;span class="k"&gt;continue&lt;/span&gt;
                &lt;span class="c1"&gt;# This is needed otherwise we&amp;#39;ll get a KeyboardInterrupt&lt;/span&gt;
                &lt;span class="c1"&gt;# strace on interpreter exit, even if the process exited&lt;/span&gt;
                &lt;span class="c1"&gt;# with sig 0.&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SIGINT&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt;
                        &lt;span class="n"&gt;old_handler&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_int_handler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                    &lt;span class="k"&gt;continue&lt;/span&gt;
                &lt;span class="c1"&gt;# There was a function which was already registered for this&lt;/span&gt;
                &lt;span class="c1"&gt;# signal. Register it again so it will get executed (after our&lt;/span&gt;
                &lt;span class="c1"&gt;# new fun).&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;old_handler&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_registered_exit_funs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;atexit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;_registered_exit_funs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# This further registration will be executed in case of clean&lt;/span&gt;
        &lt;span class="c1"&gt;# interpreter exit (no signals received).&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_registered_exit_funs&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;atexit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun_wrapper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;_registered_exit_funs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# This piece of machinery handles 3 usage cases. register_exit_fun()&lt;/span&gt;
    &lt;span class="c1"&gt;# used as:&lt;/span&gt;
    &lt;span class="c1"&gt;# - a function&lt;/span&gt;
    &lt;span class="c1"&gt;# - a decorator without parentheses&lt;/span&gt;
    &lt;span class="c1"&gt;# - a decorator with parentheses&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nd"&gt;@functools&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wraps&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;register_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;register_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="usage"&gt;
&lt;h2&gt;Usage&lt;a class="headerlink" href="#usage" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As a function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;cleanup&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;register_exit_fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a decorator:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nd"&gt;@register_exit_fun&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;cleanup&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="unit-tests"&gt;
&lt;h2&gt;Unit tests&lt;a class="headerlink" href="#unit-tests" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This recipe is hosted on &lt;a class="reference external" href="https://code.activestate.com/recipes/580672-register-exit-function/"&gt;ActiveState&lt;/a&gt; and has a full set of unittests. It works with Python 2 and 3.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="notes-about-windows"&gt;
&lt;h2&gt;Notes about Windows&lt;a class="headerlink" href="#notes-about-windows" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span class="strike"&gt;On Windows signals are only partially supported meaning a function which was previously registered via signal.signal() will be executed only on interpreter exit, but not if the process receives a signal. Apparently this is a limitation either of Windows or the signal module.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because of how different &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt; behaves on Windows, this code is UNIX only, see &lt;a class="reference external" href="https://bugs.python.org/issue26350"&gt;BPO-26350&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="proposal-for-stdlib-inclusion"&gt;
&lt;h2&gt;Proposal for stdlib inclusion&lt;a class="headerlink" href="#proposal-for-stdlib-inclusion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The fact that atexit module &lt;a class="reference external" href="http://stackoverflow.com/a/2546397/376587"&gt;does not handle signals&lt;/a&gt; and that &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt; overwrites previously registered handlers is unfortunate. It is also &lt;a class="reference external" href="http://ambracode.com/index/show/92669"&gt;confusing&lt;/a&gt; because it is not immediately clear which one you are supposed to use (and it turns out you're supposed to use both). Most of the time you have no idea (or don't care) that you're overwriting another exit function. As a user, I would just want to execute an exit function, no matter what, possibly without messing with whatever a module I've previously imported has done with &lt;a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal"&gt;signal.signal()&lt;/a&gt;. To me this suggests there could be space for something like &lt;a class="reference external" href="https://mail.python.org/pipermail/python-ideas/2016-February/038431.html"&gt;atexit.register_w_signals&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="external-discussions"&gt;
&lt;h2&gt;External discussions&lt;a class="headerlink" href="#external-discussions" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/45fvd9/how_to_always_execute_exit_functions_in_python/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=11088938"&gt;Hacker news&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="python"/><category term="recipe"/></entry><entry><title>OpenBSD support</title><link href="https://gmpy.dev/blog/2015/psutil-openbsd-support" rel="alternate"/><published>2015-11-25T00:00:00+01:00</published><updated>2015-11-25T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2015-11-25:/blog/2015/psutil-openbsd-support</id><summary type="html">&lt;p&gt;Starting from version 3.3.0 (released just now) &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; officially supports OpenBSD. This was contributed by &lt;a class="reference external" href="https://github.com/landryb"&gt;Landry Breuil&lt;/a&gt; and myself in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/615"&gt;PR-615&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="differences-with-freebsd"&gt;
&lt;h2&gt;Differences with FreeBSD&lt;a class="headerlink" href="#differences-with-freebsd" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As expected, the OpenBSD implementation is very similar to FreeBSD's, so I merged most of it into a single C file (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-3.3.0/psutil/_psutil_bsd.c"&gt;_psutil_bsd.c&lt;/a&gt;), with …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;Starting from version 3.3.0 (released just now) &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; officially supports OpenBSD. This was contributed by &lt;a class="reference external" href="https://github.com/landryb"&gt;Landry Breuil&lt;/a&gt; and myself in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/615"&gt;PR-615&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="differences-with-freebsd"&gt;
&lt;h2&gt;Differences with FreeBSD&lt;a class="headerlink" href="#differences-with-freebsd" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As expected, the OpenBSD implementation is very similar to FreeBSD's, so I merged most of it into a single C file (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-3.3.0/psutil/_psutil_bsd.c"&gt;_psutil_bsd.c&lt;/a&gt;), with 2 separate files (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-3.3.0/psutil/arch/bsd/freebsd.c"&gt;freebsd.c&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-3.3.0/psutil/arch/bsd/openbsd.c"&gt;openbsd.c&lt;/a&gt;) for the parts that differ. Here are the functional differences with FreeBSD:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.memory_maps()&lt;/tt&gt; is not implemented. The kernel provides the necessary pieces but I haven't done this yet (hopefully later).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.num_ctx_switches()&lt;/tt&gt;'s &lt;tt class="docutils literal"&gt;involuntary&lt;/tt&gt; field is always 0. &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/fc1e59d08c968898c2ede425a621b62ccf44681c/psutil/_psutil_bsd.c#L335"&gt;kinfo_proc&lt;/a&gt; provides this info but it is always set to 0.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.cpu_affinity()&lt;/tt&gt; (get and set) is not supported.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.exe()&lt;/tt&gt; is determined by inspecting the command line, so it may not always be available (returns &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;).&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.swap_memory()&lt;/tt&gt; &lt;tt class="docutils literal"&gt;sin&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;sout&lt;/tt&gt; (swap in and swap out) values are not available, and are therefore always set to 0.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.cpu_count(logical=False)&lt;/tt&gt; always returns &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As with FreeBSD, &lt;tt class="docutils literal"&gt;Process.open_files()&lt;/tt&gt; can't return file paths (FreeBSD can sometimes). Otherwise everything is there and I'm satisfied with the result.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="considerations-about-bsd-platforms"&gt;
&lt;h2&gt;Considerations about BSD platforms&lt;a class="headerlink" href="#considerations-about-bsd-platforms" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;psutil has supported FreeBSD since the beginning (year 2009). At the time, it made sense to prefer it as the preferred BSD variant as it's the &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Comparison_of_BSD_operating_systems#Popularity"&gt;most popular&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Compared to FreeBSD, OpenBSD appears to be more &amp;quot;minimal&amp;quot;, both in terms of kernel facilities and the number of CLI tools available. One thing I particularly appreciate about FreeBSD is that the source code for all CLI tools is available under &lt;tt class="docutils literal"&gt;/usr/src&lt;/tt&gt;, which was a big help when implementing psutil APIs.&lt;/p&gt;
&lt;p&gt;OpenBSD source code is &lt;a class="reference external" href="https://github.com/openbsd/src"&gt;also available&lt;/a&gt;, but it uses CVS and I am not sure it includes the source code for all CLI tools.&lt;/p&gt;
&lt;p&gt;There are still two more BSD variants worth supporting: NetBSD and DragonFlyBSD (in this order). About a year ago, someone provided a &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/429"&gt;patch&lt;/a&gt; adding basic NetBSD support, so that will likely happen sooner or later.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="other-enhancements-available-in-this-release"&gt;
&lt;h2&gt;Other enhancements available in this release&lt;a class="headerlink" href="#other-enhancements-available-in-this-release" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The only other enhancement is &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/558"&gt;#558&lt;/a&gt;, which allows specifying a different location for the &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt; filesystem on Linux.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="discussion"&gt;
&lt;h2&gt;Discussion&lt;a class="headerlink" href="#discussion" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.reddit.com/r/Python/comments/3u8wm3/openbsd_support_for_psutil_330/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://news.ycombinator.com/item?id=10628726"&gt;Hacker News&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="bsd"/><category term="new-platform"/><category term="community"/><category term="release"/></entry><entry><title>Reimplementing ifconfig in Python</title><link href="https://gmpy.dev/blog/2015/psutil-30-reimplement-ifconfig-in-python" rel="alternate"/><published>2015-06-13T00:00:00+02:00</published><updated>2015-06-13T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2015-06-13:/blog/2015/psutil-30-reimplement-ifconfig-in-python</id><summary type="html">&lt;p&gt;Here we are. It's been a long time since my last blog post and my last &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; release. The reason? I've been travelling! I mean... a lot. I've spent 3 months in Berlin, 3 weeks in Japan and 2 months in New York City. While I was there I finally …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Here we are. It's been a long time since my last blog post and my last &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;psutil&lt;/a&gt; release. The reason? I've been travelling! I mean... a lot. I've spent 3 months in Berlin, 3 weeks in Japan and 2 months in New York City. While I was there I finally had the chance to meet my friend &lt;a class="reference external" href="http://jayloden.com/software.htm"&gt;Jay Loden&lt;/a&gt; in person. We originally started working on psutil together &lt;a class="reference external" href="https://groups.google.com/forum/#!topic/psutil-dev/fj8DQ3lGFH4"&gt;7 years ago&lt;/a&gt;.&lt;/p&gt;
&lt;div&gt;
    &lt;a href="/images/me-with-jay.jpg"&gt;
    &lt;img src="/images/me-with-jay.jpg" style="width:750px" /&gt;
    &lt;/a&gt;
&lt;/div&gt;&lt;p&gt;Back then I didn't know any C (and I'm still a terrible C developer), so he was crucial in developing the initial psutil skeleton, including macOS and Windows support. Needless to say that this release builds on that work.&lt;/p&gt;
&lt;div class="section" id="net-if-addrs"&gt;
&lt;h2&gt;net_if_addrs()&lt;a class="headerlink" href="#net-if-addrs" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;We're now able to list network interface addresses similarly to the &lt;tt class="docutils literal"&gt;ifconfig&lt;/tt&gt; command on UNIX:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pprint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;net_if_addrs&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;ethernet0&amp;#39;: [snic(family=&amp;lt;AddressFamily.AF_INET: 2&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                    address=&amp;#39;10.0.0.4&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    netmask=&amp;#39;255.0.0.0&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    broadcast=&amp;#39;10.255.255.255&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt;               snic(family=&amp;lt;AddressFamily.AF_PACKET: 17&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                    address=&amp;#39;9c:eb:e8:0b:05:1f&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    netmask=None,&lt;/span&gt;
&lt;span class="go"&gt;                    broadcast=&amp;#39;ff:ff:ff:ff:ff:ff&amp;#39;)],&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;localhost&amp;#39;: [snic(family=&amp;lt;AddressFamily.AF_INET: 2&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                    address=&amp;#39;127.0.0.1&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    netmask=&amp;#39;255.0.0.0&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    broadcast=&amp;#39;127.0.0.1&amp;#39;),&lt;/span&gt;
&lt;span class="go"&gt;               snic(family=&amp;lt;AddressFamily.AF_PACKET: 17&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                    address=&amp;#39;00:00:00:00:00:00&amp;#39;,&lt;/span&gt;
&lt;span class="go"&gt;                    netmask=None,&lt;/span&gt;
&lt;span class="go"&gt;                    broadcast=&amp;#39;00:00:00:00:00:00&amp;#39;)]}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is limited to &lt;tt class="docutils literal"&gt;AF_INET&lt;/tt&gt; (IPv4), &lt;tt class="docutils literal"&gt;AF_INET6&lt;/tt&gt; (IPv6) and &lt;tt class="docutils literal"&gt;AF_LINK&lt;/tt&gt; (Ethernet) address families. If you want something more powerful (e.g. &lt;tt class="docutils literal"&gt;AF_BLUETOOTH&lt;/tt&gt;) you can take a look at the &lt;a class="reference external" href="https://pypi.python.org/pypi/netifaces/"&gt;netifaces&lt;/a&gt; extension. If you want to see how this is implemented, here's the code for POSIX and Windows:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_posix.c#L151"&gt;POSIX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_windows.c#L2907"&gt;Windows&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="net-if-stats"&gt;
&lt;h2&gt;net_if_stats()&lt;a class="headerlink" href="#net-if-stats" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This new function returns information about network interface cards:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pprint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;net_if_stats&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="go"&gt;{&amp;#39;ethernet0&amp;#39;: snicstats(isup=True,&lt;/span&gt;
&lt;span class="go"&gt;                        duplex=&amp;lt;NicDuplex.NIC_DUPLEX_FULL: 2&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                        speed=100,&lt;/span&gt;
&lt;span class="go"&gt;                        mtu=1500),&lt;/span&gt;
&lt;span class="go"&gt; &amp;#39;localhost&amp;#39;: snicstats(isup=True,&lt;/span&gt;
&lt;span class="go"&gt;                        duplex=&amp;lt;NicDuplex.NIC_DUPLEX_UNKNOWN: 0&amp;gt;,&lt;/span&gt;
&lt;span class="go"&gt;                        speed=0,&lt;/span&gt;
&lt;span class="go"&gt;                        mtu=65536)}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The implementation on each platform:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_windows.c#L3057"&gt;Windows&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_linux.c#L474"&gt;Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_posix.c#L229"&gt;macOS &amp;amp; FreeBSD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/39161251010503d6b087807c473f4fb648dfcbce/psutil/_psutil_sunos.c#L1153"&gt;SunOS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="also-in-3-0"&gt;
&lt;h2&gt;Also in 3.0&lt;a class="headerlink" href="#also-in-3-0" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Beyond the network-interface APIs, psutil 3.0 ships a few other notable changes.&lt;/p&gt;
&lt;p&gt;Several integer/string constants (&lt;tt class="docutils literal"&gt;IOPRIO_CLASS_*&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;NIC_DUPLEX_*&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;*_PRIORITY_CLASS&lt;/tt&gt;) now return &lt;tt class="docutils literal"&gt;enum&lt;/tt&gt; values on Python 3.4+.&lt;/p&gt;
&lt;p&gt;Support for zombie processes on UNIX was broken. Covered in a &lt;a class="reference external" href="zombie-processes"&gt;separate post&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="removal-of-deprecated-apis"&gt;
&lt;h2&gt;Removal of deprecated APIs&lt;a class="headerlink" href="#removal-of-deprecated-apis" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All aliases deprecated in the &lt;a class="reference external" href="../2014/psutil-20-api-redesign"&gt;psutil 2.0 porting guide&lt;/a&gt; (January 2014) are gone. For the full list see the &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#id23"&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="final-words"&gt;
&lt;h2&gt;Final words&lt;a class="headerlink" href="#final-words" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I must say I'm pretty satisfied with how psutil is evolving and with the enjoyment I still get every time I work on it. It now gets almost &lt;a class="reference external" href="https://pypi.python.org/pypi/psutil#downloads"&gt;800,000 downloads a month&lt;/a&gt;, which is quite remarkable for a Python library.&lt;/p&gt;
&lt;p&gt;At this point, I consider psutil almost &amp;quot;complete&amp;quot; feature-wise, meaning I'm starting to run out of ideas for what to add next (see &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/v3.0.0/TODO"&gt;TODO&lt;/a&gt;). Going forward, development will likely focus on supporting more exotic platforms (&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/562"&gt;OpenBSD #562&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/pull/557"&gt;NetBSD PR-557&lt;/a&gt;, &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/355"&gt;Android #355&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;There have also been discussions on the python-ideas mailing list about &lt;a class="reference external" href="https://mail.python.org/pipermail//python-ideas/2014-October/029835.html"&gt;including psutil in the Python stdlib&lt;/a&gt;, but even if that happens, it's still a long way off, as it would require a significant time investment that I currently don't have.&lt;/p&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="personal"/><category term="new-api"/><category term="compatibility"/><category term="featured"/><category term="release"/></entry><entry><title>Proper zombie process handling</title><link href="https://gmpy.dev/blog/2015/zombie-processes" rel="alternate"/><published>2015-06-13T00:00:00+02:00</published><updated>2015-06-13T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2015-06-13:/blog/2015/zombie-processes</id><summary type="html">&lt;p&gt;This is part of the psutil 3.0 release (see the full &lt;a class="reference external" href="psutil-30-reimplement-ifconfig-in-python"&gt;release notes&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Except on Linux and Windows (which does not have them), support for &lt;a class="reference external" href="http://askubuntu.com/a/48625"&gt;zombie processes&lt;/a&gt; was broken. The full story is in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/428"&gt;#428&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="the-problem"&gt;
&lt;h2&gt;The problem&lt;a class="headerlink" href="#the-problem" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Say you create a zombie process and instantiate a &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; for it …&lt;/p&gt;&lt;/div&gt;</summary><content type="html">&lt;p&gt;This is part of the psutil 3.0 release (see the full &lt;a class="reference external" href="psutil-30-reimplement-ifconfig-in-python"&gt;release notes&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Except on Linux and Windows (which does not have them), support for &lt;a class="reference external" href="http://askubuntu.com/a/48625"&gt;zombie processes&lt;/a&gt; was broken. The full story is in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/428"&gt;#428&lt;/a&gt;.&lt;/p&gt;
&lt;div class="section" id="the-problem"&gt;
&lt;h2&gt;The problem&lt;a class="headerlink" href="#the-problem" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Say you create a zombie process and instantiate a &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; for it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;create_zombie&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# the zombie&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# child exits immediately&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# parent does NOT call wait()&lt;/span&gt;

&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_zombie&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Up until psutil 2.X, every time you tried to query it you'd get a &lt;tt class="docutils literal"&gt;NoSuchProcess&lt;/tt&gt; exception:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  File &lt;span class="nb"&gt;&amp;quot;psutil/__init__.py&amp;quot;&lt;/span&gt;, line &lt;span class="m"&gt;374&lt;/span&gt;, in &lt;span class="n"&gt;_init&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;NoSuchProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="gr"&gt;psutil.NoSuchProcess&lt;/span&gt;: &lt;span class="n"&gt;no process found with pid 123&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This was misleading, because the PID technically still existed:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Depending on the platform, some process information could still be retrieved:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cmdline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="go"&gt;[&amp;#39;python&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Worst of all, &lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt; didn't return zombies at all. That was a real problem, because identifying them is a legitimate use case: a zombie usually indicates a bug where a parent process spawns a child, kills it, but never calls &lt;tt class="docutils literal"&gt;wait()&lt;/tt&gt; to reap it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-changed"&gt;
&lt;h2&gt;What changed&lt;a class="headerlink" href="#what-changed" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;A new &lt;tt class="docutils literal"&gt;ZombieProcess&lt;/tt&gt; exception is raised whenever a process cannot be queried because it is a zombie.&lt;/li&gt;
&lt;li&gt;It replaces &lt;tt class="docutils literal"&gt;NoSuchProcess&lt;/tt&gt;, which was incorrect and misleading.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;ZombieProcess&lt;/tt&gt; inherits from &lt;tt class="docutils literal"&gt;NoSuchProcess&lt;/tt&gt;, so existing code keeps working.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.process_iter()&lt;/tt&gt; now correctly includes zombie processes, so you can reliably identify them:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;

&lt;span class="n"&gt;zombies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STATUS_ZOMBIE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;zombies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NoSuchProcess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="api-design"/><category term="compatibility"/></entry><entry><title>Windows wheels available in psutil 2.1.2</title><link href="https://gmpy.dev/blog/2014/windows-wheels-available-in-psutil-212" rel="alternate"/><published>2014-09-21T00:00:00+02:00</published><updated>2014-09-21T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-09-21:/blog/2014/windows-wheels-available-in-psutil-212</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.1.2 is out. This release has been cooking for a while now, and that's because I've been travelling for the past 3 months between Spain, Japan and Germany. Hopefully I will be staying in Berlin for a while now, so I will have more time to dedicate …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.1.2 is out. This release has been cooking for a while now, and that's because I've been travelling for the past 3 months between Spain, Japan and Germany. Hopefully I will be staying in Berlin for a while now, so I will have more time to dedicate to the project. The main new &amp;quot;feature&amp;quot; of this release is that other than the exe files, Windows users can now also benefit from &lt;a class="reference external" href="http://pythonwheels.com/"&gt;Python wheels&lt;/a&gt; (full story is &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/505"&gt;here&lt;/a&gt;) which are available on PYPI. Frankly I don't know much about the new wheels packaging system but long story short is that Windows users can now install psutil via pip and therefore also include it as a dependency in requirements.txt. Other than this 2.1.2 can basically be considered a bug-fix release, including some important fixes amongst which:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/506"&gt;#506&lt;/a&gt;: restored Python 2.4 compatibility&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/340"&gt;#340&lt;/a&gt;: &lt;cite&gt;Process.get_open_files()&lt;/cite&gt; no longer hangs on Windows (this was a very old and high-priority issue)&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/501"&gt;#501&lt;/a&gt;: &lt;cite&gt;disk_io_counters()&lt;/cite&gt; may return negative values on Windows&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/504"&gt;#504&lt;/a&gt;: (Linux) couldn't build RPM packages via setup.py&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The list of all fixes can be found &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/changelog.html"&gt;here&lt;/a&gt;. For the next release I plan to drop support for Python 2.4 and 2.5 and hopefully add network interfaces information similarly to &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/376"&gt;ifconfig&lt;/a&gt;.&lt;/p&gt;
</content><category term="psutil"/><category term="python"/><category term="wheels"/><category term="release"/><category term="personal"/></entry><entry><title>Python and sendfile</title><link href="https://gmpy.dev/blog/2014/python-and-sendfile" rel="alternate"/><published>2014-06-13T00:00:00+02:00</published><updated>2014-06-13T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-06-13:/blog/2014/python-and-sendfile</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="http://linux.die.net/man/2/sendfile"&gt;sendfile(2)&lt;/a&gt; is a UNIX system call which provides a &amp;quot;zero-copy&amp;quot; way of copying data from one file descriptor (a file) to another (a socket). Because this copying is done entirely within the kernel, &lt;cite&gt;sendfile(2)&lt;/cite&gt; is more efficient than the combination of &lt;cite&gt;file.read()&lt;/cite&gt; and &lt;cite&gt;socket.send()&lt;/cite&gt;, which requires …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="http://linux.die.net/man/2/sendfile"&gt;sendfile(2)&lt;/a&gt; is a UNIX system call which provides a &amp;quot;zero-copy&amp;quot; way of copying data from one file descriptor (a file) to another (a socket). Because this copying is done entirely within the kernel, &lt;cite&gt;sendfile(2)&lt;/cite&gt; is more efficient than the combination of &lt;cite&gt;file.read()&lt;/cite&gt; and &lt;cite&gt;socket.send()&lt;/cite&gt;, which requires transferring data to and from user space. This copying of the data twice imposes some performance and resource penalties which the &lt;cite&gt;sendfile(2)&lt;/cite&gt; syscall avoids; it also results in a single system call (and thus only one context switch), rather than the series of &lt;a class="reference external" href="http://linux.die.net/man/2/read"&gt;read(2)&lt;/a&gt; / &lt;a class="reference external" href="http://linux.die.net/man/2/write"&gt;write(2)&lt;/a&gt; system calls (each system call requiring a context switch) used internally for the data copying. A more exhaustive explanation of how &lt;cite&gt;sendfile(2)&lt;/cite&gt; works is available &lt;a class="reference external" href="http://www.techrepublic.com/article/use-sendfile-to-optimize-data-transfer/"&gt;here&lt;/a&gt;, but long story short is that sending a file with &lt;cite&gt;sendfile()&lt;/cite&gt; is usually &lt;a class="reference external" href="https://github.com/giampaolo/pysendfile#a-simple-benchmark"&gt;twice as fast&lt;/a&gt; as using plain &lt;a class="reference external" href="https://docs.python.org/3/library/socket.html#socket.socket.send"&gt;socket.send()&lt;/a&gt;. Typical applications which can benefit from using &lt;cite&gt;sendfile()&lt;/cite&gt; are FTP and HTTP servers.&lt;/p&gt;
&lt;div class="section" id="socket-sendfile"&gt;
&lt;h2&gt;socket.sendfile()&lt;a class="headerlink" href="#socket-sendfile" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I recently contributed a patch for Python's socket module which adds a high-level &lt;a class="reference external" href="https://docs.python.org/3.5/library/socket.html#socket.socket.sendfile"&gt;socket.sendfile()&lt;/a&gt; method (see full discussion at &lt;a class="reference external" href="http://bugs.python.org/issue17552"&gt;BPO-17552&lt;/a&gt;). &lt;cite&gt;socket.sendfile()&lt;/cite&gt; will transmit a file until EOF is reached by attempting to use &lt;a class="reference external" href="https://docs.python.org/3/library/os.html#os.sendfile"&gt;os.sendfile()&lt;/a&gt;, if available, else it falls back on using plain &lt;cite&gt;socket.send()&lt;/cite&gt;. Internally, it takes care of handling socket timeouts and provides two optional parameters to move the file offset or to send only a limited amount of bytes. I came up with this idea because getting all of that right is a bit tricky, so a generic wrapper seemed useful. &lt;cite&gt;socket.sendfile()&lt;/cite&gt; will make its appearance in Python 3.5.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="sendfile-and-python"&gt;
&lt;h2&gt;sendfile and Python&lt;a class="headerlink" href="#sendfile-and-python" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;cite&gt;sendfile(2)&lt;/cite&gt; made its first appearance into the Python stdlib kind of late: Python 3.3. It was contributed by Ross Lagerwall and me in &lt;a class="reference external" href="http://bugs.python.org/issue10882"&gt;BPO-10882&lt;/a&gt;. Since the patch didn't make it into Python 2.X and I wanted to use sendfile() in pyftpdlib (code.google.com/p/pyftpdlib/issues/detail?id=152), I later decided to release it as a stand alone module working with older (2.5+) Python versions (see &lt;a class="reference external" href="https://github.com/giampaolo/pysendfile"&gt;pysendfile&lt;/a&gt; project). Starting with version 3.5, Python will hopefully start using &lt;cite&gt;sendfile()&lt;/cite&gt; more extensively, in detail:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="http://bugs.python.org/issue13564"&gt;BPO-13563: ftplib&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="http://bugs.python.org/issue13559"&gt;BPO-13559: httplib&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;asyncio: there are some plans for this even though no actual patch yet, see &lt;a class="reference external" href="https://groups.google.com/d/msg/python-tulip/i4OHlIkExsA/eqaK5fzEfCAJ"&gt;discussion&lt;/a&gt; and &lt;a class="reference external" href="http://bugs.python.org/issue17552#msg217099"&gt;BDFL involvement&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, Windows provides something similar to sendfile(2): &lt;a class="reference external" href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms740565(v=vs.85).aspx"&gt;TransmitFile&lt;/a&gt;. Now that socket.sendfile() is in place it seems natural to add support for it as well (see &lt;a class="reference external" href="http://bugs.python.org/issue21721"&gt;BPO-21721&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="backport-to-python-2-6-and-2-7"&gt;
&lt;h2&gt;Backport to Python 2.6 and 2.7&lt;a class="headerlink" href="#backport-to-python-2-6-and-2-7" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For those of you who are interested in using &lt;cite&gt;socket.sendfile()&lt;/cite&gt; with older Python 2.6 and 2.7 versions here's a backport. It requires &lt;a class="reference external" href="https://github.com/giampaolo/pysendfile"&gt;pysendfile&lt;/a&gt; module to be installed. Full code including tests is hosted &lt;a class="reference external" href="https://code.activestate.com/recipes/578889-socketsendfile/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="ch"&gt;#!/usr/bin/env python&lt;/span&gt;

&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class="sd"&gt;This is a backport of socket.sendfile() for Python 2.6 and 2.7.&lt;/span&gt;
&lt;span class="sd"&gt;socket.sendfile() will be included in Python 3.5:&lt;/span&gt;
&lt;span class="sd"&gt;http://bugs.python.org/issue17552&lt;/span&gt;
&lt;span class="sd"&gt;Usage:&lt;/span&gt;

&lt;span class="sd"&gt;&amp;gt;&amp;gt;&amp;gt; import socket&lt;/span&gt;
&lt;span class="sd"&gt;&amp;gt;&amp;gt;&amp;gt; file = open(&amp;quot;somefile.bin&amp;quot;, &amp;quot;rb&amp;quot;)&lt;/span&gt;
&lt;span class="sd"&gt;&amp;gt;&amp;gt;&amp;gt; sock = socket.create_connection((&amp;quot;localhost&amp;quot;, 8021))&lt;/span&gt;
&lt;span class="sd"&gt;&amp;gt;&amp;gt;&amp;gt; sendfile(sock, file)&lt;/span&gt;
&lt;span class="sd"&gt;42319283&lt;/span&gt;
&lt;span class="sd"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;errno&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;select&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;socket&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;memoryview&lt;/span&gt;  &lt;span class="c1"&gt;# py 2.7 only&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;NameError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;memoryview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;posix&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;sendfile&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pysendfile&lt;/span&gt;  &lt;span class="c1"&gt;# requires &amp;quot;pip install pysendfile&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;pysendfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;


&lt;span class="n"&gt;_RETRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;frozenset&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EAGAIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EALREADY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EWOULDBLOCK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errno&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EINPROGRESS&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pysendfile&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;_sendfile_use_sendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;_check_sendfile_params&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;sockno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fileno&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;fileno&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fileno&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ne"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UnsupportedOperation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# not a regular file&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;fsize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fstat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;st_size&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;OSError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# not a regular file&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;fsize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;# empty file&lt;/span&gt;
        &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fsize&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;

        &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gettimeout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;non-blocking sockets are not supported&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# poll/select have the advantage of not requiring any&lt;/span&gt;
        &lt;span class="c1"&gt;# extra file descriptor, contrarily to epoll/kqueue&lt;/span&gt;
        &lt;span class="c1"&gt;# (also, they require a single syscall).&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;hasattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;poll&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
            &lt;span class="n"&gt;pollster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;pollster&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sockno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;POLLOUT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wait_for_fd&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pollster&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;[]:&lt;/span&gt;
                    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;timed out&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# call select() once in order to solicit ValueError in&lt;/span&gt;
            &lt;span class="c1"&gt;# case we run out of fds&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sockno&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;wait_for_fd&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="n"&gt;fds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sockno&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="p"&gt;[]):&lt;/span&gt;
                    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;timed out&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="c1"&gt;# localize variable access to minimize overhead&lt;/span&gt;
        &lt;span class="n"&gt;os_sendfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pysendfile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendfile&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;wait_for_fd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os_sendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sockno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fileno&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blocksize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;OSError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_RETRY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="c1"&gt;# Block until the socket is ready to send some&lt;/span&gt;
                        &lt;span class="c1"&gt;# data; avoids hogging CPU resources.&lt;/span&gt;
                        &lt;span class="n"&gt;wait_for_fd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                            &lt;span class="c1"&gt;# We can get here for different reasons, the main&lt;/span&gt;
                            &lt;span class="c1"&gt;# one being &amp;#39;file&amp;#39; is not a regular mmap(2)-like&lt;/span&gt;
                            &lt;span class="c1"&gt;# file, in which case we&amp;#39;ll fall back on using&lt;/span&gt;
                            &lt;span class="c1"&gt;# plain send().&lt;/span&gt;
                            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;  &lt;span class="c1"&gt;# EOF&lt;/span&gt;
                    &lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt;
                    &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;hasattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;seek&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;_sendfile_use_sendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s2"&gt;&amp;quot;sendfile() not available on this platform&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;_sendfile_use_send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;_check_sendfile_params&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gettimeout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;non-blocking sockets are not supported&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8192&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;8192&lt;/span&gt;
    &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="c1"&gt;# localize variable access to minimize overhead&lt;/span&gt;
    &lt;span class="n"&gt;file_read&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;
    &lt;span class="n"&gt;sock_send&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blocksize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;blocksize&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;break&lt;/span&gt;
            &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;memoryview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blocksize&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;  &lt;span class="c1"&gt;# EOF&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sock_send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;OSError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errno&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_RETRY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;continue&lt;/span&gt;
                    &lt;span class="k"&gt;raise&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
                    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                        &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;hasattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;seek&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;total_sent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;_check_sendfile_params&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;mode&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;file should be opened in binary mode&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;only SOCK_STREAM type sockets are supported&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s2"&gt;&amp;quot;count must be a positive integer (got &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;)&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;repr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s2"&gt;&amp;quot;count must be a positive integer (got &lt;/span&gt;&lt;span class="si"&gt;%s&lt;/span&gt;&lt;span class="s2"&gt;)&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;repr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;sendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;sendfile(sock, file[, offset[, count]]) -&amp;gt; sent&lt;/span&gt;

&lt;span class="sd"&gt;    Send a *file* over a connected socket *sock* until EOF is&lt;/span&gt;
&lt;span class="sd"&gt;    reached by using high-performance sendfile(2) and return the&lt;/span&gt;
&lt;span class="sd"&gt;    total number of bytes which were sent.&lt;/span&gt;
&lt;span class="sd"&gt;    *file* must be a regular file object opened in binary mode.&lt;/span&gt;
&lt;span class="sd"&gt;    If sendfile() is not available (e.g. Windows) or file is&lt;/span&gt;
&lt;span class="sd"&gt;    not a regular file socket.send() will be used instead.&lt;/span&gt;
&lt;span class="sd"&gt;    *offset* tells from where to start reading the file.&lt;/span&gt;
&lt;span class="sd"&gt;    If specified, *count* is the total number of bytes to transmit&lt;/span&gt;
&lt;span class="sd"&gt;    as opposed to sending the file until EOF is reached.&lt;/span&gt;
&lt;span class="sd"&gt;    File position is updated on return or also in case of error in&lt;/span&gt;
&lt;span class="sd"&gt;    which case file.tell() can be used to figure out the number of&lt;/span&gt;
&lt;span class="sd"&gt;    bytes which were sent.&lt;/span&gt;
&lt;span class="sd"&gt;    The socket must be of SOCK_STREAM type.&lt;/span&gt;
&lt;span class="sd"&gt;    Non-blocking sockets are not supported.&lt;/span&gt;
&lt;span class="sd"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_sendfile_use_sendfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;_GiveupOnSendfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_sendfile_use_send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</content><category term="python"/><category term="python-dev"/><category term="sendfile"/><category term="zerocopy"/><category term="network"/><category term="recipe"/><category term="socket"/></entry><entry><title>Goodbye Google Code, I am moving to GitHub</title><link href="https://gmpy.dev/blog/2014/goodbye-google-code-i-am-moving-to-github" rel="alternate"/><published>2014-05-26T00:00:00+02:00</published><updated>2014-05-26T00:00:00+02:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-05-26:/blog/2014/goodbye-google-code-i-am-moving-to-github</id><summary type="html">&lt;p&gt;8 years ago I started hosting my first open source project (pyftpdlib, code.google.com/p/pyftpdlib) on Google Code and I later ended up also hosting psutil (code.google.com/p/psutil) and pysendfile (code.google.com/p/pysendfile). Back then GC had just been released and similarly to …&lt;/p&gt;</summary><content type="html">&lt;p&gt;8 years ago I started hosting my first open source project (pyftpdlib, code.google.com/p/pyftpdlib) on Google Code and I later ended up also hosting psutil (code.google.com/p/psutil) and pysendfile (code.google.com/p/pysendfile). Back then GC had just been released and similarly to other Google products I appreciated the clean and minimalistic interface, the excellent bug tracker and the freedom to choose between different revision control systems (SVN, GIT and Mercurial, which is my favourite one). Unfortunately as the years passed Google completely lost interest in maintaining GC to the point that now GC can basically be considered an &lt;a class="reference external" href="https://groups.google.com/forum/#!topic/google-code-hosting/PEhNMdtBGt4"&gt;abandoned project&lt;/a&gt;. If you take a look at the GC bug tracker (code.google.com/p/support/issues/list) you can see literally hundreds of issues which have been open for years, even some apparently easy ones such as #60 (code.google.com/p/support/issues/detail?id=60) and #919 (code.google.com/p/support/issues/detail?id=919). The lack of interest from Google is absolutely astonishing and it is the main reason why I ultimately decided to change. After at least a couple of years of thinking about migrating to github I finally bit the bullet and as of today psutil is now &lt;a class="reference external" href="https://github.com/giampaolo/psutil"&gt;hosted on github&lt;/a&gt; (update: now also &lt;a class="reference external" href="https://github.com/giampaolo/pyftpdlib"&gt;pyftpdlib&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/giampaolo/pysendfile"&gt;pysendfile&lt;/a&gt;).&lt;/p&gt;
&lt;div class="section" id="what-i-will-miss-the-most-about-gc"&gt;
&lt;h2&gt;What I will miss the most about GC&lt;a class="headerlink" href="#what-i-will-miss-the-most-about-gc" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First of all I must say that despite the unfortunate situation of GC I'm also sad for abandoning it. It started as a really great hosting platform, and it still has some peculiar aspects which I know I will be missing. In order of importance:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;The bug tracker (code.google.com/p/psutil/issues/list): it is much more powerful than &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues?state=open"&gt;github's&lt;/a&gt;, especially for the extremely customizable labeling system which is pure gold, the excellent searching system and the grid view (code.google.com/p/psutil/issues/list?can=2&amp;amp;q=&amp;amp;colspec=ID+Summary+Type+Opsys+Status+Priority+Opened+Owner&amp;amp;groupby=&amp;amp;sort=&amp;amp;x=&amp;amp;y=&amp;amp;cells=tiles&amp;amp;mode=grid). GC bug tracker seriously kicks some ass so kudos to whoever was behind its design! By comparison github bug tracker is too minimalistic and it has no good way to order issues or list them in a more compact form. I'm totally gonna miss psutil bug tracker.&lt;/li&gt;
&lt;li&gt;Mercurial: I'm a big fan of Mercurial and I consider it way more pleasant to work with &lt;a class="reference external" href="https://twitter.com/grodola/status/456238397690875905"&gt;compared to GIT&lt;/a&gt;. I don't know exactly why GIT ended up being so much more used than Mercurial (probably because of github?) but I'm sure that many other guys like me who know both systems will agree that Mercurial is simply so much easier to use. Unfortunately once you decide to stick with github you have no other choice. Mercurial, I'm gonna miss you too!&lt;/li&gt;
&lt;li&gt;GC layout: it is much simpler than github's! Everything is easy to find, even for a non-geek person. The home page alone is perfect to summarize what the project is about and doesn't have tens of icons all over the place. github layout is &lt;a class="reference external" href="https://twitter.com/JoshHelfferich/status/423680220126650368"&gt;more complicated&lt;/a&gt; and needs some time to get used to, even for a programmer. If these projects (psutil and others) weren't about programming I wouldn't have chosen github because it's &amp;quot;not for the masses&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="what-i-appreciate-about-github"&gt;
&lt;h2&gt;What I appreciate about github&lt;a class="headerlink" href="#what-i-appreciate-about-github" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Travis integration: there's this totally awesome free continuous integration service called &lt;a class="reference external" href="https://travis-ci.org/giampaolo/psutil"&gt;Travis&lt;/a&gt; which given a configuration file &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/master/.travis.yml"&gt;like this&lt;/a&gt; will automatically run tests on multiple python versions every time a commit is pushed. They recently added &lt;a class="reference external" href="http://blog.travis-ci.com/2014-05-13-multi-os-feature-available/"&gt;OSX support&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/travis-ci/travis-ci/issues/216"&gt;Windows support&lt;/a&gt; is on the way. This way I will finally be able to quickly test psutil on Linux, OSX and Windows without using virtualized systems except for FreeBSD and Solaris! To me this is like the ultimate Christmas gift and I couldn't ask for any better. Note: as of today Travis only works with github.&lt;/li&gt;
&lt;li&gt;forks and pull requests: honestly I'm not a big fan of them (yet?), probably because I'm used to the python-dev development workflow consisting of uploading patches on the bug tracker and reviewing them (see &lt;a class="reference external" href="http://bugs.python.org/issue17552"&gt;this&lt;/a&gt; for example). Nevertheless to my understanding most people use pull requests in order to contribute to open source projects so basically this is a service I'm glad to offer to my users who hopefully will be able to contribute back more easily. GC has a cloning system (code.google.com/p/psutil/source/clones) but isn't anywhere near github's and I'm not even sure how it works (never cared).&lt;/li&gt;
&lt;li&gt;the &amp;quot;social&amp;quot; side of github including the fact that you can &amp;quot;star&amp;quot; developers and receive notifications about their activity was another big incentive for migrating. The &lt;a class="reference external" href="https://github.com/giampaolo"&gt;personal landing page&lt;/a&gt; collecting all your contributions to different projects is absolutely cool. GC had something similar but they stupidly removed it (code.google.com/p/support/issues/detail?id=24324) all of a sudden and never reintroduced it. &lt;a class="reference external" href="https://groups.google.com/forum/#!topic/google-code-hosting/0XbdStxE_uw"&gt;A lot of people were angry&lt;/a&gt; but again, they didn't care. Actually this was the feature I appreciated the most about GC after the bug tracker and that is when I seriously started thinking about flipping off GC for good.&lt;/li&gt;
&lt;li&gt;the enormous user base: the fact that github is the most used code hosting platform out there will hopefully help me and my projects have a little more visibility. Also, in many job interviews I've been asked what my github profile was, so it seems github also became a factor in getting jobs.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://gist.github.com/"&gt;gists&lt;/a&gt;: gists are &amp;quot;a simple way to share code snippets and pastes with others. All gists are Git repositories, so they are automatically versioned, forkable and usable from Git&amp;quot;. Seriously, they are beautiful. In order to share &lt;a class="reference external" href="https://code.activestate.com/recipes/users/4178764/"&gt;my code snippets&lt;/a&gt; I've always used ActiveState but I think I will eventually migrate them as well in order to have everything in one place.&lt;/li&gt;
&lt;li&gt;the fact that if you mention an issue number as part of your commit message that specific issue will automatically be updated. As I said GC bug tracker is superior in basically any aspect but since I always took care of updating issues by mentioning the specific cset which fixed them (see for example code.google.com/p/psutil/issues/detail?id=463), having this little extra feature will save me some time.&lt;/li&gt;
&lt;li&gt;SSH keys: using Mercurial on GC means using password based authentication. Incredibly they still do not support SSH key based auth. Simply &amp;quot;git push&amp;quot;ing without entering any password when I'm not on my laptop is nice, and of course, it is also much more secure.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="migration"&gt;
&lt;h2&gt;Migration&lt;a class="headerlink" href="#migration" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For those of you who are interested in knowing how I did it, here goes: as for moving the issues from GC bug tracker to github's I used &lt;a class="reference external" href="https://github.com/arthur-debert/google-code-issues-migrator"&gt;this tool&lt;/a&gt;. I managed to preserve the issue IDs but unfortunately not the real owners nor the real issue dates, which kind of sucks. As for migrating the code from mercurial to git I just used &lt;a class="reference external" href="http://stackoverflow.com/a/16037861/376587"&gt;this&lt;/a&gt;. The Mercurial -&amp;gt; GIT transition was perfect and I also managed to preserve the original Mercurial named branches and tags, which for me was crucial. In conclusion, psutil is a five-year-old, medium-sized project with hundreds of issues: the transition in this case is definitely possible but not painless so if you plan on migrating, the sooner you do it the better.&lt;/p&gt;
&lt;/div&gt;
</content><category term="rant"/></entry><entry><title>Announcing psutil 2.0</title><link href="https://gmpy.dev/blog/2014/psutil-20" rel="alternate"/><published>2014-03-10T00:00:00+01:00</published><updated>2014-03-10T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-03-10:/blog/2014/psutil-20</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.0 is out. This is a major rewrite and reorganization of both the Python and C extension modules. It costed me four months of work and more than &lt;strong&gt;22,000 lines&lt;/strong&gt; (the diff against old 1.2.1). Many of the changes are not backward compatible; I'm sure …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.0 is out. This is a major rewrite and reorganization of both the Python and C extension modules. It costed me four months of work and more than &lt;strong&gt;22,000 lines&lt;/strong&gt; (the diff against old 1.2.1). Many of the changes are not backward compatible; I'm sure this will cause some pain, but I think it's for the better and needed to be done.&lt;/p&gt;
&lt;div class="section" id="api-changes"&gt;
&lt;h2&gt;API changes&lt;a class="headerlink" href="#api-changes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I already wrote a detailed &lt;a class="reference external" href="psutil-20-api-redesign"&gt;blog post&lt;/a&gt; about this, so use that as the official reference on how to port your code.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="rst-documentation"&gt;
&lt;h2&gt;RST documentation&lt;a class="headerlink" href="#rst-documentation" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I've never been happy with the old doc hosted on Google Code. The markup language provided by Google is pretty limited, plus it's not under revision control. The new doc is more detailed, uses reStructuredText as the markup language, lives in the same code repository as psutil, and is hosted on the excellent Read the Docs: &lt;a class="reference external" href="http://psutil.readthedocs.org/"&gt;http://psutil.readthedocs.org/&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="physical-cpus-count"&gt;
&lt;h2&gt;Physical CPUs count&lt;a class="headerlink" href="#physical-cpus-count" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You're now able to distinguish between logical and physical CPUs. The full story is in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/427"&gt;#427&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# logical&lt;/span&gt;
&lt;span class="go"&gt;4&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logical&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# physical cores only&lt;/span&gt;
&lt;span class="go"&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="process-instances-are-hashable"&gt;
&lt;h2&gt;Process instances are hashable&lt;a class="headerlink" href="#process-instances-are-hashable" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;psutil.Process&lt;/tt&gt; instances can now be compared for equality and used in sets and dicts. The most useful application is diffing process snapshots:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="c1"&gt;# ... some time passes ...&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;new_procs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;  &lt;span class="c1"&gt;# processes spawned in between&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Equality is not just PID-based. It also includes the process creation time, so a &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; whose PID got reused by the kernel won't be mistaken for the original. The full story is in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/452"&gt;#452&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="speedups"&gt;
&lt;h2&gt;Speedups&lt;a class="headerlink" href="#speedups" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/477"&gt;#477&lt;/a&gt;: &lt;tt class="docutils literal"&gt;Process.cpu_percent()&lt;/tt&gt; is about 30% faster.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/478"&gt;#478&lt;/a&gt;: (Linux) almost all APIs are about 30% faster on Python 3.X.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="other-improvements-and-bugfixes"&gt;
&lt;h2&gt;Other improvements and bugfixes&lt;a class="headerlink" href="#other-improvements-and-bugfixes" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/424"&gt;#424&lt;/a&gt;: published Windows installers for Python 3.X 64-bit.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/447"&gt;#447&lt;/a&gt;: the &lt;tt class="docutils literal"&gt;psutil.wait_procs()&lt;/tt&gt; &lt;tt class="docutils literal"&gt;timeout&lt;/tt&gt; parameter is now optional.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/459"&gt;#459&lt;/a&gt;: a &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/v2.0.0/Makefile"&gt;Makefile&lt;/a&gt; is now available for running tests and other repetitive tasks (also on Windows).&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/463"&gt;#463&lt;/a&gt;: the &lt;tt class="docutils literal"&gt;timeout&lt;/tt&gt; parameter of &lt;tt class="docutils literal"&gt;cpu_percent*&lt;/tt&gt; functions defaults to 0.0, because the previous default was a common source of slowdowns.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/340"&gt;#340&lt;/a&gt;: (Windows) &lt;tt class="docutils literal"&gt;Process.open_files()&lt;/tt&gt; no longer hangs.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/448"&gt;#448&lt;/a&gt;: (Windows) fixed a memory leak affecting &lt;tt class="docutils literal"&gt;Process.children()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Process.ppid()&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/461"&gt;#461&lt;/a&gt;: namedtuples are now pickle-able.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/474"&gt;#474&lt;/a&gt;: (Windows) &lt;tt class="docutils literal"&gt;Process.cpu_percent()&lt;/tt&gt; is no longer capped at 100%.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="api-design"/><category term="compatibility"/><category term="release"/></entry><entry><title>Reimplementing netstat in Python</title><link href="https://gmpy.dev/blog/2014/reimplementing-netstat-in-python" rel="alternate"/><published>2014-03-10T00:00:00+01:00</published><updated>2014-03-10T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-03-10:/blog/2014/reimplementing-netstat-in-python</id><summary type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.1.0 is out and with it I finally managed to implement something I've been wanting to have for a long time: netstat-like functionality (see ticket code.google.com/p/psutil/issues/detail?id=387). Similarly to &lt;cite&gt;&amp;quot;netstat -antp&amp;quot;&lt;/cite&gt; on UNIX you can now list system-wide connections in …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.1.0 is out and with it I finally managed to implement something I've been wanting to have for a long time: netstat-like functionality (see ticket code.google.com/p/psutil/issues/detail?id=387). Similarly to &lt;cite&gt;&amp;quot;netstat -antp&amp;quot;&lt;/cite&gt; on UNIX you can now list system-wide connections in pure python and also determine &lt;strong&gt;what process (PID) is using a particular connection&lt;/strong&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pprint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pprint&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pp&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;net_connections&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;587&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.1.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;10.0.3.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;631&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;0.0.0.0&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3389&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34785&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3591&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;127.0.0.1&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;56359&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LISTEN&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3591&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="n"&gt;sconn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;family&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;::&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;56720&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;raddr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;NONE&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another monitoring use case: say you want to make sure your HTTP server is running on port 80; you can do something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check_listening_port&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Return True if the given TCP port is busy and in LISTEN mode.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;net_connections&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;tcp&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;laddr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CONN_LISTEN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;

&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check_listening_port&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="section" id="netstat-in-pure-python"&gt;
&lt;h2&gt;Netstat in pure python&lt;a class="headerlink" href="#netstat-in-pure-python" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here it is, in 65 lines of code: &lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/release-2.1.0/scripts/netstat.py"&gt;netstat.py&lt;/a&gt;. Pretty neat right? ;-)&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="implementation-s"&gt;
&lt;h2&gt;Implementation(s)&lt;a class="headerlink" href="#implementation-s" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As always, each platform required its own, different implementation. Luckily for some platforms (OSX, Windows) I was able to reuse and customize some code from the existing &lt;cite&gt;Process.connections()&lt;/cite&gt; implementation which was already in place. For those of you who are interested in knowing how this was done here's the source code references:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/6242f7411b882d525e5d267de4bcda1079934ea2/psutil/_pslinux.py#L741"&gt;Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/6242f7411b882d525e5d267de4bcda1079934ea2/psutil/arch/windows/socks.c"&gt;Windows&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/6242f7411b882d525e5d267de4bcda1079934ea2/psutil/arch/freebsd/sys_socks.c"&gt;FreeBSD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/6242f7411b882d525e5d267de4bcda1079934ea2/psutil/_psutil_sunos.c#L1115"&gt;Solaris&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/blob/6242f7411b882d525e5d267de4bcda1079934ea2/psutil/_psutil_osx.c#L1072"&gt;OSX&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hopefully this will help whoever needs to do this in another language. The only platform where this is sort of clunky is OSX, which does not expose anything to list all system-wide sockets in a single shot, so you're forced to query each process. That means you'll need root privileges otherwise you'll get an access denied error. For what it's worth, I took a look at &lt;cite&gt;lsof&lt;/cite&gt; and it has the same limitation. netstat runs with SUID. Well, I guess this is it. I'll leave you with &lt;a class="reference external" href="https://psutil.readthedocs.io/latest/#psutil.net_connections"&gt;some docs&lt;/a&gt;. For the next one I'm planning on working on a couple of other network-related functionalities: &amp;quot;ifconfig&amp;quot; (code.google.com/p/psutil/issues/detail?id=376) and NIC speeds (code.google.com/p/psutil/issues/detail?id=250). But that's for another time...&lt;/p&gt;
&lt;/div&gt;
</content><category term="python"/><category term="network"/></entry><entry><title>Porting your code to psutil 2.0</title><link href="https://gmpy.dev/blog/2014/psutil-20-api-redesign" rel="alternate"/><published>2014-01-11T00:00:00+01:00</published><updated>2014-01-11T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2014-01-11:/blog/2014/psutil-20-api-redesign</id><summary type="html">&lt;p&gt;This blog post is going to be about &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.0, a major release in which I decided to reorganize the existing API for the sake of consistency. At the time of writing, psutil 2.0 is still under development, and the intent of this blog post is to serve …&lt;/p&gt;</summary><content type="html">&lt;p&gt;This blog post is going to be about &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; 2.0, a major release in which I decided to reorganize the existing API for the sake of consistency. At the time of writing, psutil 2.0 is still under development, and the intent of this blog post is to serve as an official reference that describes how you should port your existing code base. In doing so, I will also explain why I decided to make these changes. Even though many APIs will still be available as aliases pointing to the newer ones, the overall changes are numerous and many of them are not backward compatible. I'm sure many people will be sorely bitten, but I think this is for the better and it needed to be done, hopefully for the first and last time.&lt;/p&gt;
&lt;div class="section" id="module-constants-turned-into-functions"&gt;
&lt;h2&gt;Module constants turned into functions&lt;a class="headerlink" href="#module-constants-turned-into-functions" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;What changed&lt;/strong&gt;&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="44%" /&gt;
&lt;col width="56%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Old name&lt;/th&gt;
&lt;th class="head"&gt;Replacement&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.BOOT_TIME&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.boot_time()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.NUM_CPUS&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.cpu_count()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.TOTAL_PHYMEM&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;psutil.virtual_memory().total&lt;/span&gt;&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Why I did it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I already talked about this more extensively in the previous &lt;a class="reference external" href="../2013/making-constants-part-of-your-api-is-evil"&gt;Making constants part of your API is evil&lt;/a&gt; blog post. In short: other than introducing unnecessary slowdowns, calculating a module-level constant at import time is dangerous because if something goes wrong the whole app will crash. Also, the represented values may be subject to change (think about the system clock), but the constant cannot be updated. Thanks to this hack, accessing the old constants still works and produces a &lt;tt class="docutils literal"&gt;DeprecationWarning&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="renamed-module-functions"&gt;
&lt;h2&gt;Renamed module functions&lt;a class="headerlink" href="#renamed-module-functions" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;What changed&lt;/strong&gt;&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="54%" /&gt;
&lt;col width="46%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Old name&lt;/th&gt;
&lt;th class="head"&gt;Replacement&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.get_boot_time()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.boot_time()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.get_pid_list()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.pids()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.get_users()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;psutil.users()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Why I did it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;They were the only module-level functions with a &lt;tt class="docutils literal"&gt;get_&lt;/tt&gt; prefix. None of the others had one.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="renamed-process-class-methods"&gt;
&lt;h2&gt;Renamed Process class methods&lt;a class="headerlink" href="#renamed-process-class-methods" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All methods lost their &lt;tt class="docutils literal"&gt;get_&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;set_&lt;/tt&gt; prefixes. A single method can now be used for both getting and setting (if a value is passed). Assuming &lt;tt class="docutils literal"&gt;p = psutil.Process()&lt;/tt&gt;:&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="54%" /&gt;
&lt;col width="46%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Old name&lt;/th&gt;
&lt;th class="head"&gt;Replacement&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_children()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.children()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_connections()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.connections()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_cpu_affinity()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cpu_affinity()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_cpu_percent()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cpu_percent()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_cpu_times()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cpu_times()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_io_counters()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.io_counters()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_ionice()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.ionice()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_memory_info()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.memory_info()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_ext_memory_info()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.memory_info_ex()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_memory_maps()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.memory_maps()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_memory_percent()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.memory_percent()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_nice()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.nice()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_num_ctx_switches()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.num_ctx_switches()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_num_fds()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.num_fds()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_num_threads()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.num_threads()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_open_files()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.open_files()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_rlimit()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.rlimit()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.get_threads()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.threads()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.getcwd()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cwd()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;...as for &lt;tt class="docutils literal"&gt;set_*&lt;/tt&gt; methods:&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="41%" /&gt;
&lt;col width="59%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Old name&lt;/th&gt;
&lt;th class="head"&gt;Replacement&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.set_cpu_affinity()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cpu_affinity(cpus)&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.set_ionice()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.ionice(ioclass, value=None)&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.set_nice()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.nice(value)&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.set_rlimit()&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.rlimit(resource, limits=None)&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Why I did it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I wanted to be consistent with system-wide module-level functions, which have no &lt;tt class="docutils literal"&gt;get_&lt;/tt&gt; prefix. After I got rid of the &lt;tt class="docutils literal"&gt;get_&lt;/tt&gt; prefixes, removing &lt;tt class="docutils literal"&gt;set_&lt;/tt&gt; too seemed natural and helped reduce the number of methods.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="process-properties-are-now-methods"&gt;
&lt;h2&gt;Process properties are now methods&lt;a class="headerlink" href="#process-properties-are-now-methods" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;What changed&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assuming &lt;tt class="docutils literal"&gt;p = psutil.Process()&lt;/tt&gt;:&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="48%" /&gt;
&lt;col width="53%" /&gt;
&lt;/colgroup&gt;
&lt;thead valign="bottom"&gt;
&lt;tr&gt;&lt;th class="head"&gt;Old name&lt;/th&gt;
&lt;th class="head"&gt;Replacement&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cmdline&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.cmdline()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.create_time&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.create_time()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.exe&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.exe()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.gids&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.gids()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.name&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.name()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.parent&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.parent()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.ppid&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.ppid()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.status&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.status()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.uids&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.uids()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;tt class="docutils literal"&gt;p.username&lt;/tt&gt;&lt;/td&gt;
&lt;td&gt;&lt;tt class="docutils literal"&gt;p.username()&lt;/tt&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Why I did it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Different reasons:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Having a mixed API that uses both properties and methods for no particular reason is confusing and messy, because you don't know whether to use &lt;tt class="docutils literal"&gt;()&lt;/tt&gt; or not.&lt;/li&gt;
&lt;li&gt;A property is usually expected not to perform heavy computations internally, whereas psutil invokes a function every time it is accessed. This has two drawbacks:&lt;ul&gt;
&lt;li&gt;You may get an exception just by accessing the property (e.g. &lt;tt class="docutils literal"&gt;p.name&lt;/tt&gt; may raise &lt;tt class="docutils literal"&gt;NoSuchProcess&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;AccessDenied&lt;/tt&gt;).&lt;/li&gt;
&lt;li&gt;You may erroneously think properties are cached, but this is true only for &lt;tt class="docutils literal"&gt;name&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;exe&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;create_time&lt;/tt&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="cpu-percent-intervals"&gt;
&lt;h2&gt;CPU percent intervals&lt;a class="headerlink" href="#cpu-percent-intervals" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;What changed&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The timeout parameter of &lt;tt class="docutils literal"&gt;cpu_percent*&lt;/tt&gt; functions now defaults to 0.0 instead of 0.1. The functions affected are:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;Process.cpu_percent()&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.cpu_percent()&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;psutil.cpu_times_percent()&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Why I did it&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I originally set 0.1 as the default timeout because you need to wait some time in order to get a meaningful percent value. Having an API that &amp;quot;sleeps&amp;quot; by default is risky, though, because it's easy to forget it does so. That is particularly problematic when calling &lt;tt class="docutils literal"&gt;Process.cpu_percent()&lt;/tt&gt; for all processes: it's very easy to forget to specify &lt;tt class="docutils literal"&gt;timeout=0&lt;/tt&gt;, resulting in dramatic slowdowns that are hard to spot. For example, this code snippet might take a variable number of seconds to complete depending on the number of active processes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# this will be slow&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;process_iter&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_percent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="migration-strategy"&gt;
&lt;h2&gt;Migration strategy&lt;a class="headerlink" href="#migration-strategy" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Except for &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; properties (&lt;tt class="docutils literal"&gt;name&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;exe&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;cmdline&lt;/tt&gt;, etc.), all the old APIs are still available as aliases pointing to the newer names and raising &lt;tt class="docutils literal"&gt;DeprecationWarning&lt;/tt&gt;. psutil will be very clear on what you should use instead of the deprecated API, as long as you start the interpreter with the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-Wd&lt;/span&gt;&lt;/tt&gt; option. This will enable deprecation warnings, which were &lt;a class="reference external" href="http://bugs.python.org/issue7319"&gt;silenced in Python 2.7&lt;/a&gt; (IMHO, from a developer standpoint this was a bad decision).&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;giampaolo@ubuntu:/tmp$ python -Wd
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.
&amp;gt;&amp;gt;&amp;gt; import psutil
&amp;gt;&amp;gt;&amp;gt; psutil.get_pid_list()
__main__:1: DeprecationWarning: psutil.get_pid_list is deprecated; use psutil.pids() instead
[1, 2, 3, 6, 7, 13, ...]
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; p = psutil.Process()
&amp;gt;&amp;gt;&amp;gt; p.get_cpu_times()
__main__:1: DeprecationWarning: get_cpu_times() is deprecated; use cpu_times() instead
pcputimes(user=0.08, system=0.03)
&amp;gt;&amp;gt;&amp;gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you have a solid test suite, you can run tests and fix the warnings one by one. As for the Process properties that were turned into methods, it's more difficult because, whereas psutil 1.2.1 returns the actual value, psutil 2.0.0 returns the bound method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;# psutil 1.2.1&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="go"&gt;&amp;#39;python&amp;#39;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;

&lt;span class="go"&gt;# psutil 2.0.0&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="go"&gt;&amp;lt;bound method Process.name of psutil.Process(pid=19816, name=&amp;#39;python&amp;#39;) at 139845631328144&amp;gt;&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What I would recommend, if you want to drop support for 1.2.1, is to grep for &lt;tt class="docutils literal"&gt;&amp;quot;.name&amp;quot;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&amp;quot;.exe&amp;quot;&lt;/tt&gt;, etc. and just replace them with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;quot;.exe()&amp;quot;&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;quot;.name()&amp;quot;&lt;/span&gt;&lt;/tt&gt; one by one. If, on the other hand, you want to write code that works with both versions, I see two possibilities:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;#1 check version info, like this:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;PSUTIL2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version_info&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;PSUTIL2&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;PSUTIL2&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;#2 get rid of all &lt;tt class="docutils literal"&gt;&amp;quot;.name&amp;quot;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&amp;quot;.exe&amp;quot;&lt;/tt&gt; occurrences you have in your code and use &lt;tt class="docutils literal"&gt;Process.as_dict()&lt;/tt&gt; instead:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pinfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;exe&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pinfo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;exe&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/usr/bin/python2.7&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;python&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pinfo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pinfo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;exe&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="new-features-introduced-in-2-0-0"&gt;
&lt;h2&gt;New features introduced in 2.0.0&lt;a class="headerlink" href="#new-features-introduced-in-2-0-0" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;psutil 2.0.0 is not only about code breakage. I also had the chance to integrate a bunch of interesting features.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/427"&gt;#427&lt;/a&gt;: you're now able to distinguish between the number of logical and physical CPUs:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# logical&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;logical&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# physical cores only&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/452"&gt;#452&lt;/a&gt;: &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; instances are now hashable and can be checked for equality. That means you can use &lt;tt class="docutils literal"&gt;Process&lt;/tt&gt; objects with sets (finally!).&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/447"&gt;#447&lt;/a&gt;: the &lt;tt class="docutils literal"&gt;timeout&lt;/tt&gt; parameter of &lt;tt class="docutils literal"&gt;psutil.wait_procs()&lt;/tt&gt; is now optional.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/461"&gt;#461&lt;/a&gt;: functions returning namedtuples are now picklable.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/giampaolo/psutil/issues/459"&gt;#459&lt;/a&gt;: a Makefile is now available to automate repetitive tasks such as build, install, running tests, etc. There's also a make.bat for Windows.&lt;/li&gt;
&lt;li&gt;Introduced the &lt;tt class="docutils literal"&gt;unittest2&lt;/tt&gt; module as a requirement for running tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><category term="psutil"/><category term="python"/><category term="api-design"/><category term="compatibility"/></entry><entry><title>Making constants part of your API is evil</title><link href="https://gmpy.dev/blog/2013/making-constants-part-of-your-api-is-evil" rel="alternate"/><published>2013-12-21T00:00:00+01:00</published><updated>2013-12-21T00:00:00+01:00</updated><author><name>Giampaolo Rodola</name></author><id>tag:gmpy.dev,2013-12-21:/blog/2013/making-constants-part-of-your-api-is-evil</id><summary type="html">&lt;p&gt;One of the initial features which were included in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; since day one (5 years ago) were the system's boot time, number of CPUs and total physical memory. These metrics have one thing in common: they are (apparently) not supposed to change over time. That is why &lt;a class="reference external" href="http://www.jayloden.com/"&gt;Jay&lt;/a&gt; and I …&lt;/p&gt;</summary><content type="html">&lt;p&gt;One of the initial features which were included in &lt;a class="reference external" href="https://github.com/giampaolo/psutil/"&gt;psutil&lt;/a&gt; since day one (5 years ago) were the system's boot time, number of CPUs and total physical memory. These metrics have one thing in common: they are (apparently) not supposed to change over time. That is why &lt;a class="reference external" href="http://www.jayloden.com/"&gt;Jay&lt;/a&gt; and I decided that exposing them as module constants calculated at import time was the way to go.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;psutil&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NUM_CPUS&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BOOT_TIME&lt;/span&gt;  &lt;span class="c1"&gt;# as seconds since the epoch&lt;/span&gt;
&lt;span class="mf"&gt;1387579049.799092&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;psutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TOTAL_PHYMEM&lt;/span&gt;
&lt;span class="mi"&gt;8374120448&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Five years later I regret that decision, and I'll explain why you shouldn't make the same mistake.&lt;/p&gt;
&lt;div class="section" id="a-constant-should-not-change"&gt;
&lt;h2&gt;A constant should not change&lt;a class="headerlink" href="#a-constant-should-not-change" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When we think of 'constants', our expectations are that they should not change over time. It may be obvious, but before thinking about introducing a constant be absolutely sure the value it represents is going to remain the same.
Now, back then we thought these 3 metrics were not supposed to change, at least until the system is rebooted. Well, we were wrong: it turns out 2 of them actually can.
Apparently virtualized systems can change installed physical memory at runtime (see code.google.com/p/psutil/issues/detail?id=140#c5 and &lt;a class="reference external" href="http://technet.microsoft.com/en-us/library/hh831766.aspx"&gt;here&lt;/a&gt;) and system boot time can easily be altered every time you update the system clock.
In both of these cases, of course, the constants will not reflect the updated values.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="doing-things-at-import-time-is-dangerous"&gt;
&lt;h2&gt;Doing things at import time is dangerous&lt;a class="headerlink" href="#doing-things-at-import-time-is-dangerous" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;That's because import time usually means startup time and if something goes wrong the whole application will crash. In general the only reason for a module to crash at import time is because of a missing dependency or because it's not supposed to run on that platform in the first place.
Now, here's a couple of bug reports which were collected over time: issue 188 (code.google.com/p/psutil/issues/detail?id=188), issue 313 (code.google.com/p/psutil/issues/detail?id=133).
The inconvenience was so severe that I had to release different fixed versions ASAP and the fix consisted of a stinky workaround (code.google.com/p/psutil/source/browse/psutil/_psosx.py?name=release-1.2.1#24).
That's when I started thinking about getting rid of those constants once and for all and introducing functions instead. But how to do that without breaking everybody's code?&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="backward-compatibility-matters"&gt;
&lt;h2&gt;Backward compatibility matters&lt;a class="headerlink" href="#backward-compatibility-matters" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now here's the crucial part: every time you deliver a library to someone else you just cannot remove an API all of a sudden, especially if there are only three of them and they've been around since day one.
It should first be deprecated, possibly turned into an alias pointing to a newer API and finally be removed after one or two major releases. Also, you want the deprecated API to explicitly raise a DeprecationWarning informing the user they're relying on something which will eventually be removed. With a module constant you cannot do any of that. What you would need is a module property.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="module-properties"&gt;
&lt;h2&gt;Module properties&lt;a class="headerlink" href="#module-properties" title="Permalink to this headline"&gt;¶&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One of the greatest things about Python is that it's so dynamic that it lets you do all sorts of nasty things with objects, including injecting names into modules (which are also objects) and making them behave like actual class properties!
For this I have to thank &lt;a class="reference external" href="http://www.dr-josiah.com/2013/12/properties-on-python-modules.html"&gt;Josiah Carlson&lt;/a&gt; who came up with this very simple yet effective solution:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;ModuleWrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;repr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_module&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="fm"&gt;__str__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="fm"&gt;__repr__&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;NUM_CPUS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;NUM_CPUS constant is deprecated; use cpu_count() instead&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;warnings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="ne"&gt;DeprecationWarning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stacklevel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cpu_count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;BOOT_TIME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;BOOT_TIME constant is deprecated; &amp;quot;&lt;/span&gt; \
              &lt;span class="s2"&gt;&amp;quot;use get_boot_time() instead&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;warnings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="ne"&gt;DeprecationWarning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stacklevel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;get_boot_time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;TOTAL_PHYMEM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;TOTAL_PHYMEM constant is deprecated; &amp;quot;&lt;/span&gt; \
              &lt;span class="s2"&gt;&amp;quot;use virtual_memory().total instead&amp;quot;&lt;/span&gt;
        &lt;span class="n"&gt;warnings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="ne"&gt;DeprecationWarning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stacklevel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;virtual_memory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;

&lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ModuleWrapper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="vm"&gt;__dict__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_module&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vm"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vm"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can put this at the bottom of your module and you'll have fully working module constants (tested on Python from 2.4 to 3.4)!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;EDIT&lt;/strong&gt;: the only reason I applied this hack is to turn the old constants into aliases pointing to the newly introduced functions and produce a deprecation warning. That aside I can't think of a case where the use of a module property would be justified.&lt;/p&gt;
&lt;/div&gt;
</content><category term="python"/><category term="api-design"/></entry></feed>