title: How can i thread image processing in NodeJS?
body: <p>I am writing a image processing web service on NodeJS and in basic i need to figure out how to make the actual image processing threaded. When i test it through Apache AB NodeJS is only using one core and stalling, i am surely doing something wrong here. How can i redesign this simple app to make use of the multiple cores on my server CPU.</p>
  
  <p>I scaled away all input filtering and simplified the image processing function to give you a idea of the app structure instead of long code bits.</p>
  
  <p><strong>In app.js</strong></p>
  
  <pre><code>app.get('/generate/:q', function(req, res){
      var textString = req.params.q;
      res.setHeader("Content-Type", "image/png");
      res.end(generator.s());
  });
  </code></pre>
  
  <p><strong>In generate.js</strong></p>
  
  <pre><code>var Canvas = require('canvas')
    , Font = Canvas.Font
    , fs = require('fs')
    , path = require("path")
    , plate = new Canvas.Image;
  
  //To keep plate in RAM between requests.
  fs.readFile(__dirname + '/plates/dhw132.png', function(err, squid){
      if (err) throw err;
      plate.src = squid;
  });
  
  exports.s = function () {
      canvas = new Canvas(731,1024);
      ctx = canvas.getContext('2d');
      ctx.drawImage(plate, 0, 0, plate.width, plate.height);
      return canvas.toBuffer();
  }
  </code></pre>
  
  <p>How can i rewrite this to make the generator.s() threaded?</p>