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?