What are some advantages of using Node.js over a Flask API?

 

https://www.quora.com/What-are-some-advantages-of-using-Node-js-over-a-Flask-API

Flask is a Python web microframework. It needs to be run behind a WSGI compliant web server. The nature of WSGI is synchronous. So you'll usually spawn threads to serve requests. Python 'threads' is also blocking, due to the GIL, they cannot run in parallel. Thus reducing the number of requests you're able to be served. Spawning threads is also expensive.

NodeJS is not a framework, it's a JavaScript binding of libuv. I have a write up about it over here:

Under The Bonnet 1 - NodeJS - YKode

NodeJS is asynchronous by design. Which means that it's not blocking.  When you you do I/O it will return immediately and get notification later on. It's single threaded. It just means that you won't block the event loop when you wait for response, you can do another task including accepting incoming requests.  So even on single thread, you can accept and serve multiple requests. On synchronous environment, this I/O operation will block the thread.

 

 

The event driven and concurrent connections model of Node.js makes it one of the best platforms for real time applications like online games, collaboration tools, chat rooms, or anything where what one user (or robot? or sensor?) does with the application needs to be seen by other users immediately, without a page refresh.

As this answer on Stack Overflow says,

My feeling is that Node.js is especially suited for applications where you'd like to maintain a persistent connection from the browser back to the server. Using a technique known as "long-polling", you can write an application that sends updates to the user in real time. Doing long polling on many of the web's giants, like Ruby on Rails or Django, would create immense load on the server, because each active client eats up one server process. This situation amounts to a tarpit attack. When you use something like Node.js, the server has no need of maintaining separate threads for each open connection.


Although, there exist libraries in Python as well for doing this sort of work, Node.js does it really well (powered by cutting edge V8 JavaScript engine). However, if you are performing some heavy server side computation on receiving requests, then you are better off serving it with regular FLASK based server. (subjective comment)

 

 

 

posted @ 2018-01-02 21:55  papering  阅读(196)  评论(0编辑  收藏  举报