Best method for Node.JS forking? - node.js

I'm writing a trajectory predictor in Node.JS. You may think it's a funny language to write one in, but it's actually working great. Now, I want to be able to start the predictor from a web interface in Node.JS.
The actual predictor process takes about 5 minutes to run. So to spawn it from the Node web process, I don't want the web process waiting for the child process to finish. What is the best method of forking, in Node.JS, to allow for spawning and releasing a process like this?

Use the built-in child_process node module: http://nodejs.org/api/child_process.html

Related

How to Simulate CPU workload in nodejs app?

I'm working on building a simple app that I can use to test out microservice scaling. I have a simple node.js webserver running with a few routes, but now I need to find something to run that will use some CPU when hitting the routes.
Right now I have a listener up that starts a child process and calculates prime numbers. In my testing it may take a decent while to generate primes up to 100,000,000.... but even with 10+ child process running I am not seeing any real CPU load. So guessing this single threaded type of math equation isn't a great use case.
Can anyone point me to some simple to run things in JS that will burn some CPU?
This page was exactly what I was looking for and trying to do. After changing over to worker threads instead of child processes, I was able to simulate 100 concurrent requests and finally started hammering my CPU.

Running multiple instances of the same script NodeJS - Child Processes

I have a script which queries an API, the input to the script is a unique user ID which will determine what endpoint is queried on the API.
I want to spawn multiple instances of this script for the given X number of users and wondering what is the best solution for this?
I have looked into NodeJS child processes, is this the main way of solving this problem within node, spawning multiple child processes from a main process? Are all these processes then running on one thread I assume as NodeJS is single threaded?
The script that I want to spawn multiple process's of will be running constantly once it is started, querying an API for data every second. I guess it will have to factor in how much compute power I have available but generally how scalable is the child processes way of doing things?
Also in the background would a NodeJS child process be doing the same thing as if I was to run a bash command index.js & and spawn different processes? (Aside from being able to control the child processes then from the main process in NodeJS)
What you require can be done using a process manager like pm2.
You can read up more about pm2 here.
To launch multiple instances as a cluster using pm2 with the following syntax.
pm2 start server.js -i 4
The above command would launch 4 instances of server.js. Also note that you do a lot of configuration using a config file (read docs for it) and it even supports multi-threading.
You can try this cli tool to achive the thing you need. The library will ask for a script to spawn and will cycle it's execution with multiple child process.

Python multiprocessing pipe with process pool

I had a few manually started processes (with p.start()) for dealing with some background tasks, and I communicated with them via multiprocessing.Pipe(). So far, so good.
Now, I have to scale my application in a situation that, following the same structure, too many processes would be started.
So, I'm trying to port my code from having some manually started multiprocessing.Process's to a pool of processes. The problem is that multiprocessing.Pipe() does not seem to work with them. It seems that I should have to use a queue.
Specifically, I was using the code suggested in this stackovervlow answer to run some generators in background, but the problem is that now I have many generators.
Many thanks.

Is really NodeJS Singlethread or Multithread?

many question on stackoverflow and others website , some ones says NodeJS is Singlethread and someone says NodeJS is Multithread, and they have there own logic to be Singlethread or Multithread. But If a interviewer ask same question. what should I say. I am getting confusion here.
The main event loop in NodeJs is single-threaded but most of the I/O works run on separate threads.
You can make it multi-threaded by creating child processes.
There is a npm module napajs to create a multi-threaded javascript runtime.
However,the 10.5.0 release has announced multithreading in Node.js. The feature is still experimental and likely to undergo extensive changes, but it does show the direction in which NodeJs is heading.
So stay tuned!!
NodeJS runs JavaScript in a single threaded environment. You get to use a single thread (barring the worker_threads module, and spawning processes).
Under the scenes, NodeJS uses libuv, which uses OS threads for the async I/O you get in the form of the event loop.

What is different about the way NodeJS handles requests as opposed to a setup like Rails / Passenger?

My understanding is that Node is an 'Event' driven as opposed to sequentially driven server application. This I've come to understand means, that for event driven software, the user is in command, he can create an event at any time and the server is in a state such that it can respond, whereas with sequential software (like a DOS prompt), the application tells the user when its 'ok' to response, and may at any given time be not available (due to some other process).
Further, my understanding is that applications like Node and EventMachine use a reactor of sorts.. they wait for an 'event' to occur, and using a callback they delegate the task to some other worker. Ok.. so then, what about Rails & Passenger?
Rails might use a server like NGINX with Passenger to spawn new processes when 'events' are received by the system. Is this not conceptually the same idea? If it is, is it just the processing overhead that is really separating the two where Passenger would need to potentially spawn a new rails instance while, node is already waiting to handle the request?
Node.js is event driven non blocking programming language. The key is the non blocking part. Node doesn't spawn for other processes. It runs in one thread (this is for starters... you can actually spawn it now through some modules - i think - but that's another talk)
Anyway this is different from other typical programming languages where you receive a request and the thread is locked until it has an answer. If you assign it to another thread, that thread is still locked...
In node you never lock. You receive request and the thread continues to receive requests. When a request is processed, the callback is called.
Hope I made myself understand and I used the right terms ;)
Anyway, if you want this video is nicee: http://www.youtube.com/watch?v=jo_B4LTHi3I
The non-blocking/evented I/O as jribeiro described is one part of the answer. Ruby applications tend to be written using blocking I/O, and using processes and threads for concurrency.
However, non-blocking and evented I/O are not inherent to Node. You can achieve the same thing in Ruby by using EventMachine, and an in-process evented server like Thin. If you program directly against EventMachine and Thin, then it is architecturally almost the same as Node. That being said, the Ruby ecosystem does not have as many event-friendly libraries and documentation as Node does, so it takes a bit of skill to achieve the same thing in Ruby.
Conversely, the way Phusion Passenger manages processes - i.e. by spawning multiple processes and load balancing requests between them, and supervising processes - is not unique to Ruby. In fact, Phusion Passenger introduced Node.js support recently. The Node.js support was open sourced today.

Resources