How to Configure uWSGI for Automatic Reload When Python Files Change


2 views

During Django development, constantly restarting uWSGI after code changes becomes tedious. Unlike Django's built-in development server which auto-reloads, uWSGI requires explicit configuration for this behavior - a crucial feature for efficient development workflows.

uWSGI provides three primary approaches to enable auto-reloading:

# Method 1: Using --py-autoreload
uwsgi --http :8000 --wsgi-file myapp.py --py-autoreload 1

# Method 2: Touch-reload (monitors specific files)
uwsgi --http :8000 --wsgi-file myapp.py --touch-reload=/path/to/reload.trigger

# Method 3: FS-monitoring (best for development)
uwsgi --http :8000 --wsgi-file myapp.py --fs-reload /path/to/project

For Django projects, the most robust solution combines FS-monitoring with proper signal handling:

# uwsgi.ini configuration
[uwsgi]
http = :8000
chdir = /path/to/project
wsgi-file = project/wsgi.py
processes = 4
threads = 2
master = true
vacuum = true
fs-reload = /path/to/project/core  # Watches specific app directory
py-autoreload = 1                  # Fallback mechanism

While auto-reload is ideal for development, disable it in production. Instead, implement a proper deployment pipeline with:

# Production-safe reload through Emperor
[uwsgi]
emperor = /etc/uwsgi/vassals
emperor-tyranny = true
emperor-pidfile = /var/run/uwsgi-emperor.pid

If reloads fail to trigger:

  • Verify filesystem permissions (uWSGI must have read access)
  • Check inotify limits: sysctl fs.inotify.max_user_watches
  • Test with --logto to monitor reload attempts

During Django development, constantly restarting uWSGI after code changes becomes tedious. While Django's built-in development server offers auto-reload functionality, production deployments typically use uWSGI which doesn't enable this by default.

uWSGI provides several ways to implement automatic reloading:

# Method 1: Using --py-auto-reload
uwsgi --http :8000 --wsgi-file myapp.py --py-auto-reload 1

# Method 2: Using touch-reload
uwsgi --http :8000 --wsgi-file myapp.py --touch-reload /path/to/reload.file

For Django projects, the most effective solution is:

# uwsgi.ini configuration
[uwsgi]
http = :8000
chdir = /path/to/your/project
module = project.wsgi
master = true
processes = 4
py-autoreload = 1  # This enables auto-reloading

For more control over the reload behavior:

[uwsgi]
...
py-autoreload = 2  # Check for changes every 2 seconds
pythonpath = /path/to/your/project
touch-reload = /path/to/trigger/file  # Alternative method

While convenient during development, remember to:

  • Disable py-autoreload in production
  • Monitor CPU usage during development
  • Consider using touch-reload for larger projects

If auto-reload isn't working:

# Check uWSGI version (1.2+ required)
uwsgi --version

# Verify file permissions
ls -la /path/to/your/project

# Check uWSGI logs for errors
tail -f /var/log/uwsgi/yourproject.log