How PM2 Solved My Python Project's Production Challenges on Windows

As a solo Python developer working with a Windows production server, I faced several challenges keeping my Python applications running reliably. PM2, while primarily known as a Node.js process manager, turned out to be the perfect solution for my needs.

The Challenge

I encountered several issues when deploying Python applications in production:

  1. Service Crashes: My Python application would occasionally crash and stay down until I manually restarted it
  2. Manual Restarts: Having to remotely connect to the Windows server to restart services was time-consuming
  3. No Monitoring: Lack of easy way to check if my application was still running
  4. Windows Limitations: Traditional Unix-based solutions weren't suitable for my Windows environment

The PM2 Solution

Here's how I set up PM2 to solve these problems:

Basic Setup

# Install Node.js first (required for PM2)
# Then install PM2 globally
npm install -g pm2

# Install PM2 Windows Service
pm2 install pm2-windows-startup

Simple Configuration

I created a basic ecosystem.config.js file:

module.exports = {
  apps: [{
    name: "my-python-app",
    script: "python",
    args: "main.py",
    interpreter: "none",
    autorestart: true,
    max_restarts: 10,
    watch: false,
    error_file: "logs/err.log",
    out_file: "logs/out.log"
  }]
}

Daily Usage

These are the commands I use most often:

# Start my application
pm2 start ecosystem.config.js

# Check status
pm2 status

# View logs
pm2 logs

# Restart if needed
pm2 restart my-python-app

What Problems It Solved

  1. Automatic Recovery
    • Application now restarts automatically if it crashes
    • No more middle-of-the-night calls to restart services
    • Logs show me what went wrong
  2. Easy Monitoring
    • Quick status check with pm2 status
    • Can see memory usage and uptime
    • All logs in one place
  3. Windows Integration
    • Runs as a proper Windows service
    • Starts automatically with Windows
    • No complicated Windows Service setup needed

My Setup Tips

  1. Basic Configuration
  2. // ecosystem.config.js
    {
      env: {
        PYTHON_PATH: "C:\\Path\\To\\Python\\python.exe"
      }
    }
    1. Logging
      • Keep logs in a dedicated folder
      • Check logs regularly with pm2 logs
      • Clear old logs periodically to save space
    2. Startup
      • Make sure PM2 starts with Windows
      • Test by rebooting the server
      • Verify your app starts automatically

    Conclusion

    For me, PM2 turned out to be exactly what I needed. It:

    • Keeps my Python application running 24/7
    • Handles crashes automatically
    • Provides easy monitoring
    • Works great on Windows

    If you're a solo developer running Python applications on Windows servers, PM2 offers a simple yet powerful solution to keep your applications running reliably without constant maintenance.

 

Comments (0)

Leave a comment