Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you force a memory limit in Django WSGI apps?

I want my app to throw an MemoryError when its usage goes over 1GB. I'm running in WSGI daemon mode.

I see 3 places the memory limit could be:

  • apache.conf
  • wsgi somewhere
  • django configuration

but I can't find the right config options. In PHP you can do this with :

php_value memory_limit 1GB

in your apache.conf

like image 565
Paul Tarjan Avatar asked Feb 13 '10 22:02

Paul Tarjan


2 Answers

Resource memory limits aren't implemented on most platforms even though C API definitions exist. As such, mod_wsgi doesn't try to implement such restrictions. If PHP is doing it, is is able to do so by virtue that that it is a more constrained and controlled environment than Python.

The only portable way is to have a separate daemon process running which runs 'ps' or uses '/proc' to monitor memory usage of processes and then send a SIGINT signal to those which go over some predefined value.


UPDATE

Version 3.4 of mod_wsgi supports options for daemon mode that may be able to restrict memory usage. See:

  • http://code.google.com/p/modwsgi/wiki/ChangesInVersion0304

Whether they work depends on the operating system you are using.

like image 194
Graham Dumpleton Avatar answered Oct 27 '22 09:10

Graham Dumpleton


Use resource.setrlimit() with resource.RLIMIT_VMEM.

like image 44
Ignacio Vazquez-Abrams Avatar answered Oct 27 '22 11:10

Ignacio Vazquez-Abrams