[Python 인터넷] PM2로 Python Flask 앱 관리하기 : Managing Python Flask App with PM2
PM2로 Python Flask 앱 관리하기
Pm2는 프로덕션 중에 애플리케이션을 관리할 수 있는 프로덕션 준비 프로세스 관리자입니다. node.js로 작성되었지만 node.js 앱과 python 앱을 모두 관리하고 모니터링하는 데 사용할 수도 있습니다.
이 튜토리얼에서는 Pm2를 사용하여 플라스크로 빌드된 파이썬 애플리케이션을 관리하고 모니터링하는 방법을 살펴보겠습니다. 이 튜토리얼을 마치면 다음을 배우게 됩니다.
- Pm2 설정 방법
- 백그라운드에서 앱을 지속적으로 실행하는 방법
- 간편한 방법으로 프로세스를 모니터링하는 방법
- 앱을 확장하는 방법
- 등
pm2의 주요 이점은 앱을 시작한 곳에서 활성 터미널을 닫을 때 앱이 중단되지 않고 백그라운드에서 앱을 계속 실행할 수 있다는 것입니다. 어떤 면에서는 supervisor처럼 작동하지만 더 시원합니다. 아래 다이어그램은 제가 말하는 바를 보여줍니다. 열려 있는 터미널을 닫으면 프로세스가 중단되고 앱이 실행되지 않으므로 Pm2나 supervisor 또는 tmux가 필요합니다.
PM2 설치
PM2는 NPM 패키지로 제공되므로 아래와 같이 nodejs npm을 사용하여 설치할 수 있습니다. 프로덕션 시스템에서 작업하는 경우 전역으로 설치하는 것이 좋습니다.
npm 설치 -g pm2
PM을 사용하면 pm2 ls 명령을 사용하여 애플리케이션에서 실행 중인 모든 프로세스를 확인할 수 있습니다 . 이를 통해 애플리케이션에서 실행 중인 모든 프로세스를 나열할 수 있습니다.
PM2LS
PM2로 Flask 애플리케이션 시작
그렇다면 PM2를 파이썬, 특히 플라스크 앱과 함께 어떻게 사용하나요? 매우 간단합니다. node.js 앱에서 사용하는 것과 동일한 명령을 사용합니다. 그러나 –interpreter 명령을 통해 사용할 인터프리터를 지정해야 애플리케이션에서 사용할 파이썬 버전을 알 수 있습니다. 이렇게 하면 `ImportError: No module named Flask` 오류가 방지됩니다. 우리의 경우 python3을 사용하고 있습니다.
pm2 시작 app.py --인터프리터 python3
상태 및 로그 확인
다음 명령을 사용하여 실행 중인 프로세스의 현재 상태를 확인할 수도 있습니다. 이것들은 매우 유용한 명령입니다.
- pm2 상태
- PM2LS
- pm2 로그
이것들을 하나하나 살펴보도록 하자
pm2 상태
PM2에서 실행되는 프로세스 목록을 확인하려면 pm2 ls를 사용할 수도 있습니다 .
상태에 대한 더 자세한 정보를 얻으려면 pm2 logs 명령을 사용할 수 있습니다.
신청 프로세스 모니터링
PM2는 터미널에서 바로 전체 프로세스, CPU 및 메모리 정보를 모니터링하는 방법을 제공합니다. 여기에는 monit 명령을 사용하는 것이 포함됩니다.
pm2 모니터
앱 상세 정보 표시
PM2를 사용하면 백그라운드에서 실행 중인 특정 앱 프로세스에 대한 더 자세한 정보, 일종의 메타데이터를 얻을 수 있습니다. 다음 명령으로 앱 이름이나 프로세스 ID를 지정해야 합니다.
###pm2 <앱 이름>을 표시합니다 pm2 쇼 앱
애플리케이션 상태를 관리하는 방법
PM2를 사용하여 애플리케이션을 시작하면 시스템이 종료될 때까지 계속 실행됩니다. 그러나 앱을 중지하거나 다시 시작하거나 프로세스 목록에서 앱을 완전히 삭제할 수도 있습니다.
앱을 중지하면 프로세스가 중단되지만 pm2 restart appname을 사용하면 쉽게 다시 시작할 수 있습니다.
프로세스 목록에서 애플리케이션을 완전히 삭제하거나 제거하려면 이 명령을 사용해야 합니다.
pm2 앱 삭제
위에서 볼 수 있듯이 pm2 delete 명령을 사용하면 앱이 프로세스 목록에서 완전히 제거됩니다 .
PM2로 할 수 있는 멋진 일이 많이 있습니다. 예를 들어 pm2 시작 및 pm2 저장 명령을 통해 예상치 못한 또는 예상된 서버 재부팅 시 자동 재시작이 가능합니다. 더욱 고급 기능을 원하시면 PM2 Plus 버전과 PM2 Enterprise도 확인해 보세요.
자세한 내용은 아래 비디오 튜토리얼을 확인하세요.
시간 내주셔서 감사합니다
작성자: Jesse E.Agbebemabiase(JCharis)
[출처] https://blog.jcharistech.com/2022/01/14/managing-python-flask-app-with-pm2/
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 app.py --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
By Jesse E.Agbemabiase (JCharis)
[출처] https://blog.jcharistech.com/2022/01/14/managing-python-flask-app-with-pm2/