<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" 
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" 
   xmlns:html="http://www.w3.org/1999/html" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
   <title>James Westby</title>
   <link>http://jameswestby.net/weblog</link>
   <description></description>
   <language>en</language>
   <copyright>Copyright 2006-2008 James Westby</copyright>
   <ttl>60</ttl>
   <pubDate>Tue, 15 May 2012 02:15 GMT</pubDate>
   <managingEditor>jw+blog@jameswestby.net</managingEditor>
   <generator>PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008</generator>
<item>
   <title>Monitoring and Testability</title>
   <guid isPermaLink="false">tech/25-monitoring-and-testability</guid>
   <link>http://jameswestby.net/weblog/tech/25-monitoring-and-testability.html</link>
   <description><![CDATA[
<div class="document">
<p>At UDS last week there was another &quot;Testing in Ubuntu&quot; session. During the
event I gave a brief presentation on monitoring and testability. The thesis
was that there are a lot of parallels between monitoring and testing, so many
that it's worth thinking of monitoring as a type of testing at times. Due to
that great monitoring requires a testable system, as well as thinking about
monitoring right at the start to build a monitorable system as well as a
testable one.</p>
<p>You can watch a video of the talk <a class="reference external" href="http://www.youtube.com/watch?v=zWPp7vmMwOk#t=48m34s">here</a>. (Thanks to the video team for
recording it and getting it online quickly.)</p>
<p>I have two main questions. Firstly, what are the conventional names for
the &quot;passive&quot; and &quot;active&quot; monitoring that I describe? Seecondly, do you
agree with me about monitoring?</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Tue, 15 May 2012 02:15 GMT</pubDate>
</item>
<item>
   <title>The value of primary sources</title>
   <guid isPermaLink="false">life/05-the-value-of-primary-sources</guid>
   <link>http://jameswestby.net/weblog/life/05-the-value-of-primary-sources.html</link>
   <description><![CDATA[
<div class="document">
<p>I recently finished reading <a class="reference external" href="http://www.goodreads.com/book/show/3339527-all-art-is-propaganda">&quot;All Art is Propaganda&quot;</a> by George Orwell,
a collection of some of his critical essays. It was a fascinating read,
and would recommended it. Each of the essays is thought-provoking and
enlightening, and the topics covered are numerous and varied.</p>
<p>The most interesting feature of the book though wasn't the subject matter
of the essays, but the organisation of them. The editor decided to put them
in chronological order, meaning that you see some development of ideas
over the essays, and different topics rise and fall in prominence.</p>
<p>While that's certainly not novel, the effect of structuring the book
in this way was very noticeable in this case for me. I saw a lot of
parallels to the impressions I've had from following <a class="reference external" href="https://twitter.com/#!/RealTimeWWII">&#64;RealTimeWWII</a>
on twitter. This account is &quot;live tweeting&quot; the Second World War as
if it were happening today (currently in 1940).</p>
<p>This artifice brings a whole fresh appreciation of this period that I
have learnt so much about. Consuming the events at the pace they occured
gives time to reflect on each one, and forgetting that I know the
events that followed allows one to get a greater understanding of what
it would have been like to live at that time. The time-compressing
effect of looking back tends to obscure the uncertainty and fear of that
time, the slowness with which some events were unfolding accentuating it.
Consuming this via twitter, with its headline-like format mixing in
with the news of today, heightens this effect.</p>
<p>While it's something of a loss that we aren't able to know what Orwell
would say about the events of today, or what he would have changed in
these essays with the addition of hindsight, there's an undeniable
value in reading this primary source. While hindsight adds, it also
takes away, blurring memories and changing perspectives. Reading the
essays allows you to pick up on the thinking of those living through
the events that we think we know so well.</p>
<p>For instance Orwell seemed to believe that Soviet Russia was a greater
threat than Nazism. The essays in the book run from 1940 to 1949, and
there are many more words devoted to the Soviets than the Nazis throughout.
His writing suggests that he thought the techniques the Soviets used to
achieve and maintain power were less well known and understood, and would
be more effective over a long period.</p>
<p>After better understanding these benefits I plan to redouble my efforts
to choose books from a varied set of sources, including from different
times, and avoid falling in to a trap of thinking that more recent must
be better as knowledge is always on the increase.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">life</category>
   <pubDate>Wed, 11 Apr 2012 20:21 GMT</pubDate>
</item>
<item>
   <title>First day of the Linaro Connect</title>
   <guid isPermaLink="false">tech/24-first-day-of-linaro-connect</guid>
   <link>http://jameswestby.net/weblog/tech/24-first-day-of-linaro-connect.html</link>
   <description><![CDATA[
<div class="document">
<p>Today is the first day of the <a class="reference external" href="http://connect.linaro.org">Linaro Connect</a> in Cambridge. Linaro has
gathered to spend a week talking, coding and having fun.</p>
<p>The Infrastructure team is spending most of the week coding, on a few
select topics, chosen to make good use of the time that we have together.</p>
<p>In order to help us focus on our goals for the week I've put together
a hard copy version of <a class="reference external" href="http://status.linaro.org">status.linaro.org</a>.</p>
<img alt="/images/connect-progress-start.jpg" src="/images/connect-progress-start.jpg" />
<p>We'll be updating it during the week as we make progress. I'll report
back on how it looks at the end of the week.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Mon, 01 Aug 2011 11:54 GMT</pubDate>
</item>
<item>
   <title>pkgme: handles packaging for you</title>
   <guid isPermaLink="false">tech/23-pkgme-handles-packaging-for-you</guid>
   <link>http://jameswestby.net/weblog/tech/23-pkgme-handles-packaging-for-you.html</link>
   <description><![CDATA[
<div class="document">
<p>If you are an application developer and you want to distribute your new
application for a linux distribution, then you currently have several
hurdles in your path. Beyond picking which one to start with, you either
have a learn a packaging format well enough that you can do the work
yourself, or find someone that can do it for you.</p>
<p>At the early stages though neither of these options is particularly
compelling. You don't want to learn a packaging format, as there is
lots of code to write, and that's what you want to focus on. Finding
someone to do the work for you would be great, but there are far
more applications than skilled packagers, and convincing someone to
help you with something larval is tough: there are going to be a lot
of updates, with plenty of churn, to stay on top of, and it may be
too early for them to tell if the application will be any good.</p>
<p>This is where <tt class="docutils literal">pkgme</tt> comes in. This is a tool that can take care
of the packaging for you, so that you can focus on writing the code,
and skilled packagers can focus on packages that need high-quality
packaging as they will have lots of users.</p>
<p>This isn't a new idea, and there are plenty of tools out there to
generate the packaging for e.g. a Python application. I don't
think it is a particularly good use of developer time to produce
tools like that for every language/project type out there.</p>
<p>Instead, a few of us created <tt class="docutils literal">pkgme</tt>. This is a tool in two parts.
The first part knows about packaging, and how to create the necessary
files to build a working package, but it doesn't know anything about
your application. This knowledge is delegated to a backend, which doesn't
need to understand packaging, and just needs to be able to tell <tt class="docutils literal">pkgme</tt>
certain facts about the application.</p>
<p><tt class="docutils literal">pkgme</tt> is now at a stage where we would like to work with people to
develop backends for whatever application type you would like (Python/
Ruby On Rails/GNOME/KDE/CMake/Autotools/Vala etc.) You don't have to
be an expert on packaging, or indeed on the project type you want to
work on. All it takes is writing a few scripts (in whatever language
makes sense), which can introspect an application and report things
such as the name, version, dependencies, etc.</p>
<p>If this sounds like something that you would like to do then please
<a class="reference external" href="http://pkgme.net/doc/backends/index.html">take a look at the documentation</a>, write the scripts, and then
submit your backend for inclusion in <tt class="docutils literal">pkgme</tt>.</p>
<p>You can also <a class="reference external" href="http://pkgme.net/contact.html">contact the developers</a>, see the <a class="reference external" href="http://pkgme.net/">nascent website at pkgme.net</a>,
or <a class="reference external" href="https://launchpad.net/pkgme">visit the Launchpad page.</a> (We are also very interested in help
with the website and documentation if that is where you skills or interests
lie.)</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Sun, 06 Feb 2011 19:05 GMT</pubDate>
</item>
<item>
   <title>Couchapp Walkthrough: Part 4: How data gets to the user</title>
   <guid isPermaLink="false">tech/22-couchapp-walkthrough-part-4-how-data-gets-to-the-user</guid>
   <link>http://jameswestby.net/weblog/tech/22-couchapp-walkthrough-part-4-how-data-gets-to-the-user.html</link>
   <description><![CDATA[
<div class="document">
<p>This was the confusing part when I first ran <tt class="docutils literal">couchapp</tt> to create a new app,
I couldn't really see where the &quot;entry point&quot; of the app was. In the hope that
it might help someone else I'm going to present a quick overview of the default
setup.</p>
<div class="section" id="index-html">
<h1><tt class="docutils literal">index.html</tt></h1>
<p>The <tt class="docutils literal">index.html</tt> page is a static attachement, and the user starts by requesting
it with their browser.</p>
<p>It has some small amount of static HTML, part of which creates a <tt class="docutils literal">div</tt> for the
javascript to put the data in.</p>
<p>Either inline, or in an included file, there is a small bit of javascript that will
initialise the couchapp.</p>
<p>By default this will use the <tt class="docutils literal">div</tt> with the id <tt class="docutils literal">items</tt>, and will attach an
<tt class="docutils literal">evently</tt> widget to it.</p>
</div>
<div class="section" id="evently">
<h1><tt class="docutils literal">evently</tt></h1>
<p>The <tt class="docutils literal">evently</tt> widget that is attached will then either have an <tt class="docutils literal">_init</tt> event,
or a <tt class="docutils literal">_changes</tt> event, either of which will be immediately run by <tt class="docutils literal">evently</tt>.</p>
<p>This event will usually make a couchdb query to get data to transform to HTML and
present to the user (see <cite>part three</cite> for how this works.)</p>
<p>Once that data has been displayed the user any combination of <tt class="docutils literal">evently</tt> widgets or
javascript can be used to make further queries and build an app that works however
you like.</p>
</div>
<div class="section" id="previous-installments">
<h1>Previous installments</h1>
<p>See <a class="reference external" href="http://jameswestby.net/weblog/tech/18-couchapp-walkthrough-part-1.html">part one</a>, <a class="reference external" href="http://jameswestby.net/weblog/tech/19-couchapp-walkthrough-part-2-the-couchapp-tool.html">part two</a>, and <a class="reference external" href="http://jameswestby.net/weblog/tech/20-couchapp-walkthrough-part-3-evently.html">part three.</a></p>
</div>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Fri, 24 Dec 2010 01:16 GMT</pubDate>
</item>
<item>
   <title>Introducing soupmatchers</title>
   <guid isPermaLink="false">tech/21-introducing-soupmatchers</guid>
   <link>http://jameswestby.net/weblog/tech/21-introducing-soupmatchers.html</link>
   <description><![CDATA[
<div class="document">
<p>jml <a class="reference external" href="http://code.mumak.net/2010/12/testtools-098-released.html">just announced testtools 0.9.8</a> and in it mentioned the <a class="reference external" href="http://launchpad.net/soupmatchers">soupmatchers</a>
project that I started. Given that I haven't talked about it here before, I wanted to
do a post to introduce it, and explain some of the rationale behind it.</p>
<p>soupmatchers is a library for unit testing HTML, allowing you to assert that certain things
are present or not within an HTML string. Asserting this based on substring matching is going
to be too fragile to be usable, and so soupmatchers works on a parsed representation of the HTML.
It uses the wonderful <a class="reference external" href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup</a> library for parsing the HTML, and allows you to assert
the presence or not of tags based on the attributes that you care about.</p>
<pre class="literal-block">
self.assertThat(some_html,
                HTMLContains(Tag('testtools link', 'a',
                attrs={'href': 'https://launchpad.net/testtools'})))
</pre>
<p>You can see more examples in <a class="reference external" href="http://bazaar.launchpad.net/~soupmatchers-dev/soupmatchers/trunk/annotate/head:/README">the README</a>.</p>
<p>Basing this on the testtools matchers frameworks allows you to do this in a semi-declarative way.
I think there is a lot of potential here to improve your unit tests. For instance you can
start to build a suite of matchers tailored to talking about the HTML that your application outputs.
You can have matchers that match areas of the page, and then talk about other elements relative to
them (&quot;This link is placed within the sidebar&quot;). One thing that particularly interests me is to create
a class hierarchy that allows you test particular things across your application. For instance,
you could have an ExternalLink class that asserts that a particular class is set on all of your
external links. Assuming that you use this at the appropriate places in your tests then you will know that the
style that is applied to class will be on all external links. Should you wish to change the way that
external links are represented in the HTML you can change the one class and your tests should tell you
all the places that the code has to be updated.</p>
<p>Please go ahead and try the library and <a class="reference external" href="https://bugs.launchpad.net/soupmatchers/+filebug">let me know</a> how it could be improved.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Thu, 23 Dec 2010 02:45 GMT</pubDate>
</item>
<item>
   <title>Couchapp Walkthrough: Part 3: Evently</title>
   <guid isPermaLink="false">tech/20-couchapp-walkthrough-part-3-evently</guid>
   <link>http://jameswestby.net/weblog/tech/20-couchapp-walkthrough-part-3-evently.html</link>
   <description><![CDATA[
<div class="document">
<p>[ Apologies to those that saw this half-finished when I published
rather than saving a draft ]</p>
<p>This is the part that it took me a long time to understand: how the different
parts of the default couchapp collaborate to present data to the user.</p>
<p>In this post I'm just going to deal with client-side couchapps using the
default technologies. As explained in the previous post you can use any
combination of HTML and javascript in a couchapp, and you can also do some
of the work server-side in couchdb. However, I'm going to explain what
the couchapp tool gives you when you create a new project, as that is
where you are likely to be starting, and once you understand that you
can choose where to deviate from that model.</p>
<div class="section" id="jquery-events">
<h1>jQuery events</h1>
<p>Our first detour is in to a little bit of background, the excellent
javascript libarary that is heavily used in couchapps.</p>
<p>jQuery allows for events on elements in the DOM. There are standard
events, such as &quot;click&quot; and &quot;submit&quot;, but you are free to define your
own.</p>
<p>These events are given a name, and you can then &quot;trigger&quot; them, and
bind handlers to act when they are triggered.</p>
<p>By building up events from low level ones such as &quot;click&quot;, to more-complex
and app-specific ones such as &quot;item purchased&quot;, you can break down
your code in to smaller chunks, and have different parts of the page
react to the same event, such as having the &quot;buy&quot; link disappear from
the item that the user just bought, as well as having the total of
the shopping cart update.</p>
<p>Events can also have data, or arguments, that travels with them. For instance
the &quot;item purchased&quot; event could have the item that was purchased as the
data, so that handlers can make use of it when they run.</p>
</div>
<div class="section" id="evently">
<h1>evently</h1>
<p>Now that we know something about jQuery events, we can look at something
built on top of them, the &quot;evently&quot; library. This is a layer on top of
jQuery that allows you build up your app from pieces that have a specific
function, and communicate through events.</p>
<p>An evently &quot;widget&quot; can be bound to an element (or several elements if
you want). The widget is a bunch of event handlers which can do anything
you like, but have some conveniences built in for fetching data and
updating the page based on the result.</p>
<p>When an event is triggered the handler you defined is run. If it is
a simple javascript function then that function is run, and can do
anything you like.</p>
<pre class="literal-block">
{click: function() {
        alert(&quot;You clicked!&quot;);
    }
}
</pre>
<p>Often though you want to update the element based on the action. evently
has built in support for the &quot;mustache&quot; templating language, and if you
specify a template in that syntax it will replace the current HTML
of the element that it is attached to with the result of rendering
that template.</p>
<pre class="literal-block">
{click:
    {
        mustache: &quot;&lt;div&gt;You clicked!&lt;/div&gt;&quot;
    }
}
</pre>
<p>Which will put &quot;You clicked!&quot; in to the page instead of in an alert. What
if you don't want to replace the current content, and just want to append
another line? For that use the &quot;render&quot; option.</p>
<pre class="literal-block">
{click:
    {
        mustache: &quot;&lt;div&gt;You clicked!&lt;/div&gt;&quot;,
        render: &quot;append&quot;
    }
}
</pre>
<p>Which would put another &quot;You clicked!&quot; on the page every time you click.
As well as &quot;append&quot; there is also &quot;prepend&quot;, or really any jQuery method
that you want to call.</p>
<p>Simply rendering a static template isn't going to be very useful though,
usually you want something dynamic. For that use the &quot;data&quot; option,
which can just be an object if you want, but that's still not going to
be very dynamic either, so it can be a function that returns an object.</p>
<pre class="literal-block">
{click:
    {
        data: function(e) {
            return {name, &quot;Bob&quot;};
        },
        mustache: &quot;&lt;div&gt;Hi {{name}}!&lt;/div&gt;&quot;
    }
}
</pre>
<p>The data function gets passed the event object from jQuery (so you
can e.g. get the target of the event), and any data for the event
too (so it could see what item you just bought).</p>
<p>That's all well and good, but it doesn't help us get data from couchdb
in to the page. For that we need the opportunity to make a request
to couchdb. We could just fall back to using one function to handle
the event, but then we lose the integration with mustache. Therefore
there is an &quot;async&quot; key that allows us to make an AJAX request
and then use mustache on the result.</p>
<pre class="literal-block">
{click:
    {
        async: function(callback) {
            /* some code that does an async request, and then calls callback with the result */
        },
        data: function(resp) {
            /* Some code that processes the data from the async function to ready it for the template */
        },
        mustache: &quot;A tempate that will be rendered with the result of the data function&quot;
    }
}
</pre>
<p>Now, writing an async method to query a couchdb view is so common in couchapps that
eventy has special support for it. The <tt class="docutils literal">query</tt> key can either be a json
structure that specifies a view and the arguments to it, or a function that
returns such a structure based on things such as the query string in the URL.</p>
<p>There are two further functions that you will find helpful from time to time. The
first is <tt class="docutils literal">before</tt> that allows you to run some code before the rest of the process
starts, and may do something such as trigger another event. The other is its partner
<tt class="docutils literal">after</tt>, which can do much the same things as <tt class="docutils literal">before</tt>, but can also do things
such as modify the HTML that is output.</p>
<p>Lastly there is another thing that can be done with the HTML that is output,
specified with they <tt class="docutils literal">selectors</tt> key. This allows you to perform an action
on particular parts of the html. The keys of this structure are jQuery
selectors that specify which elements the function will be applied to. For
instance you can do something with all the divs in the output, or all the
spans with a certain class, or the form with a particular id.</p>
<p>What you can do to those elements is basically unlimited, as you can run
arbitrary javascript. However, there is built in support for specifying
an evently widget, which will automatically be bound to each element
that matches the selector. This nesting is one of the most powerful and
useful features of evently, and one you should generally be using often.
I will probably talk more about what nested widgets are useful for later.</p>
</div>
<div class="section" id="special-evently-events">
<h1>Special evently events</h1>
<p>evently has two special events. The first of these is <tt class="docutils literal">_init</tt>. This
event is triggered when the widget is created. This means you can
dynamically pre-populate the element, or at least keep the inital
state of the element with the rest of your code, rather than putting
some in the HTML file and the rest in evently code.</p>
<p>The other special event is tied to couchdb, and is the <tt class="docutils literal">_changes</tt>
event, and is triggered whenever the database that the couchapp is
in changes. This means that you can have elements on the page that
dynamically update whenever the database changes, whether that is
through user action, another user doing something, external scripts,
or couchdb replication. This makes it very easy to write &quot;live&quot;
pages that show updates without refreshes, and is very useful for
some applications.</p>
<p>Currently <tt class="docutils literal">_changes</tt> doesn't receive the modified documents, so
it is normally just used to make another request to get the updated
information, whether that be through <tt class="docutils literal">async</tt> or <tt class="docutils literal">view</tt>. If
you wish to get the modified documents in order to update the page
directly and reduce requests then you can write some custom code
to do this.</p>
</div>
<div class="section" id="conclusion">
<h1>Conclusion</h1>
<p>As you have seen, evently is just a thin layer on top of jQuery concepts
such as events and asynchronous events, with some conveniences for
templating and interacting with couchdb.</p>
<p>This combination is well suited to the needs of at least simple
and moderately complex couchapps, while still being very powerful,
and allowing you to fall back to custom javascript at any point.</p>
<p>You can find more about evently at <a class="reference external" href="http://couchapp.org/page/evently">the couchapp site.</a></p>
<p>See <a class="reference external" href="http://jameswestby.net/weblog/tech/18-couchapp-walkthrough-part-1.html">part one</a> and <a class="reference external" href="http://jameswestby.net/weblog/tech/19-couchapp-walkthrough-part-2-the-couchapp-tool.html">part two.</a></p>
</div>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Sat, 09 Oct 2010 00:56 GMT</pubDate>
</item>
<item>
   <title>Couchapp Walkthrough: Part 2: The couchapp tool</title>
   <guid isPermaLink="false">tech/19-couchapp-walkthrough-part-2-the-couchapp-tool</guid>
   <link>http://jameswestby.net/weblog/tech/19-couchapp-walkthrough-part-2-the-couchapp-tool.html</link>
   <description><![CDATA[
<div class="document">
<p>Today I would like to talk about the couchapp tool. This is something that you can use when working on couchapps, and provides a way to quickly iterate your design.</p>
<p>However, rather confusingly, the couchapp tool isn't actually required for couchapps. If you get a design document with HTML attachments in to your database then you have a couchapp.</p>
<p>Why would you want to use such a tool then? Firstly because it will generate a skeleton couchapp for you, so that you don't have to remember how to organise it, and if it is your first couchapp it's good to start from something working.</p>
<p>More importantly though, the couchapp tool is useful as you develop as it allows you to edit the parts of your app in their native format. This means that if you are writing a HTML snippet you can just put some HTML in a file, rather than having to write it as part of a deeply nested JSON structure and deal with the errors that you would make if you did that. Also it means that you can use things like syntax highlighting in your preferred text editor without any special magic.</p>
<div class="section" id="how-it-works">
<h1>How it works</h1>
<p>At it's core couchapp is a rather simple tool. It walks a filesystem tree and assembles the things that it finds there in to a JSON document.</p>
<p>For instance, if it finds a directory at the root of the tree called <tt class="docutils literal">_attachments</tt> it puts the content of each file there in to a list which it puts in to the dict with an &quot;_attachments&quot; key, Which is one of the ways that couchdb accepts document attachments. Therefore if you have an <tt class="docutils literal">_attachments/index.html</tt> file in your tree it will be attached to your design document when the JSON structure is sent to couchdb.</p>
<p>This continues across the tree, so the contents of the &quot;views&quot; directory will become the &quot;views&quot; key of the documents, which is how you do map/reduce queries on the database.</p>
<p>couchapp has various conventions for dealing with files. For instance if it finds a &quot;.js&quot; file it treats it as a javascript snippet which will be encoded in to a string in the resulting document. &quot;.html&quot; files outside of the &quot;_attachments&quot; directory will also be encoded as strings. If it finds a &quot;.json&quot; file then it treats it as literal JSON that will be embebedded.</p>
<p>This way it builds up the JSON structure that a couchapp expects, and will send it to the couchdb of your choice when it is done.</p>
<p>In addition to this functionality the tool can also generate you a skeleton app, and also add new pieces to your app, such as new views.</p>
</div>
<div class="section" id="getting-it">
<h1>Getting It</h1>
<p>couchapp is a python tool, so you can install it using pip or similar. However, Ubuntu users can <a class="reference external" href="https://launchpad.net/~couchapp/+archive/couchapp">install it from a PPA</a> (yay for daily builds with recipes!).</p>
</div>
<div class="section" id="using-it">
<h1>Using It</h1>
<p>To use it run</p>
<pre class="literal-block">
couchapp generate myapp
</pre>
<p>which will create you a new skeleton in myapp.</p>
<pre class="literal-block">
cd myapp
ls
</pre>
<p>You will see for instance the <tt class="docutils literal">_attachments</tt> and <tt class="docutils literal">views</tt> directories, and an <tt class="docutils literal">_attachments/index.html.</tt></p>
<p>To get your app in to couchdb you can run</p>
<pre class="literal-block">
couchapp push http://localhost:5984/mydb
</pre>
<p>and it will tell you the URL to visit to see your new app.</p>
<p>If you want to use desktopcouch you can run</p>
<pre class="literal-block">
couchapp push desktopcouch://
</pre>
<p>though I think it has a bug that it prints the wrong URLs when pushing to desktopcouch.</p>
<p>Once you have looked at the HTML generated by your app you should look at the design document that couchapp created. Go to</p>
<pre class="literal-block">
http://localhost:5984/_utils
</pre>
<p>or</p>
<pre class="literal-block">
xdg-open ~/.local/share/desktop-couch/couchdb.html
</pre>
<p>if you are using desktopcouch.</p>
<p>Click to the mydb database and you will see a document called <tt class="docutils literal">_design/myapp</tt>. Click on this and you will see the content of the design document; you are looking at a couchapp in its raw form.</p>
<p>If you compare what is in that design document with what is in the myapp directory that the tool created you should start to see how it generates it from the filesystem.</p>
<p>Now try making a change on the filesystem, for instance edit <tt class="docutils literal">_attachments/index.html</tt> and put your name somewhere in the body. Then push again, running</p>
<pre class="literal-block">
couchapp push http://localhost:5984/mydb
</pre>
<p>and refresh the page in your browser and you should see the change. (Just click on <tt class="docutils literal">index.html</tt> in the design document to get back to viewing your app from there).</p>
<p>I will go in to more detail about the content of the couchapp that was generated for you in another post.</p>
<p>See the <a class="reference external" href="/weblog/tech/18-couchapp-walkthrough-part-1.html">previous installment.</a></p>
</div>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Wed, 29 Sep 2010 23:48 GMT</pubDate>
</item>
<item>
   <title>Couchapp Walkthrough - Part 1</title>
   <guid isPermaLink="false">tech/18-couchapp-walkthrough-part-1</guid>
   <link>http://jameswestby.net/weblog/tech/18-couchapp-walkthrough-part-1.html</link>
   <description><![CDATA[
<div class="document">
<p>Couchapps are a particular way of using couchdb that allow you to
serve web applications directly from the database. These applications generate
HTML and javascript to present data from couchdb to the user,
and then update the database and the UI based on their actions.</p>
<p>Of course there are plenty of frameworks out there that do this sort of thing,
and more and more of them are adding couchdb support. What
makes couchapps particularly interesting are two things. Firstly, the
ease with which they can be developed and deployed. As they are served
directly from couchdb they require little infrastructure, and the
couchapp tool allows for a rapid iteration. In addition, the conveniences
that are provided mean that simple things can be done very quickly with
little code.</p>
<p>The other thing that makes couchapps attractive is that they live inside
the database. This means that the code lives alongside the data, and will
travel with it as it is replicated. This means that you can easily have
an app that you have fast, local access to on your desktop, while
at the same time replicating to a server so that you can access the same
data from your phone while you are out. Again, this doesn't require
couchapps, and they won't be suitable for all needs, but they are certainly
an interesting idea.</p>
<p>You can read more about couchapps at <a class="reference external" href="http://couchapp.org">http://couchapp.org</a>.</p>
<p>Intrigued by couchapps I set out to play with them over a weekend. Unfortunately
the documentation is rather lacking currently, so I wouldn't recommend experimenting
yourself if you are not happy digging around for answers, and sometimes
not finding them outside the code. In order to
go a little way to rectifying this, I intend to write a few posts about
the things I wish I had known when I started out. I found everything to be
a little strange at first, and it wasn't even clear where the entry point
of a couchapp was for instance. Hopefully these posts will be found using
google by others who are struggling in a similar way.</p>
<div class="section" id="architecture">
<h1>Architecture</h1>
<p>Firstly something about the pieces that make up a couchapp (or at least those
that the tool and documentation recommend,) and the way that they all fit together.</p>
<p>At the core is the couchdb database itself. It is a collection of &quot;documents&quot;,
each of which can have attachments. Some of these documents are known as
&quot;design documents,&quot; and they start with a prefix of &quot;_design.&quot; Design
documents can have &quot;view&quot; functions, and various other special fields
that can be used to query or manipulate other documents.</p>
<p>A couchapp is a design document with an attachment, usually called index.html.
These attachments are served directly by couchdb and can be accessed at a
known URL. You can put anything you like in that html file, and you could
just have a static page if you wanted. Usually however though it is is
an HTML page that uses javascript in order to display the results
of queries on the database. The user will then access the attachment on
the design document, and will interact with the resulting page.</p>
<p>In theory you can do anything you like in that page, but it is usual
to make use of standard tools in order to query the database and
provide information and opportunity for interaction to the user.</p>
<p>The first standard tool is jQuery, with a couple of plugins for
working with couchdb and couchapps specifically. These allow for
querying views in the database and acting on the results, retrieving
and updating documents, and plenty more.</p>
<p>In addition the couchapp tool sets you up with another jQuery
plugin called &quot;evently&quot;, which is a way to structure interactions
with jQuery, and change the page based on various events. I will
go in to more detail about how evently works in a later post.</p>
<p>In addition to all the client-side tools for interacting with the
database, it is also possible to make use of couchdb features such
as shows, lists, update handlers validation functions in order to move
some of the processing server-side. This is useful for various reasons,
including being more accessible, allowing search engines to index the
content, and not having to trust the client not to take malicious
actions.</p>
<p>The two approaches can be combined, and you can prototype with the
client-side tools, and then move some of the work to the server-side
facilities later.</p>
<p>Stay tuned for more on how a simple couchapp generates content based
on what is in the db.</p>
</div>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Tue, 28 Sep 2010 23:48 GMT</pubDate>
</item>
<item>
   <title>Directly logging in a user in Django tests</title>
   <guid isPermaLink="false">tech/17-directly-logging-in-a-user-in-django-tests</guid>
   <link>http://jameswestby.net/weblog/tech/17-directly-logging-in-a-user-in-django-tests.html</link>
   <description><![CDATA[
<div class="document">
<p>The <a class="reference external" href="http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.login">examples for Django testing</a> point you towards hardcoding a
username and password for a user to impersonate in tests, and
the API of the test client encourages this too.</p>
<p>However, Django has a nice pluggable authentication system that
means you can easily use something such as OpenID instead of
passwords.</p>
<p>Putting the passwords in your tests ties you to having the password
support enabled, and while you could do this for just the tests, it's
completely out of the scope of most tests (I'm not talking about any
tests for the actual login process here.)</p>
<p>When I saw this while reviewing code recently I worked with <a class="reference external" href="https://launchpad.net/~zkrynicki">Zygmunt</a>
to write a Client subclass that didn't have this restriction. With this
subclass you can just choose a User object, and have that client login
as that user, without them having to have a password at all. Doing
this decoples your tests from the implementation of the authentication
system, and makes them target the code you want to test more precisely.</p>
<p>Here's the code:</p>
<pre class="literal-block">
from django.conf import settings
from django.contrib.auth import login
from django.http import HttpRequest
from django.test.client import Client


class TestClient(Client):

    def login_user(self, user):
        &quot;&quot;&quot;
        Login as specified user, does not depend on auth backend (hopefully)

        This is based on Client.login() with a small hack that does not
        require the call to authenticate()
        &quot;&quot;&quot;
        if not 'django.contrib.sessions' in settings.INSTALLED_APPS:
            raise AssertionError(&quot;Unable to login without django.contrib.sessions in INSTALLED_APPS&quot;)
        user.backend = &quot;%s.%s&quot; % (&quot;django.contrib.auth.backends&quot;,
                                  &quot;ModelBackend&quot;)
        engine = import_module(settings.SESSION_ENGINE)

        # Create a fake request to store login details.
        request = HttpRequest()
        if self.session:
            request.session = self.session
        else:
            request.session = engine.SessionStore()
        login(request, user)

        # Set the cookie to represent the session.
        session_cookie = settings.SESSION_COOKIE_NAME
        self.cookies[session_cookie] = request.session.session_key
        cookie_data = {
            'max-age': None,
            'path': '/',
            'domain': settings.SESSION_COOKIE_DOMAIN,
            'secure': settings.SESSION_COOKIE_SECURE or None,
            'expires': None,
        }
        self.cookies[session_cookie].update(cookie_data)

        # Save the session values.
        request.session.save()
</pre>
<p>Then you can use it in your tests like this:</p>
<pre class="literal-block">
from django.contrib.auth.models import User


client = TestClient()
user = User(username=&quot;eve&quot;)
user.save()
client.login_user(user)
</pre>
<p>Then any requests you make with that client will be authenticated as the user that
was created.</p>
<p><a class="reference external" href="http://code.djangoproject.com/ticket/14350">Ticket submitted</a> with Django to have this available for everyone in future.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Tue, 28 Sep 2010 00:50 GMT</pubDate>
</item>
<item>
   <title>What I work on</title>
   <guid isPermaLink="false">ubuntu/19-what-I-work-on</guid>
   <link>http://jameswestby.net/weblog/ubuntu/19-what-I-work-on.html</link>
   <description><![CDATA[
<div class="document">
<p>I'm keen to try and write more about the things that I work on as part of my
job at Canonical. In order to get started I wanted to write a summary of some
of the things that I have done, as well as a little about what I am working on
now.</p>
<div class="section" id="ubuntu-distributed-development">
<h1>Ubuntu Distributed Development</h1>
<p>This isn't the catchiest name for a project ever, and has an unfortunate collision
with an <a class="reference external" href="http://udd.debian.org/">Debian project</a>, also shortened to &quot;UDD.&quot; However, the aim is for this
title to become a thing of the past, and this just to be the way things are done.</p>
<p>This effort is firstly about getting Ubuntu to use Bazaar, and a suite of associated
tools, to get the packaging work done. There are multiple reasons for this.</p>
<p>First, and most simply, is to give developers the power of version control as they
are working on Ubuntu packages. This is useful for both the large things and the
small. For instance I sometimes appreciate being able to walk through the history
of a package, comparing diffs here and files there when debugging a complex problem.
Sometimes though it's just being able to &quot;bzr revert&quot; a file, rather than having
to unpack the source again somewhere else, extracting the file and copying it over
the top.</p>
<p>There are higher purposes with the work too. The goal is to link the packaging
with the upstream code at the version control level, so that one flows in to
the other. This has practical uses, such as being able to follow changes as
they flow upstream and back down again, or better merging of new upstream
versions. I believe it has some other benefits too, such as being able to see
the packages more clearly as what they are, a branch of upstream. We won't just
talk about them being that, but they truly will be.</p>
<p>Some of you will be thinking &quot;that's all well and good, but &lt;project&gt; uses git,&quot;
and you are absolutely right. Throughout this work we have had two principles
in mind, to work with multiple systems outside of Ubuntu, and to provide a
consistent interface within Ubuntu.</p>
<p>Due to the way that Ubuntu works an Ubuntu developer could be working on any
package next. I would really like it if the basics of working with that package
were the same regardless of what it was. We have a lot of work to do on the
packaging level to get there, but this project gets this consistency on the
version control level.</p>
<p>We can't get everyone outside of Ubuntu to follow us in this though. We have to
work with the system that upstream uses, and also to work with Debian in the
middle. This means that we have to design systems that can interface between
the two, so we rely a lot on Launchpad's bzr code imports. We also want to
interface at the other end as well, at &quot;push&quot; time. This means that if an
Ubuntu developer produces a patch that they want to send upstream they can
do that without having to reach for a possibly different VCS.</p>
<p>Thanks mainly to the work of Jelmer Vernooij we are doing fairly well at being
able to produce patches in the format appropriate for the upstream VCS, but we
still have a way to go to close the loop. The difficultly here is more around
the hundreds of ways that projects like to have patches submitted, whether it
is a mailing list or a bug tracker, or in some other form. At this stage
I'd like to provide the building blocks that developers can put together as
appropriate for that project.</p>
</div>
<div class="section" id="daily-package-builds">
<h1>Daily package builds</h1>
<p>Relatedly, but with slightly different aims, I have been working on a project in
conjunction with the Launchpad developers to allow people to have daily builds of
their projects as packages.</p>
<p>Currently there is too often a gap between using packaged versions of a project,
and running the tip of that project daily. I believe that there are lots of people
that would like to follow the development of their favourite projects closely,
but either don't feel comfortable building from the VCS, or don't want to go through
the hassle.</p>
<p>Packages are of course a great way to distribute pre-compiled software, so it was
natural to want to provide builds in this format, but I'm not aware of many projects
doing that, aside from those which <a class="reference external" href="https://launchpad.net/~fta">fta</a> provides builds for. Now that Launchpad
provides PPAs and code imports, and the previous project provides imports of the
packaging of all Debian and Ubuntu packages in to bzr, all the pieces are there
in order to allow you to produce packages of a project automatically every day.</p>
<p>This is currently available in beta in Launchpad, so you can go and <a class="reference external" href="https://help.launchpad.net/Packaging/SourceBuilds/Recipes">try it out</a>,
though there are a few known problems that we are working on until it will be
as pleasant as we want.</p>
<p>This has the potential to do great things for projects if used correctly. It can
increase the number of people testing fresh code and giving feedback by orders
of magnitude. Also, just building the packages acts as a kind of continuous
integration, and can provide early warning of problems that will affect the
packaging of the project. Also, they provide an easy way for people to test
the latest code if a bug is believed to be fixed.</p>
<p>Obviously there are some dangers associated with automatic builds, but if they
are used by people who know what they are doing then it can help to close
the loop between users and developers.</p>
<p>There are also many more things that can be done with this feature by people
with imagination, so I'm excited to see what directions people will take
it in.</p>
</div>
<div class="section" id="fixing-things">
<h1>Fixing things</h1>
<p>Aside from these projects, I was also given some time to work on Ubuntu itself,
but without long-term projects to ship. That meant that I was able to fix things
that were standing in my way, either in the way of the above projects, or just
hampering my use of Ubuntu, or fix important bugs in the release.</p>
<p>In addition I took on smaller projects, such as getting kerneloops enabled by
default in Ubuntu. While doing this I realised that the user experience of that
tool could be improved a lot for Ubuntu users, as well as allowing us to report
the problems caught by the tool as bugs in Launchpad if we wished.</p>
<p>I really enjoyed having this flexibility, as it allowed me to learn about many
areas of the Ubuntu system, and beyond, and also played to my strengths of
being able to quickly dive in to a new codebase and diagnose problems.</p>
<p>I think that in my own small way, each of these helped to improve Ubuntu releases,
and in turn the projects that Ubuntu is built from.</p>
</div>
<div class="section" id="sponsoring">
<h1>Sponsoring</h1>
<p>While I'm sorry to say that other demands have pulled my code review time in to
other projects, I did used to spend a lot of time reviewing and sponsoring changes
in to Ubuntu.</p>
<p>I highlight this mainly as another chance to emphasise how important I think code
review is, especially when it is review of code from people new to the project.
It improves code quality, but is also a great opportunity for mentoring,
encouraging good habits, and helping new developers join the project. I hope
that my efforts in this are had a few of these characteristics and helped increase
the number of free software developers. Oh how I wish there were more time to
continue doing this.</p>
</div>
<div class="section" id="linaro">
<h1>Linaro</h1>
<p>I've now been started working on the Linaro project, specifically in the Infrastructure
team, working on tools and Infrastructure for Linaro developers and beyond. I'm not one
to be all talk and no action, so I won't talk to much about what I am working on, but
I would like to talk about why it is important.</p>
<p>Firstly I think that Linaro is an important project for Free Software, as it has the
opportunity to lead to more devices being sold that are built on or entirely
free software, some in areas that have historically been home to players that have
not been good open source citizens.</p>
<p>Also, I think tools are an important area to work on, not just in Linaro. They pervade
the development experience, and can be a huge pain to work with. It's important that
we have great tools for developing free software so as not to put people off. Developers,
volunteers and paid, aren't going to carry on too long with tools that cause them more
problems than they are worth, and not all are going to persist because they value
Free Software over their own enjoyment of what they do.</p>
</div>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">ubuntu</category>
   <pubDate>Tue, 14 Sep 2010 19:36 GMT</pubDate>
</item>
<item>
   <title>Improving the usability of launchpadlib-using code</title>
   <guid isPermaLink="false">tech/16-Improving-the-usability-of-launchpadlib-using-code</guid>
   <link>http://jameswestby.net/weblog/tech/16-Improving-the-usability-of-launchpadlib-using-code.html</link>
   <description><![CDATA[
<div class="document">
<p>Normally when you write some code using launchpadlib you end up with Launchpad
showing your users something like this:</p>
<img alt="/images/lplib-before.png" src="/images/lplib-before.png" />
<p>This isn't great, how is the user supposed to know which option to click? What
do you do if they don't choose the option you want?</p>
<p>Instead it's possible to limit the choices that the user has to make to only
those that your application can use, plus the option to deny all access, by
changing the way you create your Launchpad object.</p>
<pre class="literal-block">
from launchpadlib.launchpad import Launchpad

lp = Launchpad.get_token_and_login(&quot;testing&quot;, allow_access_levels=[&quot;WRITE_PUBLIC&quot;])
</pre>
<p>This will present your users with something like this:</p>
<img alt="/images/lplib-after.png" src="/images/lplib-after.png" />
<p>which is easier to understand. There could be further improvements, but they would
happen on the Launchpad side.</p>
<p>This approach works for both Launchpad.get_token_and_login and Launchpad.login_with.</p>
<p>The values that you can pass here aren't documented, and should probably be constants
in launchpadlib, rather than hardcoded in every application, but for now you can
use:</p>
<ul class="simple">
<li>READ_PUBLIC</li>
<li>READ_PRIVATE</li>
<li>WRITE_PUBLIC</li>
<li>WRITE_PRIVATE</li>
</ul>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Wed, 07 Jul 2010 20:17 GMT</pubDate>
</item>
<item>
   <title>Re: Getting the hobbyist back</title>
   <guid isPermaLink="false">tech/15-Re-Getting-the-hobbyist-back</guid>
   <link>http://jameswestby.net/weblog/tech/15-Re-Getting-the-hobbyist-back.html</link>
   <description><![CDATA[
<div class="document">
<p>Dear <a class="reference external" href="http://blogs.gnome.org/bolsh/2010/04/29/getting-the-hobbyist-back/">Mr Neary</a>, thanks for your thought provoking post, I think it is a
problem we need to be aware of as Free Software matures.</p>
<p>Firstly though I would like to say that the apparent ageism present in your
argument isn't helpful to your point. Your comments appear to diminish the
contributions of a whole generation of people. In addition, we shouldn't just
be concerned with attracting young people to contribute, the same changes will
have likely reduced the chances that people of all ages will get involved.</p>
<p>Aside from that though there is much to discuss. You talk about the changes in
Free Software since you got involved, and it mirrors my observations. While these
changes may have forced fewer people to learn all the details of how the system
works, they have certainly allowed more people to use the software, bringing many
different skills to the party with them.</p>
<p>I would contend that often the experience for those looking to do the compilation
that you rate as important has parallels to the experience of just using the software
you present from a few years ago. If we can change that experience as much as we
have the installation and first use experience then we will empower more people to
take part in those activities.</p>
<p>It is instructive then to look at how the changes came about to see if there are
any pointers for us. I think there are two causes of the change that are of interest
to this discussion.</p>
<p>Firstly, one change has been an increased focus on user experience. Designing
and building software that serves the users needs has made it much more palatable
for people, and reduced the investment that people have to make before using it.
In the same way I think we should focus on developer experience, making it more
pleasant to perform some of the tasks needed to be a hobbyist. Yes, this means
hiding some of the complexity to start with, but that doesn't mean that it can't
be delved in to later. Progressive exposure will help people to learn by not
requiring them to master the art before being able to do anything.</p>
<p>Secondly, there has been a push to make informed decisions on behalf of the user
when providing them with the initial experience. You no longer get a base system
after installation, upon which you are expected to select from the thousands of
packages to build your perfect environment. Neither are you led to download multiple
CDs that contain the entire contents of a distribution, much of which is installed
by default. Instead you are given an environment that is already equipped to do
common tasks, where each task is covered by an application that has been selected
by experts on your behalf.</p>
<p>We should do something similar with developer tools, making opinionated decisions
for the new developer, and allowing them to change things as they learn, similar
to the way in which you are still free to choose from the thousands of packages
in the distribution repositories. Doing this makes documentation easier to write,
allows for knowledge sharing, and reduces the chances of paralysis of choice.</p>
<p>There are obviously difficulties with this given that often the choice of tool
that one person makes on a project dicatates or heavily influences the choice
other people have to make. If you choose autotools for your projects then I can't
build it with CMake. Our development tools are important to us as they shape
the environment in which we work, so there are strong opinions, but perhaps
consistency could become more of a priority. There are also things we can do
with libraries, format specifications and wrappers to allow choice while still
providing a good experience for the fledgling developer.</p>
<p>Obviously as we are talking about free software the code will always be available,
but that isn't enough in my mind. It needs to be easier to go from code to
something you can install and remove, allowing you to dig deeper once you have
achieved that.</p>
<p>I believe that our effort around things like <a class="reference external" href="https://dev.launchpad.net/BuildBranchToArchive">https://dev.launchpad.net/BuildBranchToArchive</a>
will go some way to helping with this.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Fri, 30 Apr 2010 13:59 GMT</pubDate>
</item>
<item>
   <title>Summer of Code Student Application Deadline</title>
   <guid isPermaLink="false">ubuntu/18-summer-of-code-student-application-deadline</guid>
   <link>http://jameswestby.net/weblog/ubuntu/18-summer-of-code-student-application-deadline.html</link>
   <description><![CDATA[
<div class="document">
<p>The deadline for students to submit their applications to Google for Summer
of Code is imminent.</p>
<p>If you were waiting for the last minute to submit, that is now!</p>
<p>If you are mentor and have the perfect student you have been working with,
check with them that they have submitted the application to Google, otherwise
you will be stuck.</p>
<p>Next week we'll start to process the huge number of applications that we
have for Ubuntu.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">ubuntu</category>
   <pubDate>Fri, 09 Apr 2010 16:00 GMT</pubDate>
</item>
<item>
   <title>Caution: python-multiprocessing, threads and glib don't mix</title>
   <guid isPermaLink="false">tech/14-caution-python-multiprocessing-and-glib-dont-mix</guid>
   <link>http://jameswestby.net/weblog/tech/14-caution-python-multiprocessing-and-glib-dont-mix.html</link>
   <description><![CDATA[
<div class="document">
<p>If you don't want to read this article, then just steer clear of
python-multiprocessing, threads and glib in the same application. Let me
explain why.</p>
<p>There's a rather <a class="reference external" href="https://bugs.edge.launchpad.net/ubuntu/+source/gwibber/+bug/554005">famous bug</a> in <a class="reference external" href="https://launchpad.net/gwibber">Gwibber</a> in Ubuntu Lucid, where
a gwibber-service process will start taking 100% of the CPU time
of one of your cores if it can. While looking in to why this bug
happened I learnt a lot about how multiprocessing and GLib work,
and wanted to record some of this so that others may avoid the
bear traps.</p>
<p>Python's <a class="reference external" href="http://docs.python.org/library/multiprocessing.html">multiprocessing module</a> is a nice module to allow you to
easily run some code in a subprocess, to get around the restriction of
the GIL for example. It makes it really easy to run a particular function
in a subprocess, which is a step up from what you had to do before it
existed. However, when using it you should be aware how the way it works
can interact with the rest of your app, because there are some possible
nasties lurking there.</p>
<p><cite>GLib</cite> is a set of building blocks for apps, most notably used by GTK+.
It provides an object system, a mainloop and lots more besides. What we are
most interested here is the mainloop, signals, and thread integration that
it provides.</p>
<p>Let's start the explanation by looking at how multiprocessing does its thing.
When you start a subprocess using multiprocessing.Process, or something that
uses it, it causes a fork(2), which starts a new process with a copy of the
programs current memory, with some exceptions. This is really nice for
multiprocessing, as you can just run any code from that program in the
subprocess and pass the result back without too much difficulty.</p>
<p>The problems occur because there isn't an exec(3) to accompany the fork(2).
This is what makes multiprocessing so easy to use, but doesn't insert a clean
process boundary between the processes. Most notably for this example, it
means the child inherits the file descriptors of the parent (critically even
those marked FD_CLOEXEC).</p>
<p>The other piece to this puzzle is how the GLib mainloop communicates
between threads. It requires some mechanism where one thread can alert
another that something of interest happened. To do this when you tell
GLib that you will be using threads in your app by calling g_thread_init
(gobject.threads_init() in Python) then it will create a pipe for use by
glib to alert other threads.  It also creates a watcher thread that
polls one end of this pipe so that it can act when a thread wishes to
pass something on to the mainloop.</p>
<p>The final part of the puzzle is what your app does in a subprocess with
mutliprocessing. If you purely do something such as number crunching
then you won't have any issues. If however you use some glib functions
that will cause the child to communicate with the mainloop then you
will see problems.</p>
<p>As the child inherits the file descriptors of the parent it will use the
same pipe for communication. Therefore if a function in the child writes
to this pipe then it can put the parent in to a confused state. What
happens in gwibber is that it uses some gnome-keyring functions and that
puts the parent in to a state where the watcher thread created by
g_thread_init busy-polls on the pipe, taking up as much CPU time as it can
get from one core.</p>
<p>In summary, you will see issues if you use python-multiprocessing from
a thread and use some glib functions in the children.</p>
<p>There are some ways to fix this, but no silver bullet:</p>
<blockquote>
<ul class="simple">
<li>Don't use threads, just use multiprocessing. However, you can't
communicate with glib signals between subprocesses, and there's
no equivalent built in to multiprocessing.</li>
<li>Don't use glib functions from the children.</li>
<li>Don't use multiprocessing to run the children, use exec(3) a script
that does what you want, but this isn't as flexible or as
convenient.</li>
</ul>
</blockquote>
<p>It may be possible to use the support for different GMainContexts for
different threads to work around this, but:</p>
<blockquote>
<ul class="simple">
<li>You can't access this from Python, and</li>
<li>I'm not sure that every library you use will correctly implement it,
and so you may still get issues.</li>
</ul>
</blockquote>
<p>Note that none of the parties here are doing anything particularly
wrong, it's a bad interaction caused by some decisions that are known to
cause issues with concurrency. I also think there are issues when using
DBus from multiprocessing children, but I haven't thoroughly
investigated that. I'm not entirely sure why the multiprocessing child
seems to have to be run from a non-main thread in the parent to trigger
this, any insight would be welcome. You can find a small script to
reproduce the problem <a class="reference external" href="http://jameswestby.net/scratch/multiprocessing_bug.py">here</a>.</p>
<p>Or, to put it another way, global state bad for concurrency.</p>
</div>

]]></description>
   <category domain="http://jameswestby.net/weblog">tech</category>
   <pubDate>Thu, 08 Apr 2010 12:01 GMT</pubDate>
</item>
</channel>
</rss>
