Friday, March 21, 2014

Creating processes in node.js

In some of my previous posts I started comparing some of the features of node.js with java and since then my interest has started to grow for node.js.

One thing I just learned is that node runs in a single thread mode, it uses an event-driven paradigm to handle concurrency.

Well that's fine but what about parallel processes how can you take advantage of this when you are in a single thread program, well node.js has some mechanisms to create processes and take advantage of parallel computing, and I will tell you how.

First we will need a process to run as a worker, there could be many instances running of this process at the same time.

For the purpose of this blog this process will have just a log to console, also any kind of process can be created not just node process, but also for the blog I will use just this.

For creating a child process the child_process module is required, this module has different ways to create process.

The exec method of the child_process runs a command in a shell and buffers the output. It has the following signature:

               child_process.exec(command, [options], callback)


In the example the command "node worker.js i"  is executed where i is a parameter passed to the process.
Also a callback function is passed and it is called when the process terminates and allows to read and process the output from it.

After running the example the following should be printed in console for each process created.

stdout: Process 0 at work
stderr:

Child process exited with exit code 0

The spawn method of the child_process module launches a new process with a given command, it has the following signature:

               child_process.spawn(command, [args], [options])


After running the example the following should be printed in console for each process created.

stdout: Process 0 at work

child process exited with code 0

Exec() and spawn() both create processes, but they differ in what they return, spawn returns streams (stdout & stderr), while exec returns a buffer with a max size. Spawn() should be used when the process returns large amount of data.

Another difference is that spawn() starts receiving the response as soon as the process starts executing, and exec() otherwise waits for the process to end and tries to return all the buffered data at once

The fork method is a special case of the spawn() functionality, it only creates Node processes. Its signature is the following:


              child_process.fork(modulePath, [args], [options])

After running the example the following should be printed in console for each process created.

Process 0 at work
child process exited with code 0

Node.js also has the cluster module and also allows to create processes, the definition of the cluster module says: "A single instance of Node runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load".

From the example you can notice that the cluster module is imported, once imported you can call fork() method to create a new child process, after calling fork() the same process will run but as a child, to distinguish if the process is running as a Master or child the cluster module has the isMaster() and isWorker() methods.

After running the example above, the following should be printed.

Master Forking!!!!
Worker 4984 is online.
....
Child process running!!!
....


You can find more information about these modules in the following links:
http://nodejs.org/api/child_process.html
http://nodejs.org/api/cluster.html

Node.js says that runs in a single thread mode, but as with this brief example, I show that node.js has different mechanisms to create processes and to take advantage of multiple processors.





