<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><language>en-us</language><copyright>Copyright 2006</copyright><managingEditor>palrich@gmail.com</managingEditor><webMaster>palrich@gmail.com</webMaster><lastBuildDate>Sun, 05 Feb 2012 08:17:47 +0000</lastBuildDate><pubDate>Sun, 05 Feb 2012 08:17:47 +0000</pubDate><ttl>60</ttl><generator>WeirdLooking.com</generator><link>http://www.weirdlooking.com/</link><description>WeirdLooking.com: Michael Barton's Blog</description><title>WeirdLooking.com: Michael Barton's Blog</title><image><url>http://www.weirdlooking.com/images/feed.png</url><title>WeirdLooking.com: Michael Barton's Blog</title><link>http://www.weirdlooking.com/</link></image><item><title>interrupts in python io</title><dc:creator>Michael Barton</dc:creator><link>http://www.weirdlooking.com/blog/interrupts-in-python-io</link><guid>http://www.weirdlooking.com/blog/interrupts-in-python-io</guid><pubDate>Mon, 09 Mar 2009 12:55:55 +0000</pubDate><description>Graham Dumpleton just wrote a &lt;a href=&quot;http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html&quot; style=&quot;position: relative; padding-left: 8px; zoom: 1;&quot;&gt;&lt;span style=&quot;position: absolute; top: -5px; left: 0px; width: 16px; height: 16px; background: URL(http://www.weirdlooking.com/exticon?http%3A%2F%2Fblog.dscpl.com.au%2F2009%2F03%2Fload-spikes-and-excessive-memory-usage.html) no-repeat center center; -moz-opacity: 0.3; opacity: 0.3; filter:alpha(opacity=30);&quot;&gt;&lt;/span&gt;blog post&lt;/a&gt; recommending mod_wsgi&amp;rsquo;s daemon mode, I guess on the grounds that it&amp;rsquo;s harder to screw up than apache&amp;rsquo;s mpm configurations.&amp;nbsp; All the talk of mpms reminded me that I&amp;rsquo;d never posted anything about this one problem I had a while back.&lt;br /&gt;&lt;br /&gt;See, apache&amp;rsquo;s &lt;a href=&quot;http://httpd.apache.org/docs/2.0/mod/worker.html&quot; style=&quot;position: relative; padding-left: 8px; zoom: 1;&quot;&gt;&lt;span style=&quot;position: absolute; top: -5px; left: 0px; width: 16px; height: 16px; background: URL(http://www.weirdlooking.com/exticon?http%3A%2F%2Fhttpd.apache.org%2Fdocs%2F2.0%2Fmod%2Fworker.html) no-repeat center center; -moz-opacity: 0.3; opacity: 0.3; filter:alpha(opacity=30);&quot;&gt;&lt;/span&gt;worker mpm&lt;/a&gt; looks pretty appealing when you&amp;rsquo;re trying to squeeze a little more performance out of a mod_python or mod_wsgi app.&amp;nbsp; Serving requests in individual threads running on the same Python interpreter should reduce the overall memory footprint and would let you keep expensive resources like persistent database connections in a per-interpreter pool that threads access as needed.&amp;nbsp; SQLAlchemy&amp;rsquo;s queue pool is threadsafe for exactly that sort of application.&lt;br /&gt;&lt;br /&gt;But there&amp;rsquo;s a snag if you use any networked services:&amp;nbsp; Apache children get signaled when they hit MaxRequestsPerChild (with SIGUSR2 or SIGRTMIN or something along those lines), and maybe on some other events &amp;#8211; that&amp;rsquo;s just the one I could readily reproduce.&amp;nbsp; If any of the child&amp;rsquo;s threads are in blocking system calls (i.e. network i/o) when that signal hits, the call will be interrupted and a Python exception raised.&lt;br /&gt;&lt;br /&gt;In real life you have to write networking code so that it can treat interrupts as non-fatal errors, but Python&amp;rsquo;s standard library makes that difficult.&amp;nbsp; The popular sendall and readline methods on sockets can&amp;rsquo;t be used period, since they&amp;rsquo;ll lose important state if they&amp;rsquo;re interrupted.&amp;nbsp; Most libraries that use the network, including anything built on top of httplib (such as the clients from xmlrpclib, zsi, suds, etc.), fail to treat interrupts as non-fatal errors.&amp;nbsp; Fortunately, the database libraries I&amp;rsquo;ve looked at have more robust networking.&lt;br /&gt;&lt;br /&gt;In some cases, you could wrap an entire high-level request in a try/except, check if the error argument is EINTR (or EWOULDBLOCK due to a bug in Python &amp;lt; 2.5), then re-try the whole request.&amp;nbsp; But that&amp;rsquo;s expensive, and if you happened to be streaming transient data to a remote server or something, you&amp;rsquo;re screwed.&lt;br /&gt;&lt;br /&gt;I&amp;rsquo;ve had some success monkey patching the socket object with methods that re-try on interrupt, but the socket module&amp;rsquo;s layout makes that difficult, fragile and slow.&amp;nbsp; I also tried writing a module that re-registered all signal handlers for the process with the SA_RESTART set, but that caused more problems than it solved (and some system calls can&amp;rsquo;t be restarted anyway).&amp;nbsp; We&amp;rsquo;ve considered patching the socket library to re-try i/o on interrupt at the C level, but the administrative overhead of maintaining a forked Python just isn&amp;rsquo;t worth it.&lt;br /&gt;&lt;br /&gt;So it looks like pre-fork mpm is the only workable choice for mod_python if you use any networked services.&amp;nbsp; Maybe mod_wsgi in daemon mode would insulate the Python interpreter from any signals, but honestly it&amp;rsquo;s pretty unlikely that our app will be ported any time soon.</description><category>programming</category><category>python</category><comments>http://www.weirdlooking.com/blog/interrupts-in-python-io#comments</comments><wfw:comment>http://www.weirdlooking.com/comments/110</wfw:comment><wfw:commentRss>http://www.weirdlooking.com/rss/comments/110</wfw:commentRss><slash:comments>0</slash:comments></item><item><title>glass box unit testing can bite me</title><dc:creator>Michael Barton</dc:creator><link>http://www.weirdlooking.com/blog/glass-box-unit-testing-can-bite-me</link><guid>http://www.weirdlooking.com/blog/glass-box-unit-testing-can-bite-me</guid><pubDate>Thu, 24 Apr 2008 09:32:37 +0000</pubDate><description>I&amp;rsquo;ve seen unit tests where everything touched by the code is mocked out, and the mocks are intelligent in such a way that the tests fail if the mocked methods aren&amp;rsquo;t all called with correct arguments.&amp;nbsp; It winds up being nearly a line for line check of the implementation of the code, and is about the most horrible thing I think I&amp;rsquo;ve ever heard of.&lt;br /&gt;&lt;br /&gt;I have basically the same problem with tests against private methods.&lt;br /&gt;&lt;br /&gt;Changing implementation details without changing behavior called &amp;ldquo;refactoring&amp;rdquo; and is one of the selling points of unit tests.&amp;nbsp; If a class behaves exactly the same today as it did yesterday, its unit tests shouldn&amp;rsquo;t suddenly &lt;span style=&quot;font-style: italic;&quot;&gt;fail&lt;/span&gt;, even if someone was mucking around inside it.&lt;br /&gt;&lt;br /&gt;If you just want to make sure nobody changes the code, why not get a checksum of the file?&amp;nbsp; It&amp;rsquo;ll be a lot less work and have about the same effect.&amp;nbsp; Ta freakin&amp;#39; da.&lt;br /&gt;&lt;br /&gt;And I&amp;rsquo;m not saying that there aren&amp;rsquo;t exceptions, just that they &lt;span style=&quot;font-style: italic;&quot;&gt;are&lt;/span&gt; exceptions.</description><category>programming</category><category>unit tests</category><comments>http://www.weirdlooking.com/blog/glass-box-unit-testing-can-bite-me#comments</comments><wfw:comment>http://www.weirdlooking.com/comments/102</wfw:comment><wfw:commentRss>http://www.weirdlooking.com/rss/comments/102</wfw:commentRss><slash:comments>1</slash:comments></item></channel></rss>
