<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rss="http://purl.org/rss/1.0/" xmlns:a="http://www.w3.org/2005/Atom" xmlns:dt="http://xsltsl.org/date-time" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns="http://www.w3.org/1999/xhtml">
  <rss:channel rdf:about="http://yoan.dosimple.ch/blog/">
    <rss:title>Yoan Blanc’s weblog</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/</rss:link>
    <rss:description>The boring weblog of Yoan
    Blanc</rss:description>
    <rss:items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2011/05/31"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2011/04/30"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2011/03/31"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2011/02/28"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2011/01/31"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2010/12/28"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2010/11/29"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2010/10/20"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2010/05/05"/>
        <rdf:li rdf:resource="http://yoan.dosimple.ch/blog/2010/04/24"/>
      </rdf:Seq>
    </rss:items>
  </rss:channel>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2011/05/31">
    <rss:title>Alpage</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2011/05/31</rss:link>
    <dc:date>2011-05-31T12:00:00+01:00</dc:date>
    <dc:subject>Life Alp Master Me</dc:subject>
    <content:encoded><![CDATA[<p>
I’m doing this again, spending a summer working in the Alp with cows, cheese,
alpine ibexes and everything that come with that. Because there is a balance
with my other life I guess, because it brings a rythm I’m looking for. Some
kind of peace.
</p><p>
After that, I’ll try to go back to school, doing a Master in
Computer Science (because I have no choices in fact) in either the EPFL or the
Universities of Bern, Neuchâtel and Fribourg. I see this as being an exit door
to what I’m currently doing for a living and an opening door to a new world of
opportunities which must not be related to the everywhere Internet.
</p><p>
Many people of my family or friends seem to not follow my moves and not
understanding and seeing the meaning behind them. I can tell you, there is
nothing particular I’m looking for except the thing I know the most, myself.
</p><p>
Going back to the Alp has a strong meaning, the work there is repetitive.
You have to milk, do the cheese every single day no matter how you
are, what the weather is or if something went wrong. You have to do it,
working in rythm with the animals, the nature, the weather, everything
surrounding you. It directs, inhibits my eager of discoveries, knowledge,
questions, novelties, forcing me to focus on what matters. And what matters
isn’t related to myself, it’s what matter for the whole, the bigger stuff I’m
part of.
</p><p>
To be honest with you, working as a Freelance is a way to not be part of
anything.
</p><p>
Now the studies, what’s hidden behind this. I’m only describing the idea I may
have now which won’t be what it’s gonna bring while doing it. It’s again about
going back to something, somewhere. Being humble enough to say, I don’t know
shit and I need to be told because I’m ready to listen and learn, instead of
pretending I know what I, in fact, ignore. Also, if we learned a lot at the
engineering school the focus was really on practice and my theroritical basics
as been missed. Sometimes I can feel that lack and it’s a gap that can be
filed.
</p><p>
I had the opportunity to give some lessons at an engineering school and it might
be something I could do in the future once I’ve learned how to be nice and to
answer kindly to stupid and repetitive questions. They are days where I feel
more like Linus than Richard when it comes to explaining something very simple
to smart people. We need both of them but not everyone likes when you hurt
their feelings.
</p><p>
And the most important thing, maybe is because I can. I don’t know how hard it
will be but I know I can try without worring about money or people that
would be affected by this. (Put here a little “forever alone” rage comic head)
</p><p>
You are who you are, nothing you do, own or make define you.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2011/04/30">
    <rss:title>Pure PHP web server</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2011/04/30</rss:link>
    <dc:date>2011-04-30T12:00:00+01:00</dc:date>
    <dc:subject>PHP Server Socket WSGI</dc:subject>
    <content:encoded><![CDATA[<p>
    Over a discussion about doing <a href="http://dev.w3.org/html5/websockets/" hreflang="en">WebSocket</a> in PHP on Reddit, I realized that I still had a
    wrong opinion on this language, considering it as being the scripting
    language tied to a web server (commonly mod_php, php-fpm or fastcgi). I can
    also be a scripting language for running tasks, like many web frameworks
    take advantage of. But you can also write your own server in pure PHP,
    working at the UDP/TCP level.
</p><p>
    Let’s build a web server (aka HTTP server).
</p><pre>#!/usr/bin/env php
&lt;?php
$socket = <a href="http://ch.php.net/stream_socket_server">stream_socket_server</a>(
 'tcp://0.0.0.0:8000', $errno, $errstr
);

if (!$socket) {
 echo "$errstr ($errno)", PHP_EOL;
} else {
 while ($conn = <a href="http://ch.php.net/stream_socket_accept">stream_socket_accept</a>($socket, -1)) {
  $request = '';
  while (substr($request, -4) !== "\r\n\r\n") {
   $request .= fread($conn, 1024);
  }
  $response = &lt;&lt;&lt;EOS
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 12

Hello World!
EOS;
  fwrite($conn, $response);
  fclose($conn);
 }
}</pre><p>
    This is pretty much it. As PHP isn’t multi-threaded maybe the <a href="http://php.net/libevent" hreflang="en">libevent</a> library can help
    but I didn’t manage to install it. Or you could fork it
    using the <code>pcntl_fork</code> way. It actually works, badly but works.
</p><p>
    Let’s create a <a href="http://www.wsgi.org/wsgi/" hreglang="en">WSGI</a>
    for PHP. Maybe not.
</p><p>
    I’m now wondering who would use this for real production stuff. It’s not
    close from what exists in the other web servers like Unicorn, Gunicorn, …
    even if they can leverage threads, native code and existing libraries more
    easily I guess. Not an expert though.
</p><p>
    The code is on Github: <a href="https://gist.github.com/949850">pure PHP
    web servers (non-concurrent and concurrent)</a>
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2011/03/31">
    <rss:title>Safer uploads or don’t trust a browser</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2011/03/31</rss:link>
    <dc:date>2011-03-31T12:00:00+01:00</dc:date>
    <dc:subject>PHP Python libmagic Security</dc:subject>
    <content:encoded><![CDATA[<p>
We did tell you to no trust the browser right? Or the user/bot behind it. And to filter, check, sanitize everything that comes from him, her or it.
</p><p>
So you check the basics like <code>GET</code> and <code>POST</code> arguments. And maybe you check the values coming for the cookies as well. But do you check the file uploading? You know that <code>type</code> arguments sent. This how a request look like:
</p><pre>POST /…
…
Content-Type: multipart/form-data; boundary=boundary

--boundary
Content-Disposition: form-data; name="key"

value
--boundary
Content-Disposition: form-data; name="file"; filename="image.png"
<strong>Content-Type: image/png</strong>

…Your PNG image here…
--boundary--
</pre><p>
The <code>Content-Type</code> field is guessed by the browser and most of the time on the file extension. But it can decide to send whatever it wants like application/octet-stream which just says that it’s a binary file.
<p/>
So, you shall not trust it and always verify what you get using the proper tool, like the UNIX command <a href="http://www.darwinsys.com/file/" hreflang="en">file</a> PHP ships <a href="http://www.php.net/manual/en/book.fileinfo.php" hreflang="en">with it</a> and Python has a wrapper <a href="http://pypi.python.org/pypi/python-magic/" hreflang="en">called magic</a>. That how you use it in Python in a WSGI application (using WebOb).
</p><pre>import magic
from webob import Request

def application(environ, start_response):
 req = Request(environ, charset="utf-8")
 <i># sent from the browser</i>
 uploaded_file = req.POST.get("file")
 <i># what we actually got</i>
 guessed_type = uploaded_file.type
 magic_type = magic.from_buffer(uploaded_file.read(1024),
                                mime=True)
 uploaded_file.seek(0)
 <i>"… whatever else …"</i>
</pre><p>
This way you can really white list the file you users are uploading and prevent them for sending you .mov, .doc or whatever when they think they are uploading a new avatar for their profile. True story.
</p><p>
<a href="https://gist.github.com/896241">The code is on github</a>
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2011/02/28">
    <rss:title>Migrating data from and to MySQL</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2011/02/28</rss:link>
    <dc:date>2011-02-28T12:00:00+01:00</dc:date>
    <dc:subject>MySQL ETL</dc:subject>
    <content:encoded><![CDATA[<p>
In the current project I’m working on, we are currently in the process of migrating the old data to their new model, and it’s way more complex that it first seemed. Mostly because the old database is a mess but that not the point here. I want to expose the way we choose to do this, a way that is efficient. It must be a better way, so speak your mind.
</p><p>
At the end this operation will happen once, but during the current development we might be running them a lot while adding new data sets to it.
</p><p>
Let’s dive, on how we import the data into the new database:
</p><pre>LOAD DATA INFILE
 "/tmp/data.dat"
INTO TABLE
 table_name;</pre><p>
That’s very fast, and can be faster if you allow more space for the InnoDB logs.
</p><p>
Now, where does that tmp/data.dat file come from what does it look like. This file has been generated by another request ran on the old database.
</p><pre>SELECT
 id, name, some_value
FROM
 old_table
INTO OUTFILE
 "/tmp/data.dat";</pre><p>
This is a trivial one, recreating a new table with three columns. By default the output format is a tab separated file where NULL has been replaced by \N.
</p><p>
If your old data base — like it’s our case — is still in Latin 1 or 15 (in Europe), using iconv on the .dat file can convert it to, for example, utf-8.
</p><pre>iconv \
 -f iso-8859-15 \
 -t utf-8 \
 /tmp/data.dat &gt; /tmp/data_utf8.dat
mv /tmp/data{_utf8,}.dat</pre><p>
We got tricked by some cp-1252 characters mixed with the Latin 15 ones, beware of those.
</p><p>
And to finish this, two tipps if your old data are too messed up.
</p><p>
You are using text fields in the old data but will use a dedicated table now one so you should use a temporary table for this kind of mapping, i.e. country.
</p><p>
You cannot get all the data at once and need to compute stuff for some rows. In our case moving the pictures to a dedicated table and linking them back afterwards. So we load the data that need to be updated into a temporary table and then perform on update SQL statement to update them all at once, they work like natural joins.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2011/01/31">
    <rss:title>Posting on a Page from a Facebook Application</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2011/01/31</rss:link>
    <dc:date>2011-01-31T12:00:00+01:00</dc:date>
    <dc:subject>PHP Python Facebook Page</dc:subject>
    <content:encoded><![CDATA[<p>
No revolutions here but since <a href="http://developers.facebook.com/docs/" hreflang="en">Facebook’s documentation</a> always lacks a bit behind because they move too fast, it might be useful to some of you.
</p><p>
First things to know is that pages are owned by users not applications and you can only interact with facebook as an application on the behalf of a user. First thing first, you need <a href="http://www.facebook.com/developers/" hreflang="en">to create an application</a>.
</p><p>
To act on <a href="http://www.facebook.com/pages/create.php">a Page</a> from an application, the administrator of a Page your application want to administrate must give you the permission to manage his pages. <a href="http://developers.facebook.com/docs/authentication/permissions" hreflang="en">This permission</a> is called, no surprises here: <code>manage_pages</code>.
</p><p>
Once the permission is granted, an <i>access_token</i> can be retrieved from <code>https://graph.facebook.com/me/accounts</code> and used from the application to interact with the page.
</p><p>
Nowadays Facebook applications should be made using mostly the <a href="http://developers.facebook.com/docs/reference/javascript/" hreflang="en">JavaScript SDK</a> to login, logout and so forth. That said, ours gonna be static only as we don’t need more.
</p><script src="https://gist.github.com/804679.js?file=index.html"/><p>
Okay, now you have an application and a page with its <i>access_token</i>. Let’s have fun with the <a href="http://developers.facebook.com/docs/api" hreflang="en">Graph API</a>. Frankly, there isn’t much I can say against the Graph API. It’s a nice example of REST API build upon a very complex data schema.
</p><p>
Let’s submit something.
</p><pre>&lt;?php
$fb = new Facebook(array(
 appId =&gt; null,
 secret =&gt; null
));
$message = 'Hello World!';
$access_token = /* token for the page */;
$fb-&gt;api(
 '/pageNameOrId/feed',
 'POST',
 compact('access_token', 'message')
);</pre><p>
This a simple message, you can make it way more complex. The message will appear as being posted by your application, so pick a cool name.
</p><p>
<a href="https://gist.github.com/804679" hreflang="en" title="source code">This is it.</a>
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2010/12/28">
    <rss:title>The Lithium way</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2010/12/28</rss:link>
    <dc:date>2010-12-28T12:00:00+01:00</dc:date>
    <dc:subject>PHP Lithium Monkeypatching</dc:subject>
    <content:encoded><![CDATA[<p>
I’m currently working with <a href="http://lithify.me/" hreflang="en">Lithium</a> (aka li3 or Cake 3) mostly for more than a month and would like to share some structure they’ve been putting it and that I like.
</p><p>
I like them because they made that framework so much flexible in a way you can plug anything at almost any places or add — what they call — <a href="http://lithify.me/docs/lithium/util/collection/Filters" hreflang="en">filters</a> to certain key methods or functions to tweak their behaviour at your needs.
</p><p>
Let’s start with the mentioned filters. A filter will wrap the body of a (static) function or method and let you act before / after the initial work, very much like monkeypatching.
</p><pre>&lt;?php
namespace monkey;

class Math extends \lithium\core\StaticObject {
 static function add($a, $b) {
  $params = compact('a', 'b');
  return static::_filter(__FUNCTION__, $params, function($self, $params) {
   extract($params);
   return $a + $b;
  });
 }
}

Math::add(2, 2); <i>// 4</i></pre><p>
As you can see we are using PHP 5.3 features like <a href="http://php.net/namespace" hreflang="en">namespace</a>, <a href="http://php.net/manual/en/functions.anonymous.php" hreflang="en">closures</a> and <a href="http://php.net/manual/en/language.oop5.static.php">late static binding</a>.
</p><ul>
<li>The namespace enable you to avoid Zend Framework alike naming, here we are in the <code>monkey</code> namespace.</li>
<li>A closure is used to describe the body of the function, what it’s gonna do. Before that PHP had only eval-like function (via create_function) that were very ugly to create and use.</li>
<li>And last but not least, the late static binding lives in <code>static::</code> (which is close to <code>self::</code>) but will always be the class where it’s written and not any sub classes (that’s what <code>self::</code> is for)</li>
</ul><p>
Just in case you didn’t know, PHP 5.2 is <a href="http://www.php.net/archive/2010.php#id2010-12-16-1" hreflang="en">not supported anymore</a>. At least before its <a href="http://www.php.net/archive/2010.php#id2010-12-09-1" hreflang="en">next release</a>. <em>Jamais deux sans trois.</em> (never two times without a third time).
</p><p>
Now, let’s hack this.
</p><pre>
&lt;?php
use lithium\util\collection\Filters;

Filters::apply('\monkey\Math', 'add', function ($self, $params, $chain) {
 extract($params);
 $ret = $chain-&gt;next($self, $params, $chain);
 fwrite(STDERR, "$a + $b = $ret");
 return $ret;
});

use monkey\Math;
Math::add(1, 2); <i>// 3</i>
<i>// stderr: 1 + 2 = 3</i></pre><p>
For those who don’t know, stderr is the error output on UNIX. You can pipe / redirect it to another location using <code>2&gt;</code>.
</p><p>
Inside lithium this kind of filter are mostly used in the <a href="http://lithify.me/docs/lithium/action/Dispatcher" hreflang="en">Dispatcher</a>. The Dispatcher is the class that handles all the requests, matching a route with an existing controller. So you can do exactly what you would do with WSGI/Rack/… middlewares.
</p><p>
Methods can aslo be written in a way you can filter them afterwards.
</p><pre>&lt;?php
namespace monkey;

class Tweeter extends \lithium\core\Object {
 function tweet($message) {
  $params = compact('message');
  return $this-&gt;_filter(__METHOD__, $params, function ($self, $params) {
   echo 'tweet: ', $params['message'], "\n";
  });
 }
}

$t = new Tweeter();
$t-&gt;tweet(’Hello’); <i>// Hello</i>
$t-&gt;applyFilter('tweet', function ($self, $params, $chain) {
 $params['message'] .= ' ^YB';
 return $chain-&gt;next($self, $params, $chain);
});

$t-&gt;tweet('Hello') <i>// Hello ^YB</i></pre><p>
Pardon my examples to be simplistic. A great usage of all this are tests. By performing surgerical modification inside your objects, it becomes very easy to tweak everything without bloating the initial object much. I agree that filterable methods or functions aren’t as readable as normal ones.
</p><p>
And because on major thing you do in Object Oriented Programming is to link objects, instances together here comes the last pattern, kind of, you may find into Lithium. I dunno exactly if it’s Inversion of Control or Dependencies Injection. I’ve stayed away from Java a too long time to know.
</p><pre>&lt;?php
namespace monkey;

class MyObject extends \lithium\core\Object {
  protected $_classes = array(
   'math' =&gt; '\monkey\Math'
  );

  protected $_autoConfig = array('classes');

  public function plus1($n) {
   $math = $this-&gt;_classes['math'];
   return $math::add($n, 1);
  }
}

$obj = new MyOject(array('classes' =&gt; array(
 'math' =&gt; '\ape\Math'
)));

$obj-&gt;plus1(2); <i>// 3</i></pre><p>
I always feel silly having such useless example but I hope that you get the point. MyObject isn’t linked to a particular Math class but can use a different one if needed with no code modifications.
</p><p>
By the way, <a href="http://symfony-reloaded.org/" hreflang="en">Symfony2</a> uses something similar with a <code>_class</code> suffix in the options.
</p><p>
From a JavaScript, Ruby or even Python point of view, this is the poor’s man monkeypatching and it involves a really heavy syntax for few gains. As I’m still complaining for every <code>array</code> I have to type, this is no big deal.
</p><p>
Don’t forget to join <a href="irc://irc.freenode.net/#li3" hreflang="en">#li3 on freenode</a>.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2010/11/29">
    <rss:title>Freelancer, one year later</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2010/11/29</rss:link>
    <dc:date>2010-11-29T12:00:00+01:00</dc:date>
    <dc:subject>Freelance Life Work</dc:subject>
    <content:encoded><![CDATA[<p>
Now that I left the chalet and am back into business, I’d like to share some thoughts about it.
</p><p>
One year ago, I was leaving <a href="http://eboutic.ch/wilkommen" hreflang="de">Eboutic.ch</a> and started being my own boss. Being your own boss means nothing much as you’ll still be working for someone. That someone will just change more often that it was before.
</p><p>
I really never asked me this question before: <em>“Why would someone hire you for?”</em> What I would be doing there was more clear to my mind but the why wasn’t. And the situation is mostly unpleasant.
</p><p>
If you’re doing well and have no problems in either deadline or staffing, then why would you hire a freelancer? No reasons. Obviously, it starts with a messed up situation. Brilliant!
</p><p>
Your clients are in trouble and they need someone to just fix it. To just fix it, nothing more because the urge of a deadline or an already exploded budget (that your salary will blow up even more) doesn’t allow more time. Time is cruxial, always.
</p><p>
Cruxial because, quite often, they need you right on. It is also true with companies but they don’t care much if you don’t work on first day, or week, or maybe month from my experience.
</p><p>
Let’s recap, you’ll join a project that might be late, with no more budget and with no controls of what have been done and no way to have an impact on it because you’re not supposed to do so.
</p><p>
I didn’t sign for this, really!
</p><p>
I might be lucky with the actual project where I’ve got the opportunity to learn a lot on human relationships and project managment, being put in the center of those. And also to teach some stuff, to set up good practices and tools. It’s hard and painful as well but at least here is somewhere I can put my guts in.
</p><p>
There is always something to learn out of anything and being freelance gives me the opportunity to do that more often.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2010/10/20">
    <rss:title>Life: unplugged</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2010/10/20</rss:link>
    <dc:date>2010-10-20T12:00:00+01:00</dc:date>
    <dc:subject>Life Me</dc:subject>
    <content:encoded><![CDATA[<p>
As <a href="http://yoan.dosimple.ch/blog/2010/05/05/" hreflang="en">you may know</a>, early this year I decided to live an experience and worked at L'Étivaz, then Le Gros Jable, as a farmer (farmer-helper). My work was focused on the three months we spent at l'Alpage (a chalet in the mountain, at 1824m above the sea) where cheese is made.
</p><p>
One of my goals going there was: <em>to let go</em>. I let everything aside: work, flat, computer, loves and worries. None of these define who you are.
</p><p>
My work usually doesn't require any particular rythm. I can start when I'm ready, stop when I'm bored (or it's finished), eat whenever I'm hungry. Dealing with cows that have to be milked twice a day, milk, cheese fabrication, weather and whatever occurs was a real shift. It's also a daily routine where 80% of what you do has to be done that day at a particular time. This repeatition was far from being boring, surprisingly. You start to master some operations, getting better at them, getting faster, parallelizing what can be. At the beginning everything was very intense in how much attention was required and as the time goes none of them remained as something hard to do. Even cleaning the shit out of where the 43 cows were resting during the day was something that became easy.
</p><p>
What about now? I don't know yet, I'll get back on tracks and catch up with projects I was involved in and try to write about stuff I'm doing to gain visibility. Ain't it right?
</p><p>
As a conclusion, this was a huge experience like many others I did in the past. Surprisingly not the one that created the biggest shock of all. I start learning to go through changes it seems.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2010/05/05">
    <rss:title>A little bit of fresh air</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2010/05/05</rss:link>
    <dc:date>2010-05-05T12:00:00+01:00</dc:date>
    <dc:subject>Life Me</dc:subject>
    <content:encoded><![CDATA[<p>
Back in March, I was participating to a ski camp with three classes of 11 years old kids. It was an awesome experience that put a little seed in my mind. This kind of seed that made me moving to Paris and later Oslo but with another motivation this time: working outside.
</p><p>
No more air conditioning, wheel chairs, dual screens, coffee break, cantine, daily standup, weekly meeting, headset, skype calls or who’s-the-hell-broke-the-build mornings. It’d be great to get that body to finally do something (gym doesn’t count and I’m not really into it) and let that full of not-that-important-stuff brain relax a bit.
</p><p>
Via sab.ch, I found some farmers looking for a hand this summer, applied and accepted. This means I’ll know how to milk cows and make cheese by this automn, will let any computer-related activities aside for a while and concentrate on a work that is more meaningful, at least seems to.
</p><p>
I’ll be <a href="http://maps.google.com/maps?f=q&amp;hl=de&amp;geocode=&amp;time=&amp;date=&amp;ttype=&amp;q=E+7%C2%B0+11%27+19.85%22+N+46%C2%B0+25%27+47.08%22&amp;ie=UTF8&amp;om=1&amp;ll=46.416796,7.189693&amp;spn=0.07006,0.154324&amp;t=h&amp;z=13">up there</a>, send me a postcard.
</p><p>
This ends mid-October and I don’t know yet what I’ll do then. Freelancing as I do since December is the logical way but as I know me, I don’t know what’s ahead and it’s a good feeling.
</p>]]></content:encoded>
  </rss:item>
  <rss:item rdf:about="http://yoan.dosimple.ch/blog/2010/04/24">
    <rss:title>Going Async</rss:title>
    <rss:link>http://yoan.dosimple.ch/blog/2010/04/24</rss:link>
    <dc:date>2010-04-24T12:00:00+01:00</dc:date>
    <dc:subject>Asynchronous Eventlet Gevent Gunicorn Node.js Tornado Twisted Ruby EventMachine</dc:subject>
    <content:encoded><![CDATA[<p>
I’ll talk about some back-end stuff for a change, showing what can kill your server especially when it comes to external data (aka I/O) and hopefully how solutions are being found.
</p><p>
Let’s take that simple piece of PHP code :
</p><pre>&lt;?php
$req = "http://pipes.yahoo.com/pipes/pipe.run?_id=…&amp;_render=php";
$data = unserialize(file_get_contents($req));</pre><p>
It grabs some external content and might take some time to achieve this depending of Yahoo! Pipes. This is where the problem starts to itch. When the server gonna call that page it’ll do mostly nothing but waiting for the external data to arrive.
</p><p>
Using Apache, a process or a thread is spawned, waiting on external data and, more importantly, consuming memory and CPU by just waiting.
</p><p>
Going asynchronous would mean being able to do other stuff, answer to other requests while waiting.
</p><p>
Unfortunately, I don’t know any solutions for that problem in PHP. PHP is designed to be executed as fast as possible and not for doing heavy operations. Big websites that are using PHP use it in general as pure front-end language (for example, the before-Google YouTube).
</p><p>
They are a couple of solutions to go async. Some come from a specific language that is asynchronous by design, like JavaScript (with node.js) or Erlang (to name a few). Ruby can achieve this by using <a href="http://rubyeventmachine.com/" hreflang="en">EventMachine</a> and Python (which I explored more) has a couple of solutions (<em>Twisted</em>, <em>Tornado</em> (coming from FriendFeed, bought by Facebook), <em>Eventlet</em>, <em>Gevent</em>, …). Take this dummy WSGI code:
</p><pre>from time import sleep

def application(environ, start_response):
 start_response("200 OK",
		[("Content-Type", "text/plain")])
 sleep(1)
 return ["Hello, world!"]</pre><p>
The external stuff you’re waiting on is simulated by a <code>sleep</code>, which makes it easy to understand. Up to you to fetch external data that will take a fixed time to arrive (which is the same).
</p><p>
When you run this particular code, a process/thread will be dedicated for that during one second blocking other incoming ones, right? You’ll need as many workers/threads/processes as requests to handle them all in one second. Suboptimal since those particular processes aren’t doing anything special.
</p><p>
Take a look at <a href="http://gist.github.com/376416">some tries</a> I made.
</p><p>
<a href="http://www.eventlet.net" hreflang="en">Eventlet</a> and <a href="http://www.gevent.org/" hreflang="en">Gevent</a>, the more convenient to use <abbr title="in my humble opinion">imho</abbr>, will run each incoming requests into a coroutine (like a sub process) enabling the main process to do something else when some time is available. The only thing to change in the code above is where the sleep function comes from.
</p><pre>from eventlet import sleep
from gevent import sleep</pre><p>
And, of course, to run it using the appropriate server (the one from the library, <a href="http://www.gunicorn.org/" hreflang="en">Gunicorn</a> or <a href="http://pypi.python.org/pypi/Spawning/" hreflang="en">Spawning</a> (Eventlet only)). Those libraries are doing monkeypatching so no code has to be modified in order to become asynchronous. I.e. using <code>urllib.urlopen</code> to fetch external data will not block the whole process. No code changes are required.
</p><p>
<a href="http://www.twistedmatrix.com/" hreflang="en">Twisted</a> or <a href="http://www.tornadoweb.org/" hreflang="en">Tornado</a>, for example, are using a reactor model, which forces you to handle asynchronous code with callbacks. It’s closer to the metal but the learning curve might gives you some headaches. I do love Twisted, but it’s sometimes just too much, really.
</p><p>
Python has another project, running on top of <a href="http://stackless.com/" hreflang="en">Python Stackless</a>, called <a href="http://code.google.com/p/syncless/" hreflang="en">Syncless</a> dedicated to async code. It looks quite promising.
</p><p>
Ruby 1.9 will get coroutines under the name of <a href="http://ruby-doc.org/core-1.9/classes/Fiber.html" hreflang="en">Fibers</a>, until that <em>EventMachine</em> seems the way to go. It looks like super clean <em>Twisted</em> too me. I hope not hurting too much feeling by saying that. (BTW, there is a great article from the <em>SuperFeedr</em> guys: <a href="http://blog.superfeedr.com/open/ruby/ruby-fibers-may-confuse/" hreflang="en">Ruby Fibers may confuse</a>)
</p><p>
One very interesting project, and very young as well, is <a href="http://www.nodejs.org" hreflang="en">node.js</a>. Basically server-side JavaScript. This one is asynchronous by design and totally awesome if you love JavaScript or coming out of hell if you don’t like it. I do like it.
</p><p>
With the web evolving the way it does, where data is coming from multiple sources, heavy or special tasks are delegated to specialized units (SOA), I clearly see this asynchronous idea as an ongoing paradigm shift. Today’s bottleneck is the I/O, most often represented by the database.
</p><p>
Don’t forget that the backend performances aim only to serve more people, faster but 80-90 % of the time is spent on the client side. I’m a frontend guy after all.
</p>]]></content:encoded>
  </rss:item>
</rdf:RDF>

