Troubleshooting

How do I fix Django reporting an ImproperlyConfigured error?

With asynchronous workers, creating URLs with the reverse function of django.core.urlresolvers may fail. Use reverse_lazy instead.

How do I avoid Gunicorn excessively blocking in os.fchmod?

The current heartbeat system involves calling os.fchmod on temporary file handlers and may block a worker for arbitrary time if the directory is on a disk-backed filesystem. For example, by default /tmp is not mounted as tmpfs in Ubuntu; in AWS an EBS root instance volume may sometimes hang for half a minute and during this time Gunicorn workers may completely block in os.fchmod. os.fchmod may introduce extra delays if the disk gets full. Also Gunicon may refuse to start if it can’t create the files when the disk is full.

Currently to avoid these problems you can create a tmpfs mount (for a new directory or for /tmp) and pass its path to --worker-tmp-dir. First, check whether your /tmp is disk-backed or RAM-backed:

  1. $ df /tmp
  2. Filesystem 1K-blocks Used Available Use% Mounted on
  3. /dev/xvda1 ... ... ... ... /

No luck. Let’s create a new tmpfs mount:

  1. sudo cp /etc/fstab /etc/fstab.orig
  2. sudo mkdir /mem
  3. echo 'tmpfs /mem tmpfs defaults,size=64m,mode=1777,noatime,comment=for-gunicorn 0 0' | sudo tee -a /etc/fstab
  4. sudo mount /mem

Check the result:

  1. $ df /mem
  2. Filesystem 1K-blocks Used Available Use% Mounted on
  3. tmpfs 65536 0 65536 0% /mem

Now you can set --worker-tmp-dir /mem.