# Gunicorn recommends 2-4 x number_of_cpu_cores, but
# you'll want to vary this a bit to find the best for your
# particular work load.
-workers = 4
+workers = 1
# Some application-wide initialization is needed.
on_starting = openerp.wsgi.on_starting
# big reports for example
timeout = 240
+#max_requests = 150
+
# Equivalent of --load command-line option
openerp.conf.server_wide_modules = ['web']
# Path to the OpenERP Addons repository (comma-separated for
# multiple locations)
-conf['addons_path'] = '/home/openerp/addons/trunk,/home/openerp/web/trunk/addons'
+conf['addons_path'] = '/home/thu/repos/addons/trunk,/home/thu/repos/web/trunk/addons,/home/thu/repos/server/trunk-limits/openerp/tests/addons'
# Optional database config if not using local socket
#conf['db_name'] = 'mycompany'
# If --static-http-enable is used, path for the static web directory
#conf['static_http_document_root'] = '/var/www'
+def time_expired(n, stack):
+ import os
+ import time
+ print '>>> [%s] time ran out.' % (os.getpid())
+ raise Exception('(time ran out)')
+
+def pre_request(worker, req):
+ import os
+ import psutil
+ import resource
+ import signal
+ # VMS and RLIMIT_AS are the same thing: virtual memory, a.k.a. address space
+ rss, vms = psutil.Process(os.getpid()).get_memory_info()
+ soft, hard = resource.getrlimit(resource.RLIMIT_AS)
+ print ">>>>>> [%s] %s %s %s %s %s" % (os.getpid(), vms, req.method, req.path, req.query, req.fragment)
+ print ">>>>>> %s" % (req.body,)
+ # Let's say 512MB is ok, 768 is a hard limit (will raise MemoryError),
+ # 640 will nicely restart the process.
+ resource.setrlimit(resource.RLIMIT_AS, (768 * 1024 * 1024, hard))
+ if vms > 640 * 1024 * 1024:
+ print ">>> Worker eating too much memory, reset it after the request."
+ worker.alive = False # Commit suicide after the request.
+
+ r = resource.getrusage(resource.RUSAGE_SELF)
+ cpu_time = r.ru_utime + r.ru_stime
+ signal.signal(signal.SIGXCPU, time_expired)
+ soft, hard = resource.getrlimit(resource.RLIMIT_CPU)
+ resource.setrlimit(resource.RLIMIT_CPU, (cpu_time + 15, hard))
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
--- /dev/null
+# -*- coding: utf-8 -*-
+import time
+
+import openerp
+
+class m(openerp.osv.osv.Model):
+ """ This model exposes a few methods that will consume between 'almost no
+ resource' and 'a lot of resource'.
+ """
+ _name = 'test.limits.model'
+
+ def consume_nothing(self, cr, uid, context=None):
+ return True
+
+ def consume_memory(self, cr, uid, size, context=None):
+ l = [0] * size
+ return True
+
+ def leak_memory(self, cr, uid, size, context=None):
+ if not hasattr(self, 'l'):
+ self.l = []
+ self.l.append([0] * size)
+ print ">>>", len(self.l)
+ return True
+
+ def consume_time(self, cr, uid, seconds, context=None):
+ time.sleep(seconds)
+ return True
+
+ def consume_cpu_time(self, cr, uid, seconds, context=None):
+ import os
+ t0 = time.clock()
+ t1 = time.clock()
+# try:
+ while t1 - t0 < seconds:
+ print "[%s] ..." % os.getpid()
+ for i in xrange(10000000):
+ x = i * i
+ t1 = time.clock()
+# except Exception, e:
+# print "+++", e
+ return True
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: