If what you're doing is event based (listening for a user to press a button, flip a switch) or time based (want to poll a battery level every 100ms) Don't write blocking code in a thread, use the many event primitives that node provides. You can write your API as a subclass to EventEmitter.
The reason I mention theads and processes is for when you're doing active busy CPU busy work that takes longer than a ms. (like a raytracer or gzipping 10mb of data). Small CPU bound actions are perfectly find within the event loop. On Fri, Apr 6, 2012 at 2:49 PM, Bruno Jouhier <[email protected]> wrote: > And you can now write CPU intensive functions directly in Javascript, and > run them in a separate thread: https://github.com/xk/node-threads-a-gogo. > > > On Friday, April 6, 2012 9:22:29 PM UTC+2, Tim Caswell wrote: >> >> Keep in mind that the nextTick hack technique still blocks your CPU. >> It's just broken up into many small parts. If you must do something that's >> truly CPU intensive, put it on a thread or another process. Most modern >> machines (even phones) have multiple CPU cores. >> >> Interprocess communication is a fairly well solved problem in node. >> There is the built-in cluster module, there is the child_process.fork >> function (somewhat like a webworker, but using a full child process). Then >> in userland there are things like dnode, hook.io, architect-protocol >> that provide various levels of abstraction. >> >> If you're writing a C addon then libuv provides a couple interfaces to do >> work in a thread. If what you doing is numerically intensive, doing it in >> javascript is probably a bad idea to begin with. Either write a node addon >> and do the hard work in C or talk to another process via sockets/tcp/stdio. >> >> On Fri, Apr 6, 2012 at 2:02 PM, mscdex <[email protected]> wrote: >> >>> On Apr 6, 2:38 pm, Mark Hahn <[email protected]> wrote: >>> > In any case, here is a loop that runs until cond is true ... >>> > >>> > function processOneTick() { >>> > // do work for one tick >>> > if (!cond) setTimeout processOneTick, 0} >>> > >>> > processOneTick() >>> >>> May as well use process.nextTick() instead for that approach. >>> >>> -- >>> Job Board: http://jobs.nodejs.org/ >>> Posting guidelines: https://github.com/joyent/**node/wiki/Mailing-List-* >>> *Posting-Guidelines<https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines> >>> You received this message because you are subscribed to the Google >>> Groups "nodejs" group. >>> To post to this group, send email to [email protected] >>> To unsubscribe from this group, send email to >>> nodejs+unsubscribe@**googlegroups.com<nodejs%[email protected]> >>> For more options, visit this group at >>> http://groups.google.com/**group/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en> >>> >> >> -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
