Here we are considering a scenario where we have developed a custom connector logic using python technology and have used Flask as a development server.
It is ok to use flask in development environment to develop and test your connector. But as this was provided only for development, same time it has many disadvantages, like it is not designed to be particularly efficient, stable and secure.
For such reasons, we avoid direct use of python flask service for production environment.
Also, when we run python flask service it shows up a Warning message saying do not use this development server in a production environment.
So, the question comes that we have developed and tested the python package on development then how to deploy it on production without using WSGI server and which one to use?
The answer is to use production WSGI server instead of flask. These are Self-hosted as WSGI servers that has HTTP servers built in.
A WSGI server is used to run the application, converting incoming HTTP requests to the standard WSGI, and converting outgoing WSGI responses to HTTP responses.
There is a list of several Self hosted WSGI servers like Gunicorn, uWSGI, ASGI, gevent etc.
It is suggested to use Waitress, as it has many advantages like it is easy to configure, supports Windows as well as Linux, Secure and no dependencies, and once you installed waitress then you won’t need any other python library.
Now, let’s consider that we have developed and updated our python package using Waitress. The next challenge is, in the customer's Production environment, we need to deploy this package as a Windows Service.
NSSM: What it is?
It is recommended to use NSSM that stands for Non-Sucking Server Manager. This is a service helper that help run Flask application as a windows service.
There are many advantages in choosing NSSM for deploying Python package as Windows Service:
1. If the service stops because of unknown reason, then NSSM restart the service.
2. User can easily manage the service from Services.msc screen.
3. We can also use NSSM commands to run the application’s batch file as windows service.
4. The command line output can also be saved in log files as text.
NSSM to deploy Python package as a Windows service:
Now let's look at one of the examples of using NSSM commands for running python batch file as windows service.
Let’s assume we have server.py python file and we created its batch file named run_server.bat. Then we can use below given NSSM commands to run this batch file as background service on windows.
Once completed executing above commands, we could verify our service if it got deployed successfully from under Services.msc screen.
So, that’s how using NSSM one can deploy Python Flask application as a windows service on customer's environment.
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.