<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>In Nomine - The Lotus Land &#187; cherrypy</title>
	<atom:link href="http://www.in-nomine.org/tag/cherrypy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.in-nomine.org</link>
	<description>The focused mind can pierce through stone...</description>
	<lastBuildDate>Sat, 14 May 2011 20:22:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Werkzeug 0.1 released &#8211; WSGI toolbox</title>
		<link>http://www.in-nomine.org/2007/12/21/werkzeug-01-released-wsgi-toolbox/</link>
		<comments>http://www.in-nomine.org/2007/12/21/werkzeug-01-released-wsgi-toolbox/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 11:48:26 +0000</pubDate>
		<dc:creator>asmodai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[werkzeug]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://www.in-nomine.org/2007/12/21/werkzeug-01-released-wsgi-toolbox/</guid>
		<description><![CDATA[Armin Ronacher has released Werkzeug 0.1 a little while ago. As the website for Werkzeug says: &#8220;Werkzeug is a collection of various utilities for WSGI applications. It features request and response objects as well as a powerful url dispatcher and &#8230; <a href="http://www.in-nomine.org/2007/12/21/werkzeug-01-released-wsgi-toolbox/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Armin Ronacher has released <a title="Werkzeug 0.1 Released" href="http://lucumr.pocoo.org/cogitations/2007/12/09/werkzeug-01-released/">Werkzeug 0.1 a little while ago</a>. As the <a title="Werkzeug" href="http://werkzeug.pocoo.org/">website for Werkzeug</a> says: &#8220;Werkzeug is a collection of various utilities for WSGI applications. It     features request and response objects as well as a powerful url dispatcher     and a debugging system.&#8221;</p>
<p>I have it on my todo list to convert my current CherryPy environment to Werkzeug as a proof of concept and see which one of the two I prefer and will ultimately use for <a title="The Kouyou Project" href="http://www.rangaku.org/">my Japanese-Dutch dictionary project</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.in-nomine.org/2007/12/21/werkzeug-01-released-wsgi-toolbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CherryPy, lighttpd and flup</title>
		<link>http://www.in-nomine.org/2007/06/01/cherrypy-lighttpd-and-flup/</link>
		<comments>http://www.in-nomine.org/2007/06/01/cherrypy-lighttpd-and-flup/#comments</comments>
		<pubDate>Fri, 01 Jun 2007 21:24:41 +0000</pubDate>
		<dc:creator>asmodai</dc:creator>
				<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[flup]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://www.in-nomine.org/2007/06/01/cherrypy-lighttpd-and-flup/</guid>
		<description><![CDATA[For a personal project I found myself writing bits and pieces to do URL dispatch handling (using Routes) and then found myself having to write more and more specific handling cases that I just knew was probably already taken care &#8230; <a href="http://www.in-nomine.org/2007/06/01/cherrypy-lighttpd-and-flup/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a personal project I found myself writing bits and pieces to do URL dispatch handling (using <a href="http://routes.groovie.org/" title="Routes is a Python re-implementation of the Rails routes system">Routes</a>) and then found myself having to write more and more specific handling cases that I just knew was probably already taken care of with some sort of framework. Now, <a href="http://www.djangoproject.com/" title="The Web framework for perfectionists with deadlines">Django</a> and <a href="http://www.turbogears.org/" title="TurboGears: Front-to-Back Web Development">TurboGears</a>, however great, seemed to be overkill for this since I will design some stuff from scratch since my demands are just very particular. Then I remembered <a href="http://www.cherrypy.org/" title="CherryPy is a pythonic, object-oriented HTTP framework">CherryPy</a>, which advocates itself as quite a bare-bones HTTP framework.</p>
<p>CherryPy uses a regular expression syntax for URL dispatching that people familiar with Django might recognise. It is a decent way to dispatch URLs, but once you&#8217;ve seen Routes&#8217; maps using such regular expressions feels hackish. Well, at least to me, use whatever works for you.</p>
<p>There was only one issue, how on earth did I put all this in a FastCGI setup? I had previously, for my own script, used <a href="http://trac.saddi.com/flup" title="flup: random Python WSGI stuff">flup</a>&#8216;s fcgi WSGIServer class to kickstart my application. This means that my Lighttpd environment configures a Python file as a FastCGI script and creates a Unix domain socket to connect through. This worked quite well, so I set out to convert my old way to see how to use CherryPy and Routes. The first hurdle I encountered was that <a href="http://www.cherrypy.org/wiki/PageHandlers#Dispatchers" title="CherryPy: Dispatchers">using Routes with CherryPy is not documented well</a> (of course, it is/was not at the time of this writing). Nowhere in that page does it mention the magic incantation to switch dispatchers from the default to, say, <code>RoutesDispatcher</code>. Using some Google-magic as well as discussing this with Alec Thomas (of <a href="http://trac.edgewall.org/" title="Trac is an enhanced wiki and issue tracking system for software development projects">Trac</a> fame) I arrived at the following Python code to switch the dispatcher around:</p>
<pre lang="python">import cherrypy
from project.controllers import *
dispatcher = cherrypy.dispatch.RoutesDispatcher()
dispatcher.connect('home', '', controller=HomeController())
config = {'/': {'request.dispatch': dispatcher}}
app = cherrypy.tree.mount(None, config=config)</pre>
<p>Next I started to pass <code>app</code> to my <code>WSGIServer</code> class to run it. I noticed that something strange was actually happening when I checked with <code>sockstat</code> on my <a href="http://www.freebsd.org/" title="FreeBSD Project website">FreeBSD</a> machine if there were any Unix domain sockets left after stopping Lighttpd. And indeed, there was a stray socket left. Now that was funny, since normally after Lighttpd has sent a termination signal (SIGTERM) to the spawned processes, they shut down and the socket gets cleaned up. What was left was something like this:</p>
<pre>USER     COMMAND    PID   FD PROTO  LOCAL ADDRESS         FOREIGN ADDRESS
www      python     955   0  stream /tmp/labs.sock-0</pre>
<p>So the file descriptor 0 socket is still left. This is, according to Unix tradition, standard input, so in effect it is still waiting to handle data. But wait a second, we told it to shut down, but it didn&#8217;t completely. Using the top command I looked for the process id (PID) and found a line like this:</p>
<pre>PID USERNAME   THR PRI NICE   SIZE    RES STATE  C   TIME   WCPU COMMAND
955 www          9  20    0 14784K 11556K kserel 0   1:25  0.00% python</pre>
<p>Normally when everything is still running normally it displays as:</p>
<pre>PID USERNAME   THR PRI NICE   SIZE    RES STATE  C   TIME   WCPU COMMAND
955 www         10  20    0 14784K 11556K kserel 0   1:25  0.00% python</pre>
<p>Notice the difference in the thread (THR) column (going from 10 to 9). So apparently Lighttpd sends a SIGTERM to the process and it succeeds in killing off one thread and subsequently lets the other nine stay and wait for new requests to serve. Now, this would not be potentially bad, were it not that every stop/start cycle spawns another process with ten threads and thus wasting valuable resources. So clearly this problem had to be solved.</p>
<p>The current code I had in place was (partially lifted from an older Trac FCGI start script):</p>
<pre lang="python">app = cherrypy.tree.mount(None, config=config)
cherrypy.engine.start(blocking=False)
WSGIServer(app).run()</pre>
<p>The traceback should get printed to the browser if the <code>WSGIServer</code> cannot be started for whatever reason or if it raises an exception.</p>
<p>I finally realised that apparently it had to be CherryPy that was not shutting down as it should, especially since this worked with flup&#8217;s <code>WSGIServer</code> and my own code before! Furthermore, <a href="http://groups.google.com/group/cherrypy-users/browse_thread/thread/1221febf36be2d30" title=" cherrypy 3.0.1, lighttpd and fcgi through flup stop problem">in a thread I started on the CherryPy-users group over at Google</a>, Robert Brewer pointed me (mistakenly as he later pointed out) to a <a href="http://www.cherrypy.org/wiki/ServerAPI#HTTPServerAPI" title="CherryPy: HTTP Server API">page detailing the CherryPy HTTPServer API</a>. Even though it was not correct in this case &#8211;CherryPy is not the controller in this case&#8211; it did point out one thing a <code>thread_pool</code> attribute set to a default value of 10! So this really confirmed my thought that CherryPy was not getting closed down as it should.</p>
<p>The solution to such a problem, as with most things in life I guess, was rather an anti-climax, the code above had to changed to be like this:</p>
<pre lang="python">app = cherrypy.tree.mount(None, config=config)
cherrypy.engine.start(blocking=False)
try:
    WSGIServer(app).run()
finally:
    cherrypy.engine.stop()</pre>
<p>And that&#8217;s it!</p>
<p><em>Update 2007-06-02 10:57</em>: Stripped the exceptions, they actually do not add much in this case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.in-nomine.org/2007/06/01/cherrypy-lighttpd-and-flup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

