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.





50 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. Nodejs is an open source and cross platform environment for developing the server side web application. The main advantage of using nodejs is because of its fastness, single threaded and highly scalable.
    Node JS training in chennai | Node JS training institute in chennai

    ReplyDelete
  10. Great stuff man, thanks for sharing.

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

    Melbourne Web Developer

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

    ReplyDelete
  13. 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
  14. 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
  15. 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
  16. 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
  17. This comment has been removed by the author.

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

    ReplyDelete
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. Wonderfull content, Nice blog. We provide a best accounting service QuickBooks Customer Service you can reach us at +1 855-885-5111.

    ReplyDelete
  29. 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
  30. 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
  31. 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
  32. 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
  33. Thank you for this important information keep doing this work International school in faridabad

    ReplyDelete
  34. 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

  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. https://www.visualpath.in/devops-online-training.html

    ReplyDelete