Django, Ajax Long Poll, Postgresql: Inaction Transaction

I implemented chat using a long survey of ajax and Gevent. To read, the ajax client displays the update view and waits for Gevent.event.wait to update.

Problem: The Postgresql operation opened by Django at the beginning of the request (to obtain session information) does not close until the end of the request. And these unoccupied transactions take up a lot of memory.

What would be the cleanest way to close a Postgresql transaction without closing the request? I am currently sending request_finished manually, but this seems like a hack.

+7
source share
2 answers

The way you do this is probably the best way as part of your hack anyway. Is there some reason why you are trying to enable polling during the request-response process, instead of using something like django-socketio ?

+2
source

See here: https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.commit_manually

@transaction.commit_manually def yourview(request): # do your db actions transaction.commit() 

Or, if you prefer context managers:

 def yourview(request): ... with transaction.commit_manually(): # do your db actions ... 

Also, if you have memory problems associated with opening PostgreSQL connections, you should look for a solution for the pool using pgbouncer or various existing gevent connection pools. You should see some significant performance gains from this.

0
source

All Articles