<?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>Tue, 07 Sep 2010 14:24:40 +0000</lastBuildDate><pubDate>Tue, 07 Sep 2010 14:24:40 +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>some sign code</title><dc:creator>Michael Barton</dc:creator><link>http://www.weirdlooking.com/blog/some-sign-code</link><guid>http://www.weirdlooking.com/blog/some-sign-code</guid><pubDate>Fri, 03 Oct 2008 16:54:44 +0000</pubDate><description>So, what I&amp;rsquo;ve got &lt;a href=&quot;http://www.weirdlooking.com/blog/108/protocol.py&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%2Fwww.weirdlooking.com%2Fblog%2F108%2Fprotocol.py) no-repeat center center; -moz-opacity: 0.3; opacity: 0.3; filter:alpha(opacity=30);&quot;&gt;&lt;/span&gt;here&lt;/a&gt; is my implementation of the favotech sign&amp;rsquo;s protocol in python.&amp;nbsp; As I&amp;rsquo;ve mentioned, it&amp;rsquo;s horrible.&amp;nbsp; From what I can tell, the sign supports basically the same protocol over TCP, UDP, and serial.&amp;nbsp; I&amp;rsquo;ve been using UDP over a &lt;a href=&quot;http://www.flickr.com/photos/24388430@N08/2909395961/&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%2Fwww.flickr.com%2Fphotos%2F24388430%40N08%2F2909395961%2F) no-repeat center center; -moz-opacity: 0.3; opacity: 0.3; filter:alpha(opacity=30);&quot;&gt;&lt;/span&gt;crossover cable&lt;/a&gt; from a whitebox with two NICs, and that&amp;rsquo;s worked fine.&lt;br /&gt;&lt;br /&gt;I&amp;rsquo;m using it inside a &lt;a href=&quot;http://twistedmatrix.com/trac/&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%2Ftwistedmatrix.com%2Ftrac%2F) no-repeat center center; -moz-opacity: 0.3; opacity: 0.3; filter:alpha(opacity=30);&quot;&gt;&lt;/span&gt;twisted&lt;/a&gt; app, mostly for all the batteries it includes.&amp;nbsp; I have a UDP server class that inherits from this class, but implements its own send_to_sign method.&amp;nbsp; This layout made reusing the code for different connection media a breeze.&amp;nbsp; Basic usage then looks something like this:&lt;br /&gt;&lt;br /&gt;&lt;code style=&quot;display: block;&quot; class=&quot;blockocode&quot;&gt; &amp;nbsp; self.test_reset()&lt;br/&gt; &amp;nbsp; self.pause()&lt;br/&gt; &amp;nbsp; self.time_sync()&lt;br/&gt; &amp;nbsp; self.set_frame_count(&lt;span style=&quot;color: #FF0000&quot;&gt;3&lt;/span&gt;)&lt;br/&gt; &amp;nbsp; self.set_text(&lt;span style=&quot;color: #FF0000&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #00B000&quot;&gt;'{y}OH HAI'&lt;/span&gt;)&lt;br/&gt; &amp;nbsp; self.set_text(&lt;span style=&quot;color: #FF0000&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #00B000&quot;&gt;'I AM {f}THE{/f} SIGN'&lt;/span&gt;)&lt;br/&gt; &amp;nbsp; self.set_text(&lt;span style=&quot;color: #FF0000&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: #00B000&quot;&gt;'{0}{ma} {dd} &amp;nbsp;{12}'&lt;/span&gt;)&lt;br/&gt; &amp;nbsp; self.resume()&lt;/code&gt;&lt;br /&gt;To dumb down the interface, a frame count of &lt;span style=&quot;font-style: italic;&quot;&gt;n&lt;/span&gt; actually creates a playlist containing &lt;span style=&quot;font-style: italic;&quot;&gt;n&lt;/span&gt; two-letter filenames, &amp;#39;AA&amp;rsquo;&amp;#8230;&amp;rsquo;ZZ&amp;rsquo;.&amp;nbsp; Calling set_text with a frame ID then sets the text of the corresponding file.&amp;nbsp; The set_text method has a dead simple markup language it can use to set text colors and stuff.&lt;br /&gt;&lt;br /&gt;More to come, probably&amp;#8230;</description><category>favotech</category><category>led sign</category><category>python</category><comments>http://www.weirdlooking.com/blog/some-sign-code#comments</comments><wfw:comment>http://www.weirdlooking.com/comments/108</wfw:comment><wfw:commentRss>http://www.weirdlooking.com/rss/comments/108</wfw:commentRss><slash:comments>1</slash:comments></item></channel></rss>