Managing Python Flask App with PM2

Pm2 is a production ready process manager that allows you to manage your applications during production. It is written in node.js however it can also be used to manage and monitor both node.js apps as well as python apps.

In this tutorial we will explore how to use Pm2 to manage and monitor a python application built with flask. By the end of this tutorial you will learn

  • How to setup Pm2
  • How to run your apps continuously on the background
  • How to monitor your processes with easy
  • How to scale you apps
  • etc

The main benefit of pm2 is to enable you to run your apps continously on the background without the app stopping when you close your active terminal from where you started the app.In a way it works like supervisor but it is cooler. The diagram below demonstrate what I mean. Once you close the open terminal, the process stops and the app fails to run, hence the need for Pm2 or supervisor or tmux.

Installing PM2

PM2 is available as an NPM package hence you can install it using nodejs npm as below. It is recommended to install it globally in case you are working on a production system.

npm install -g pm2

With PM you can check all the running processes for your application using the pm2 ls command. This allows you to list all the processes running for your application.

pm2 ls

Starting A Flask Application with PM2

So how do you use PM2 with python, specifically with flask apps?. It is quite simple , the same command we use for working with node.js apps is what is used. However you may have to specify which interpreter to use via the –interpreter command so that your application will know which python version to use . This will help prevent the `ImportError: No module named flask` error.In our case we are using python3.

pm2 start --interpreter python3

Checking the Status and Logs

We can also use the the following commands to check for the current status of our running process. These are very useful commands.

  • pm2 status
  • pm2 ls
  • pm2 logs

Let us see each of these one after the other

pm2 status

You can also use the pm2 ls for checking for the list of process being run by PM2

To get even more information about the status you can use the pm2 logs command.

Monitoring the Application Processes

PM2 offers a way to monitor the entire processes , the CPU and memory info right in the terminal . This involves the use of the monit command

pm2 monit
Show App Detailed Info

With PM2 you can get a more detailed info, sort of metadata about a particular app process you are running in the background. You will have to specify the app name or the process id with the following command

###pm2 show <appname>
pm2 show app

How to Manage the State of the Application

Once you start an application using PM2, it will continue to run till the system shutdown. However you can also stop the app or restart or even completely delete the app from the process list.

Stopping the app will halt the process however it can still be restarted easily using the pm2 restart appname

To completely delete or remove the application from the process list you will have to use this command

pm2 delete app

As you can see from above the app is completely removed from the process list with the pm2 delete command.

There are alot of cool things we can do with PM2 such as auto restart during unexpected or expected server reboots via the pm2 startup and pm2 save command. You can also check out the PM2 Plus version and PM2 Enterprise for more advanced features.

You can check out the video tutorial below for more.

Thank You for Your Time
Jesus Saves

By Jesse E.Agbemabiase (JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *