Home > Article > Web Front-end > child_process implements multi-process in Node.js_node.js
The above example provides a Fibonacci sequence calculation service. Since this calculation is quite time-consuming and is single-threaded, when there are multiple requests at the same time, only one can be processed. This can be solved by child_process.fork() This question
Here is an example from the official website, through which you can better understand the function of fork()
The result of executing the above code snippet:
The content of sub.js is as follows:
In the child process, the process object has a send() method, and it will publish a message object every time it receives a message
What’s a little confusing is: the message sent by child.send() is received by the process.on() method, and the message sent by the process.send() method is received by the child.on() method
Referring to this example, we can improve the first service that provides Fibonacci data so that each request has a separate new process to handle
fibonacci-calc.js
After starting the service, visit http://localhost:8080/9 to calculate the value of the Fibonacci sequence of 9
The above is the entire content of this article, I hope you all like it.