52 comments:

  1. Easy to understand examples. Much appreciated.

    ReplyDelete
  2. Hi, Great post about node processes!

    I have created a simple nodejs module that really simplify processes handling:
    https://github.com/hugorodrigues/child

    Take a look, hope it helps :)

    ReplyDelete
  3. With a thorough understanding of Node.js, development you will be capable of developing real-time, data driven, fast, scalable web applications, but you will need to have the requisite knowledge so that you can quickly adapt to any of the emerging JavaScript frameworks.

    ReplyDelete
  4. Thanks for this simple explanation.

    ReplyDelete
  5. great post! Thx!

    is there a limit in the number of process Node can create?

    ReplyDelete
  6. great post! Thx!

    is there a limit in the number of process Node can create?

    ReplyDelete
  7. Goddamn it man! why use "process" as the variable name for child_process? Just use var cp = require('child_process'); process should be reserved for Node core itself, if not only to reduce confusion. Why? Whyyyyy??? Just because you saw someone else use "process", don't do that.

    ReplyDelete
    Replies
    1. But.. cp can be confused with Cerebral palsy.. And child_process can be confused with pregnancy.. And and... :-(

      Delete
    2. No, 'process' is a global variable that NodeJS exposes. Using it in your own script is akin to calling something 'class'.

      Delete
  8. Goddamn it man! why use "process" as the variable name for child_process? Just use var cp = require('child_process'); process should be reserved for Node core itself, if not only to reduce confusion. Why? Whyyyyy??? Just because you saw someone else use "process", don't do that.

    ReplyDelete
  9. Great stuff man, thanks for sharing.

    ReplyDelete
  10. Thank you for sharing this information. This article is very interesting and useful. Keep up the good work!

    Melbourne Web Developer

    ReplyDelete
  11. Awesome information. Easy to understand. thanks for this information. Nodejs Training in Bangalore

    ReplyDelete
  12. hello Thanks for sharing this information.
    We are one of the most trusted Android Mobile App Development
    and IOS application development to meet your business complex requirement through offshore world.
    Do you have project and want to discuss with us ?
    We can assist you in Web Application Development, Mobile App development, Games Development & Emerging Technology.

    ReplyDelete
  13. Thanks for sharing this Valuable information. I like this blog.
    courses in digital marketing in bangalore
    A comprehensive Digital Marketing learning experience involves Training, Certification, Internships, and Placement in a reputable Digital Marketing firm.

    ReplyDelete
  14. Very Nice Blog, You are spreading very good information among us… Yes Web Hosting plays very important role in business world. And it is important to have the best hosting services.
    Germany VPS Hosting

    ReplyDelete
  15. Nicely you explained everything in your article. Thanks for your information, this is nice and helpful… Definitely going to share this article to my friends.
    Germany VPS Hosting

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Hi, Great post about node processes! Thank you for sharing this information. This article is very interesting and useful. Hey , we provide you the best USA VPS Hosting service at a very cheap price.

    ReplyDelete
  19. This is blog very interesting and very helpful information for me. Nodejs is an open source and cross platform environment for developing the server side web application. It is very nice information for me I liked it your blog. Keep it always sharing with us. If you want to know USA VPS Server Hosting at affordable price. then you will more infomation related USA VPS Hosting by onlive Server.

    ReplyDelete
  20. Sharetipsinfo is known for providing highly accurate Indian stock market tips which covers Cash tips, F&O intraday tips, Nifty intraday tips, Mcx intraday commodity tips and Share market tips with high accuracy.

    ReplyDelete
  21. To send or receive assistance, you can dial the toll-free number of Quickbooks support service – Quickbooks Customer Service +1 888-698-6548 which is available 24 hours a day.

    ReplyDelete
  22. The QuickBooks service number is .Quickbooks Customer Service Number +1 855-885-5111 and is always available 24 hours a day to serve you with all your QuickBooks questions.

    ReplyDelete
  23. The only thing that can come with using this software is the income you get from work. The best part about Quickbooks Customer Service +1 877-693-1115 is that it has a team of professional employees who are available around the clock for help.

    ReplyDelete
  24. Quickbooks Customer Service Number +1 855-885-5111 is a treasure trove of features. It has a free subscription for a year, and the software offers plenty of features for any size business.

    ReplyDelete
  25. But if you still want quick assistance from QuickBooks, then dial our toll-free number at Quickbooks Support Phone Number +1 855-444-2233 will help you out.

    ReplyDelete
  26. Whether you are just starting your company, working with a new vendor or just want to learn more about how QuickBooks works, dial Quickbooks Support Phone Number +1 855-769-6757 for help along the way.

    ReplyDelete
  27. Wonderfull content, Nice blog. We provide a best accounting service QuickBooks Customer Service you can reach us at +1 855-885-5111.

    ReplyDelete
  28. Quickbooks also offers email support which can be accessed through both their website and through dialling Quickbooks Customer Service +1 855-769-6757. The number for live help is actually only available if you are using the United States version of QuickBooks, but it’s free to call anyway.

    ReplyDelete
  29. Quickbooks customers can dial a toll-free Quickbooks Customer Service +1 855-769-6757 to get answers to their questions. The phone line is free and the live operators are trained to handle many Quickbooks related issues.

    ReplyDelete
  30. QuickBooks is available in both software and cloud versions. They are the number one accounting software company out there, so they must be able to provide assistance when you have questions or need help with fixing something on the system. Contact them at QuickBooks Support Phone Number +1 855-769-6757 Suggest if you have any questions related to their product or need other important financing needs to be met.

    ReplyDelete
  31. Thank you for sharing this post and this post is a very usefully my name is shop.jvatec May live in jaipur city of India Country I am a web devleper shop.jvatec For any information related to domain and hosting, visit our site.

    ReplyDelete
  32. Thank you for this important information keep doing this work International school in faridabad

    ReplyDelete
  33. What a wonderful blog it has everything I was looking for also check out this one Top Hospital In Faridabad keep doing this work and thank you for this post

    ReplyDelete

  34. Your sponsored product listings are the key to sales. If they’re not pulling their weight, they’re useless. Sustainability begins with optimizing your product listings and identifying relevant and profitable traffic to send to your sponsored product campaigns. Ecommphics can take your advertising campaigns to the next level. They have successfully segmented advertising campaigns to cover all potential customers on Amazon, and have implemented new ad types before they were even announced by them.
    Amazon PPC management services and advanced macros optimize your advertising efforts, and not only do they ensure your PPC campaigns are reaching the right people, but they optimize your product pages to improve ranking, relevancy, and brand scalability. Trust them to drive more traffic to your listings.

    ReplyDelete
  35. Best digital marketing company in Allahabad Ekaasha Technologies
    Your sponsored product listings are the key to sales. If they’re not pulling their weight, they’re useless. Sustainability begins with optimizing your product listings and identifying relevant and profitable traffic to send to your sponsored product campaigns. Ecommphics can take your advertising campaigns to the next level. They have successfully segmented advertising campaigns to cover all potential customers on Amazon, and have implemented new ad types before they were even announced by them.

    ReplyDelete
  36. Thanks for sharing more interesting blog. I got some good information and And also got a good opportunity to share my opinion. I really want to appreciate your efforts. and also looking forward to another great blog from you.

    Here is sharing some Oracle Apps Technical Training concepts may be its helpful to you.
    oracle apps technical training

    ReplyDelete
  37. Keep doing this work also have a look on this Best Embryo Freezing Centre In Faridabad this blog contains some interesting topics and thank you for this informative post.

    ReplyDelete
  38. What a wonderful blog it contains some interesting topics and help me a lot in many ways keep doing this work also have a look on this Best Egg Donation Centre In Faridabad

    ReplyDelete
  39. Keep doing this work also have a look on this Best IVF Centre in Delhi this blog contains some interesting topics and thank you for this informative post.

    ReplyDelete
  40. What a wonderful blog it really helped me to understand new things keep doing this work also have a look at this Best gynaecologist in Dwarka and thank you for this informative post.

    ReplyDelete
  41. Plusinno Fishing Lures, under the Wished For, offer a wide assortment of superior grade, strong lures intended to draw in a wide range of fish. These lures highlight reasonable plans and dynamic tones, upgrading their viability in different fishing conditions. Ideal for both beginner and experienced fishermen, Plusinno Fishing Lures guarantee a fruitful and pleasant fishing experience. Plusinno Fishing Lures

    ReplyDelete
  42. The Vollgut Bath Lift Chair consolidates a smooth plan with ideal comfort, guaranteeing a protected and loosening-up bathing experience. Ideal for those looking for both style and usefulness in their bathroom versatility arrangements. Vollgut Bath Lift Chair

    ReplyDelete
  43. "I'm Vernon, a unique individual with a story and perspective formed by my experiences. Whether exploring ideas, pursuing passions, or connecting with others, I bring authenticity and profundity to everything I do."

    ReplyDelete
  44. Nursing Assignment Help offer comprehensive support to guarantee the successful achievement of an A+ grade on your university assignments.

    ReplyDelete