Python As NodeJS Dev.

Python As NodeJS Dev.

So I took a turn to write a live Python app. Let me take you through a journey of a Javascript developer writing a Python project

Javascript and Python are two aggressively competing language in the market. I have been writing Javascript code since I started my career. Written tons of lines in both back end and front end. And later when I started writing Python, it gave a different perspective.

This is not a guide or step by step preparation for a tutorial. Rather more of a kind of experience
amalgamated with a gesture of opinions.

Installation

Python

Python comes preloaded with Linux and Mac, but for windows, we may have to install it. Mostly Python2 will be the default version. Days of Python2 is gonna fade eventually, all the recent projects are using Python3. In case of starting a new project, I would hardly recommend Python2 unless there is a serious dependency with legacy code or some library. Python3 might not come out of the box, but could be easily installed.

NodeJS

Well…. am not sure of any operating system that comes NodeJS pre-installed, I mean till date. But installation is straight forward in all platforms. There are executable for Windows and brew/apt/yup/… packages for Mac/Linux.

Starting a REST API project

Python

So for accelerated development, we could choose flask or Django(read as Jango) to write APIs. Flask is a customizable library with a less opinionated view. We could redraw the architecture of the project as we need. Now, this comes with it’s on responsibility too. Whereas Django is a sort of framework. Architectural changes that we could make to the project is limited. But the architecture of Django is done by amazing engineers and is rock solid. Needless to say, Instagram is one of the largest Django deployment.

NodeJS

Express is the first word everyone yells when it comes to REST in NodeJS. Express is light weight and a good framework. But there are options like hapi, restify (Netflix uses this) and loopback. Even without any framework, we could create REST endpoints, but that may look like reinventing the wheel.

Dependency management

Python uses pip and NodeJS uses npm, although there is a stronger sibling yarn. One thing to notice about Python dependency management is, all the external dependencies installed using pip goes to the system binaries of Python. I mean, all the dependencies will be saved where Python is installed. Considering the age of Python, this seems to be an obvious choice that they made during those days. The problem arises when we need to run multiple application written in Python which uses different versions of the same library. And with virtualenv, this problem is no more NPHard.

NodeJS dependency management is cleaner. All the dependencies mentioned in the package.json file goes to the folder node_modules which resides in the project’s root directory. So multiple projects using multiple versions of a specific library is not an issue. Python needs the Python binaries along with dependencies, whereas NodeJS doesn’t need it. And to export a module from a file an explicit export statement is needed in NodeJS, whereas it’s not needed in Python. And the import statement is always relative to the file in NodeJS, whereas its absolute in Python.

Coding pattern

Python uses a synchronous coding style, which means the second statement will wait till the first statement completes execution. On the other hand, NodeJS by default is asynchronous. Which means, if we hit any process which uses i/o, that goes to background and next statement starts executing. And this is the reason why NodeJS is good for an i/o or data-intensive back end.

Async code usually comes under the cost of readability. But the recent ES6 async await seems to be an implementation on an edge. On the other hand, async nature is the feature that makes Javascript stand apart from others. Out of the box, async await is great, for i/o or data-intensive applications.

Execution pattern

Concurrency is by default to NodeJS/Javascript[sort of, not as go routine concurrency]. This is achieved by the use of event loop and its single threaded concept. Event-loop sometimes comes under some cost, which we will discuss in further part. So basically any request emerging from the remote server will be processed by the event loop.
Python, to be precise Flask, come out of the box with a single process. So if we simply put the dev environment to live, you could see the request getting bottle-necked. And no one does that, we usually use workers to implement this. We use Gunicorn workers, to do parallel processing of the requests. Gunicorn could be configured for both synchronous and asynchronous execution.

JSON and marshmallow

XML is a long gone story for developers now(sorry for legacy developers). JSON is now used as the common response data-type. And for Javascript JSON is a first class citizen. And JSONdata received or to be sent from the server, it could be simply loaded to a variable and do operations on that[Well this is bot good and bad].
On the other hand, there is no native support for json objects in Python. Which means you cant directly operate on top of JSON objects in Python. JSON objects are to converted to Python objects and only on the operations of that object could be performed. Marshmallow library could help this action in python.

Functional support

Both languages support functional paradigm out of the box. Or functions are considered as first-class citizen by both Python and Javascript. So in these languages

  • Functions can be nested.
  • Functions can be passed as a parameter to another function.
  • The function could be returned from another function.
  • Functions could be stored into the data type.

An important thing here is how closures work. May be I will write it as a different blog, how closures work on Python and Javascript. Closures work differently on each platform.

Decorators

This is one awesome thing I felt in Python. If anyone who is not aware of the decorator, let us take an example to explore it

@foo
def method(params):
pass

foo is the decorator called here.

Here the function method will be passed as an argument to the decorator function foo. The function method will be called inside the function foo

For step by step guide to function decorators in Python, refer this.

Sadly Javascript lacks decorator, but we have a piece of happy news. Decorators are on the way on ECMAScript. More about that on here.

Performance

Well in the context of a data-intensive restful back end service, NodeJS might perform sound. Especially the new v8 improvements coming each day makes NodeJS stronger and quicker. Needless to say, it might hang a bit when it comes to computational intensive stuff. This is why because, NodeJS runs on a single thread called event loop. Whenever an i/o operation occurs, event loop with the help of libuv library sends the tasks to the newly spawned thread. So new thread spawning happens only for an i/o task. Am not talking about the experimental implementation of workers and clustering here. Those works in a different way.

Python is not as fast as NodeJS but could do a complex thing in a simple way. And for computationally intensive tasks Python could go better than NodeJS. On top of that, using cython, a C dialect of Python gives much performance improvement over Python for computationally intensive tasks.

Maturity

Python is very matured. Language evolved very well, and Python 3 we could say a language with a great legacy.
NodeJS is young. On the other hand, is Javascript beneath NodeJS is old. But Javascript is a language created within 10 days. In the initial days, it cants even be considered as a language. But with ECMAScript updates, Javascript turned to a decent language, and the upcoming changes in the pipeline hopefully make it better.

Availability of libraries

The abundance of a library is stellar for both Python and Javascript. With pip and npm we could get a library for almost any purpose.

Debugging

NodeJS and Python come with debuggers. Which means rather than printing each line to test output, you could put a breakpoint and stop execution flow.
One easy way to work with debuggers is using vs code or JetBrains ide. Since NodeJS runs on top of v8, it could be directly debugged using Chrome.

Going live

Both Python and NodeJS is recommended to go with nginx. But Python might need gunicorn and celery task queue, for better task management.

On the other hand, if we are going with PaaS services, configurations might be available from a predefined set. If we are going with IaaS, it’s better to do a deployment with docker and kubernetes.

Both Python and NodeJS is a great tool for the back end. Both are growing exponentially. Choosing the right platform depends upon several aspects and tradeoffs. Few of them worth mentioning are

Go with Python if you are building

  • Monolithic.
  • Computational intensive.
  • You have good no of Python developers in the organization.

Go with NodeJS if you are building

  • Microservice.
  • Data Intensive.
  • You have good no of Javascript developers in the organization.

Source