Django, Ajax polling lungo, PostgreSQL: operazione di inattività
-
25-10-2019 - |
Domanda
I implementata una chat, utilizzando Ajax polling lungo e Gevent. Per leggere, client AJAX la vista di aggiornamento e di attesa con Gevent.event.wait per un aggiornamento.
Problema: L'operazione PostgreSQL aperto da Django all'inizio di una richiesta (per ottenere le informazioni di sessione) non è chiuso fino alla fine della richiesta. E tali operazioni di inattività prendere un sacco di memoria.
Quale sarebbe il modo più pulito per chiudere la transazione PostgreSQL senza chiudere la richiesta? Attualmente sto inviando il segnale request_finished manualmente, ma ci si sente come un hack.
Soluzione
The way you're doing it is probably the the best way within the framework of your hack anyway. Is there any reason you're trying to shoe-horn long-poll into the request-response process instead of using something like django-socketio?
Altri suggerimenti
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're having memory issues holding PostgreSQL connections open you should look a pooling solution using pgbouncer or the various gevent connection pools that exist. You should see some sizable performance gains from doing this.