Django Python trash killing

After 2 days of debugging, I kept my whistle to a minimum: the Python garbage collector.
My application stores many objects in memory. And it works well. GC performs regular rounds (I did not play with thresholds by default (700, 10, 10)).
From time to time, in the middle of an important transaction, passing the 2nd generation starts and checks my objects ~ 1.5M generation 2.
It will take 2 seconds! A nominal transaction takes less than 0.1 seconds.

My question is: what should I do?
I can turn off 2nd generation sweeps (setting a very high threshold is the right way?), And the GC is obedient. When should I turn them on?
We implemented a web service using Django, and each user request takes about 0.1 seconds.
Optimally, I will run these GC gen 2 loops between user API requests. But how to do that?
My view ends with return HttpResponse(), AFTER I would like to run the Gen 2 GC generator.
How should I do it? Does this approach make sense?

Is it possible to mark an object that NEVER needs to collect garbage so that the GC does not test them every cycle of the 2nd generation?
How to configure GC to run full deployments when the Django server is relatively inactive?

Python 2.6.6 on multiple platforms (Windows / Linux).

+5
source share
4 answers

I believe that one option would be to completely disable garbage collection, and then manually collect at the end of the request, as suggested here: How does the garbage collection mechanism work?

I assume that you can disable GC in your file settings.py.

GarbageCollection , , :

import gc
class GCMiddleware(object):
    def process_response(self, request, response):
        gc.collect()
        return response
+3

- . , wsgi , , . Django request_finished, .

, 2 :

def pre_request(worker, req):
    # disable gc until end of request
    gc.disable()


def post_request(worker, req, environ, resp):
    # enable gc after a request
    gc.enable()

post_request , HTTP , .

+1

My view HttpResponse(), Gen 2 GC.

// turn off GC
// do stuff
resp = HttpResponse()
// turn on GC
return resp

, //turn on GC // spawn thread to turn on GC in 0.1 sec.

, GC , , , django - django-hook, dcurtis.

, , C/++ , Python /.

0

GC mod_wsgi ( , ), .

0

All Articles