forked from DoubleSpout/node-threads-a-gogo2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.json
72 lines (72 loc) · 18.5 KB
/
package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{
"name": "tagg2",
"version": "0.2.0",
"main": "index.js",
"description": "full name is threads_a_gogo2,it is a cross platform support, Simple and fast JavaScript threads for Node.js",
"keywords": [
"The",
"most",
"awesome",
"project",
"ever",
"a gogo"
],
"author": {
"name": "doublespout",
"email": "[email protected]"
},
"homepage": "https://github.com/DoubleSpout/node-threads-a-gogo2",
"bugs": {
"url": "https://github.com/DoubleSpout/node-threads-a-gogo2/issues",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/DoubleSpout/node-threads-a-gogo2.git"
},
"scripts": {
"preinstall": "node install.js",
"install": "node-gyp rebuild",
"test": "node ./test/main_test.js"
},
"os": [
"macos",
"linux",
"darwin",
"win32"
],
"engines": {
"node": ">:z=0.8.0"
},
"dependencies":{
"generic-pool": "2.0.3"
},
"contributors": [
{
"name": "//Threads_a_gogo AUTHORS"
},
{
"name": "2011-11-06 Jorge Chamorro Bieling",
"email": "[email protected]"
},
{
"name": "2011-11-25 Juan Falgueras Cano",
"email": "[email protected]"
},
{
"name": "2012-01-26 Bruno Jouhier",
"email": "[email protected]"
},
{
"name": "2013-5-12 doublespout",
"email": "[email protected]"
}
],
"readme": "# Threads A GoGo2 for Node.js\n\nA native module for Node.js that provides an asynchronous, evented and/or continuation passing style API for moving blocking/longish CPU-bound tasks out of Node's event loop to JavaScript threads that run in parallel in the background and that use all the available CPU cores automatically; all from within a single Node process.\n\n## Installing the module\n\nWith [npm](http://npmjs.org/):\n\n npm install threads_a_gogo\n\nFrom source:\n\n git clone http://github.com/xk/node-threads-a-gogo.git\n cd node-threads-a-gogo\n node-waf configure install\n\nTo include the module in your project:\n\n var threads_a_gogo= require('threads_a_gogo');\n\n**You need a node with a v8 >= 3.2.4 to run this module. Any node >= 0.5.1 comes with a v8 >= 3.2.4.**\n\nThe module **runs fine, though, in any node >= 0.2.0** as long as you build it with a v8 >= 3.2.4. To do that you simply have to replace /node/deps/v8 with a newer version of v8 and recompile it (node). To get any version of node goto http://nodejs.org/dist/, and for v8 goto http://github.com/v8/v8, click on \"branch\", select the proper tag (>= 3.2.4), and download the .zip.\n\n## (not so) Quick Intro\n\nAfter the initialization phase of a Node program, whose purpose is to setup listeners and callbacks to be executed in response to events, the next phase, the proper execution of the program, is orchestrated by the event loop whose duty is to [juggle events, listeners and callbacks quickly and without any hiccups nor interruptions that would ruin its performance](http://youtube.com/v/D0uA_NOb0PE?autoplay=1)\n\nBoth the event loop and said listeners and callbacks run sequentially in a single thread of execution, Node's main thread. If any of them ever blocks, nothing else will happen for the duration of the block: no more events will be handled, no more callbacks nor listeners nor timeouts nor nextTick()ed functions will have the chance to run and do their job, because they won't be called by the blocked event loop, and the program will turn sluggish at best, or appear to be frozen and dead at worst.\n\n**A.-** Here's a program that makes Node's event loop spin freely and as fast as possible: it simply prints a dot to the console in each turn:\n\n cat examples/quickIntro_loop.js\n \n``` javascript\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**B.-** Here's another program that adds to the one above a fibonacci(35) call in each turn, a CPU-bound task that takes quite a while to complete and that blocks the event loop making it spin slowly and clumsily. The point is simply to show that you can't put a job like that in the event loop because Node will stop performing properly when its event loop can't spin fast and freely due to a callback/listener/nextTick()ed function that's blocking.\n\n cat examples/quickIntro_blocking.js\n\n``` javascript\nfunction fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n}\n\n(function fiboLoop () {\n process.stdout.write(fibo(35).toString());\n process.nextTick(fiboLoop);\n})();\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**C.-** The program below uses `threads_a_gogo` to run the fibonacci(35) calls in a background thread, so Node's event loop isn't blocked at all and can spin freely again at full speed:\n\n cat examples/quickIntro_oneThread.js\n \n``` javascript\nfunction fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n}\n\nfunction cb (err, data) {\n process.stdout.write(data);\n this.eval('fibo(35)', cb);\n}\n\nvar thread= require('threads_a_gogo').create();\n\nthread.eval(fibo).eval('fibo(35)', cb);\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**D.-** This example is almost identical to the one above, only that it creates 5 threads instead of one, each running a fibonacci(35) in parallel and in parallel too with Node's event loop that keeps spinning happily at full speed in its own thread:\n\n cat examples/quickIntro_fiveThreads.js\n \n``` javascript\nfunction fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n}\n\nfunction cb (err, data) {\n process.stdout.write(\" [\"+ this.id+ \"]\"+ data);\n this.eval('fibo(35)', cb);\n}\n\nvar threads_a_gogo= require('threads_a_gogo');\n\nthreads_a_gogo.create().eval(fibo).eval('fibo(35)', cb);\nthreads_a_gogo.create().eval(fibo).eval('fibo(35)', cb);\nthreads_a_gogo.create().eval(fibo).eval('fibo(35)', cb);\nthreads_a_gogo.create().eval(fibo).eval('fibo(35)', cb);\nthreads_a_gogo.create().eval(fibo).eval('fibo(35)', cb);\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**E.-** The next one asks `threads_a_gogo` to create a pool of 10 background threads, instead of creating them manually one by one:\n\n cat examples/multiThread.js\n\n``` javascript\nfunction fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n}\n\nvar numThreads= 10;\nvar threadPool= require('threads_a_gogo').createPool(numThreads).all.eval(fibo);\n\nthreadPool.all.eval('fibo(35)', function cb (err, data) {\n process.stdout.write(\" [\"+ this.id+ \"]\"+ data);\n this.eval('fibo(35)', cb);\n});\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**F.-** This is a demo of the `threads_a_gogo` eventEmitter API, using one thread:\n\n cat examples/quickIntro_oneThreadEvented.js\n\n``` javascript\nvar thread= require('threads_a_gogo').create();\nthread.load(__dirname + '/quickIntro_evented_childThreadCode.js');\n\n/*\n This is the code that's .load()ed into the child/background thread:\n \n function fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n }\n\n thread.on('giveMeTheFibo', function onGiveMeTheFibo (data) {\n this.emit('theFiboIs', fibo(+data)); //Emits 'theFiboIs' in the parent/main thread.\n });\n \n*/\n\n//Emit 'giveMeTheFibo' in the child/background thread.\nthread.emit('giveMeTheFibo', 35);\n\n//Listener for the 'theFiboIs' events emitted by the child/background thread.\nthread.on('theFiboIs', function cb (data) {\n process.stdout.write(data);\n this.emit('giveMeTheFibo', 35);\n});\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n**G.-** This is a demo of the `threads_a_gogo` eventEmitter API, using a pool of threads:\n\n cat examples/quickIntro_multiThreadEvented.js\n\n``` javascript\nvar numThreads= 10;\nvar threadPool= require('threads_a_gogo').createPool(numThreads);\nthreadPool.load(__dirname + '/quickIntro_evented_childThreadCode.js');\n\n/*\n This is the code that's .load()ed into the child/background threads:\n \n function fibo (n) {\n return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;\n }\n\n thread.on('giveMeTheFibo', function onGiveMeTheFibo (data) {\n this.emit('theFiboIs', fibo(+data)); //Emits 'theFiboIs' in the parent/main thread.\n });\n \n*/\n\n//Emit 'giveMeTheFibo' in all the child/background threads.\nthreadPool.all.emit('giveMeTheFibo', 35);\n\n//Listener for the 'theFiboIs' events emitted by the child/background threads.\nthreadPool.on('theFiboIs', function cb (data) {\n process.stdout.write(\" [\"+ this.id+ \"]\"+ data);\n this.emit('giveMeTheFibo', 35);\n});\n\n(function spinForever () {\n process.stdout.write(\".\");\n process.nextTick(spinForever);\n})();\n```\n\n## More examples\n\nThe `examples` directory contains a few more examples:\n\n* [ex01_basic](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex01_basic.md): Running a simple function in a thread.\n* [ex02_events](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex02_events.md): Sending events from a worker thread.\n* [ex03_ping_pong](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex03_ping_pong.md): Sending events both ways between the main thread and a worker thread.\n* [ex04_main](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex04_main.md): Loading the worker code from a file.\n* [ex05_pool](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex05_pool.md): Using the thread pool.\n* [ex06_jason](https://github.com/xk/node-threads-a-gogo/blob/master/examples/ex06_jason.md): Passing complex objects to threads.\n\n## API\n\n### Module API\n``` javascript\nvar threads_a_gogo= require('threads_a_gogo');\n```\n##### .create()\n`threads_a_gogo.create( /* no arguments */ )` -> thread object\n##### .createPool( numThreads )\n`threads_a_gogo.createPool( numberOfThreads )` -> threadPool object\n\n***\n### Thread API\n``` javascript\nvar thread= threads_a_gogo.create();\n```\n##### .id\n`thread.id` -> a sequential thread serial number\n##### .load( absolutePath [, cb] )\n`thread.load( absolutePath [, cb] )` -> reads the file at `absolutePath` and `thread.eval(fileContents, cb)`.\n##### .eval( program [, cb])\n`thread.eval( program [, cb])` -> converts `program.toString()` and eval()s it in the thread's global context, and (if provided) returns the completion value to `cb(err, completionValue)`.\n##### .on( eventType, listener )\n`thread.on( eventType, listener )` -> registers the listener `listener(data)` for any events of `eventType` that the thread `thread` may emit.\n##### .once( eventType, listener )\n`thread.once( eventType, listener )` -> like `thread.on()`, but the listener will only be called once.\n##### .removeAllListeners( [eventType] )\n`thread.removeAllListeners( [eventType] )` -> deletes all listeners for all eventTypes. If `eventType` is provided, deletes all listeners only for the event type `eventType`.\n##### .emit( eventType, eventData [, eventData ... ] )\n`thread.emit( eventType, eventData [, eventData ... ] )` -> emit an event of `eventType` with `eventData` inside the thread `thread`. All its arguments are .toString()ed.\n##### .destroy( /* no arguments */ )\n`thread.destroy( /* no arguments */ )` -> destroys the thread.\n\n***\n### Thread pool API\n``` javascript\nthreadPool= threads_a_gogo.createPool( numberOfThreads );\n```\n##### .load( absolutePath [, cb] )\n`threadPool.load( absolutePath [, cb] )` -> `thread.load( absolutePath [, cb] )` in all the pool's threads.\n##### .any.eval( program, cb )\n`threadPool.any.eval( program, cb )` -> like `thread.eval()`, but in any of the pool's threads.\n##### .any.emit( eventType, eventData [, eventData ... ] )\n`threadPool.any.emit( eventType, eventData [, eventData ... ] )` -> like `thread.emit()` but in any of the pool's threads.\n##### .all.eval( program, cb )\n`threadPool.all.eval( program, cb )` -> like `thread.eval()`, but in all the pool's threads.\n##### .all.emit( eventType, eventData [, eventData ... ] )\n`threadPool.all.emit( eventType, eventData [, eventData ... ] )` -> like `thread.emit()` but in all the pool's threads.\n##### .on( eventType, listener )\n`threadPool.on( eventType, listener )` -> like `thread.on()`, registers listeners for events from any of the threads in the pool.\n##### .totalThreads()\n`threadPool.totalThreads()` -> returns the number of threads in this pool: as supplied in `.createPool( number )`\n##### .idleThreads()\n`threadPool.idleThreads()` -> returns the number of threads in this pool that are currently idle (sleeping)\n##### .pendingJobs()\n`threadPool.pendingJobs()` -> returns the number of jobs pending.\n##### .destroy( [ rudely ] )\n`threadPool.destroy( [ rudely ] )` -> waits until `pendingJobs()` is zero and then destroys the pool. If `rudely` is truthy, then it doesn't wait for `pendingJobs === 0`.\n\n***\n### Global thread API\n\nInside every thread .create()d by threads_a_gogo, there's a global `thread` object with these properties:\n##### .id\n`thread.id` -> the serial number of this thread\n##### .on( eventType, listener )\n`thread.on( eventType, listener )` -> just like `thread.on()` above.\n##### .once( eventType, listener )\n`thread.once( eventType, listener )` -> just like `thread.once()` above.\n##### .emit( eventType, eventData [, eventData ... ] )\n`thread.emit( eventType, eventData [, eventData ... ] )` -> just like `thread.emit()` above.\n##### .removeAllListeners( [eventType] )\n`thread.removeAllListeners( [eventType] )` -> just like `thread.removeAllListeners()` above.\n##### .nextTick( function )\n`thread.nextTick( function )` -> like `process.nextTick()`, but twice as fast.\n\n***\n### Global puts\n\nInside every thread .create()d by threads_a_gogo, there's a global `puts`:\n##### puts(arg1 [, arg2 ...])\n`puts(arg1 [, arg2 ...])` -> .toString()s and prints its arguments to stdout.\n\n## Rationale\n\n[Node.js](http://nodejs.org) is the most [awesome, cute and super-sexy](http://javascriptology.com/threads_a_gogo/sexy.jpg) piece of free, open source software.\n\nIts event loop can spin as fast and smooth as a turbo, and roughly speaking, **the faster it spins, the more power it delivers**. That's why [@ryah](http://twitter.com/ryah) took great care to ensure that no -possibly slow- I/O operations could ever block it: a pool of background threads (thanks to [Marc Lehmann's libeio library](http://software.schmorp.de/pkg/libeio.html)) handle any blocking I/O calls in the background, in parallel.\n\nIn Node it's verboten to write a server like this:\n\n``` javascript\nhttp.createServer(function (req,res) {\n res.end( fs.readFileSync(path) );\n}).listen(port);\n```\nBecause synchronous I/O calls **block the turbo**, and without proper boost, Node.js begins to stutter and behaves clumsily. To avoid it there's the asynchronous version of `.readFile()`, in continuation passing style, that takes a callback:\n\n``` javascript\nfs.readfile(path, function cb (err, data) { /* ... */ });\n```\n\nIt's cool, we love it (*), and there's hundreds of ad hoc built-in functions like this in Node to help us deal with almost any variety of possibly slow, blocking I/O.\n\n### But what's with longish, CPU-bound tasks?\n\nHow do you avoid blocking the event loop, when the task at hand isn't I/O bound, and lasts more than a few fractions of a millisecond?\n\n``` javascript\nhttp.createServer(function cb (req,res) {\n res.end( fibonacci(40) );\n}).listen(port);\n```\n\nYou simply can't, because there's no way... well, there wasn't before `threads_a_gogo`.\n\n### What is Threads A GoGo for Node.js\n\n`threads_a_gogo` provides the asynchronous API for CPU-bound tasks that's missing in Node.js. Both in continuation passing style (callbacks), and in event emitter style (event listeners).\n\nThe same API Node uses to delegate a longish I/O task to a background (libeio) thread:\n\n`asyncIOTask(what, cb);`\n\n`threads_a_gogo` uses to delegate a longish CPU task to a background (JavaScript) thread:\n\n`thread.eval(program, cb);`\n\nSo with `threads_a_gogo` you can write:\n\n``` javascript\nhttp.createServer(function (req,res) {\n thread.eval('fibonacci(40)', function cb (err, data) {\n res.end(data);\n });\n}).listen(port);\n```\n\nAnd it won't block the event loop because the `fibonacci(40)` will run in parallel in a separate background thread.\n\n\n### Why Threads\n\nThreads (kernel threads) are very interesting creatures. They provide:\n\n1.- Parallelism: All the threads run in parallel. On a single core processor, the CPU is switched rapidly back and forth among the threads providing the illusion that the threads are running in parallel, albeit on a slower CPU than the real one. With 10 compute-bound threads in a process, the threads would appear to be running in parallel, each one on a CPU with 1/10th the speed of the real CPU. On a multi-core processor, threads are truly running in parallel, and get time-sliced when the number of threads exceed the number of cores. So with 12 compute bound threads on a quad-core processor each thread will appear to run at 1/3rd of the nominal core speed.\n\n2.- Fairness: No thread is more important than another, cores and CPU slices are fairly distributed among threads by the OS scheduler.\n\n3.- Threads fully exploit all the available CPU resources in your system. On a loaded system running many tasks in many threads, the more cores there are, the faster the threads will complete. Automatically.\n\n4.- The threads of a process share exactly the same address space, that of the process they belong to. Every thread can access every memory address within the process' address space. This is a very appropriate setup when the threads are actually part of the same job and are actively and closely cooperating with each other. Passing a reference to a chunk of data via a pointer is many orders of magnitude faster than transferring a copy of the data via IPC.\n\n\n### Why not multiple processes.\n\nThe \"can't block the event loop\" problem is inherent to Node's evented model. No matter how many Node processes you have running as a [Node-cluster](http://blog.nodejs.org/2011/10/04/an-easy-way-to-build-scalable-network-programs/), it won't solve its issues with CPU-bound tasks.\n\nLaunch a cluster of N Nodes running the example B (`quickIntro_blocking.js`) above, and all you'll get is N -instead of one- Nodes with their event loops blocked and showing a sluggish performance.\n",
"readmeFilename": "README.md",
"_id": "[email protected]",
"dist": {
"shasum": "f23dff612c37190cd91b2bf0b81279a694db827a"
},
"_from": "tagg2"
}