• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
NodeJS Concurrency

NodeJS Concurrency



A discussion of concurrency features in NodeJS: the good and the less-than-good.

A discussion of concurrency features in NodeJS: the good and the less-than-good.

An overview of using Web Workers to address the less-than-good.



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Has featural development on this stopped? Well if it's complete it's complete I suppose! Though I can't get the thing to work. Both require('webworker') and require('../lib/webworker') result in 'object not found' or some such...
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    NodeJS Concurrency NodeJS Concurrency Presentation Transcript

    • Concurrency in NodeJS PeterGriess, Yahoo! Inc. pg@std.in | @pgriess| http://blog.std.in
    • NodeJS Concurrency Model • Single process • Single thread • Non-preemptive • Conncurrency provided by multiple processes
    • The Bad • Can’t utilize multiple cores by default – Modern server-class hardware is at least 4x • Easy to stall request processing – Invoke a method that blocks (there are some) – Run some CPU-intensive code
    • The Good • No race conditions (almost) • No locking strategies to get wrong • Comparatively easy to reason about • Processes are a powerful sandbox • QoS: nice(2), ionice(1), etc. • Permissions: chroot(2), setuid(2), etc. • Fault isolation
    • child_process • Node has an API for creating child processes, child_process.spawn() • It has problems – Very UNIX; not friendly to JavaScript developers – IPC is DIY (transport? data format?)
    • Web Workers • A rich but (mostly) easy-to-use JavaScript API – Spawn new workers – Send and receive messages to/from workers – Worker lifecycle management (creation, termination, etc) • A Worker executes a JavaScript file/URL • Separate process and execution context • A W3C standard, part of the “HTML5” bucket; designed for browsers
    • Shameless plug: node-webworker • A Web Workers implementation for NodeJS • npm install webworker • Interested about the design? – http://blog.std.in/2010/07/08/nodejs-webworker- design/
    • Ping/pong example: parent.js var Worker = require(‘webworker/webworker’).Worker; varw = new Worker(‘/Users/griessp/child.js’); w.onmessage = function(e) { console.log(e.data); }; w.onerror = function(e) { console.log(‘Error: ‘ + e.message); w.terminate(); }; w.postMessage({ping : ‘pong’});
    • Ping/pong example: child.js onmessage = function(e) { console.log(e.data); postMessage([e.data.ping, {Hello : ‘world’}]); setTimeout(function() { throw new Error(‘Ohnoes!’); }, 1000); }; onclose = function() { console.log(‘Child: Shutting down.’); };
    • Ping/pong example: output % node ./parent.js { ping: 'pong' } [ 'pong', { Hello: 'world' } ] Parent error: Oh noes! Child: Shutting down.
    • Ping/pong example: hilights • Easily spawn a new process! • Pass complex messages! • Exceptions bubble up to the parent! • Graceful shutdown! • This code will work in a browser with (almost) no changes. Hooray standard APIs!
    • node-webworkers: Extensions • Gracefully shut down workers with onclose() • Watch for worker termination with onexit() • Send file descriptors with postMessage()