1
const run = ()=> {
console.log("Begin")
for(var i=0;i<10000000000;i++){
}
console.log("End")
}
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
 run()
 res.send('Hello World!')
})
app.listen(port, () => {
 console.log(`Example app listening at http://localhost:${port}`)
})

This is a minimal node app, on every request, it calls the loop (5-10 seconds), and returns the response. I tried to hit it twice, at the same time almost, the request that reached second had to wait for the first one to completely finish.

from flask import Flask
def run():
    print("Begin")
    for i in range(1000000000):
        pass
    print("End")
app = Flask(__name__)
@app.route('/')
def hello_world():
    run()
    return 'Hello, World!'
if __name__ == '__main__':
    app.run()

This is a minimal flask app that does the same thing, the method takes about 10 seconds, but when I hit the API twice, “Begin” was printed twice ( 2 Threads ).

I'm sure that I'm missing something in my Node app that is preventing me from giving each request its own thread since CPU bound operations are involved. I know that NodeJS runs on a single-threaded event loop, but I'm sure that when it's serving multiple requests, there should be some threading involved. What am I missing?

2 Answers 2

2

Although Node.js is single thread, it also provides multiple forms of handle CPU bound tasks.

First, you can create a cluster of processes for your server, it may sound complicated but most of the work is already done for you, as communication between processes or even requests distribution to the child processes to handle. You can also divide a specific task along with multiple worker threads. It really depends on what problem you're trying to solve, both methods have their own strengths and weaknesses.

Sign up to request clarification or add additional context in comments.

2 Comments

That's interesting, so you'd need some manual intervention to handle concurrent requests whereas other frameworks are multithreaded out of the box?
In fact, everything that I showed was Node.js features. On Python, you're using Flask, a micro framework on top of Python own language features.
1

It doesn't handle multiple synchronous cpu bound tasks at the same time. A single thread means exactly what it sounds like. If you have one piece of code executing no other code will execute until it has finished or has yielded to another by performing some sort of async call. For more details there are many many articles written on the topic. https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

4 Comments

But how will this be efficient when you have 100 requests hitting your server concurrently?
If you are performing CPU bound tasks it will be absolutely terrible. However, most of what web servers do is IO. They communicate with a database, or they make request to other services. They can also farm out work to other processes on the same box. In my experience it is rare to have a web service providing a truly cpu bound service outside of rare cases such as image processing. Those can easily be farmed out to a more appropriate language/service. Also computers are ridiculously insanely fast at cpu tasks. Feeding them the data can be the real bottleneck.
Native calls and the node vm are allowed to do whatever they want. Under the covers this means that multiple threads may be involved. However, the actual execution of Javascript is done within a single thread. It sounds like you did not read the document I linked. You should read a bit more and come back with a more specific question in the future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.